Coverage Report - org.liquibase.maven.plugins.LiquibaseDatabaseDiff
 
Classes in this File Line Coverage Branch Coverage Complexity
LiquibaseDatabaseDiff
0%
0/41
0%
0/8
3.2
 
 1  
 // Version: $Id: $
 2  
 // Copyright: Copyright(c) 2008 Trace Financial Limited
 3  
 package org.liquibase.maven.plugins;
 4  
 
 5  
 import java.io.IOException;
 6  
 
 7  
 import javax.xml.parsers.ParserConfigurationException;
 8  
 
 9  
 import liquibase.Liquibase;
 10  
 import liquibase.database.Database;
 11  
 import liquibase.exception.LiquibaseException;
 12  
 import liquibase.integration.commandline.CommandLineUtils;
 13  
 
 14  
 import org.apache.maven.plugin.MojoExecutionException;
 15  
 import org.apache.maven.plugin.MojoFailureException;
 16  
 import org.apache.maven.wagon.authentication.AuthenticationInfo;
 17  
 
 18  
 /**
 19  
  * Generates a diff between the specified database and the reference database.
 20  
  * 
 21  
  * @author Peter Murray
 22  
  * @goal diff
 23  
  */
 24  0
 public class LiquibaseDatabaseDiff extends AbstractLiquibaseChangeLogMojo {
 25  
 
 26  
     /**
 27  
      * The fully qualified name of the driver class to use to connect to the reference database. If this is not
 28  
      * specified, then the {@link #driver} will be used instead.
 29  
      * 
 30  
      * @parameter expression="${liquibase.referenceDriver}"
 31  
      */
 32  
     protected String referenceDriver;
 33  
 
 34  
     /**
 35  
      * The reference database URL to connect to for executing Liquibase. If performing a diff against a Hibernate config
 36  
      * xml file, then use <b>"hibernate:PATH_TO_CONFIG_XML"</b> as the URL. The path to the hibernate configuration file
 37  
      * can be relative to the test classpath for the Maven project.
 38  
      * 
 39  
      * @parameter expression="${liquibase.referenceUrl}"
 40  
      */
 41  
     protected String referenceUrl;
 42  
 
 43  
     /**
 44  
      * The reference database username to use to connect to the specified database.
 45  
      * 
 46  
      * @parameter expression="${liquibase.referenceUsername}"
 47  
      */
 48  
     protected String referenceUsername;
 49  
 
 50  
     /**
 51  
      * The reference database password to use to connect to the specified database. If this is null then an empty
 52  
      * password will be used.
 53  
      * 
 54  
      * @parameter expression="${liquibase.referencePassword}"
 55  
      */
 56  
     protected String referencePassword;
 57  
 
 58  
     /**
 59  
      * The reference database password to use to connect to the specified database. If this is null then an empty
 60  
      * password will be used.
 61  
      * 
 62  
      * @parameter expression="${liquibase.referenceDefaultSchemaName}"
 63  
      */
 64  
     protected String referenceDefaultSchemaName;
 65  
 
 66  
     /**
 67  
      * The server id in settings.xml to use when authenticating with.
 68  
      * 
 69  
      * @parameter expression="${liquibase.referenceServer}"
 70  
      */
 71  
     private String referenceServer;
 72  
 
 73  
     /**
 74  
      * The diff change log file to output the differences to. If this is null then the differences will be output to the
 75  
      * screen.
 76  
      * 
 77  
      * @parameter expression="${liquibase.diffChangeLogFile}"
 78  
      */
 79  
     protected String diffChangeLogFile;
 80  
 
 81  
     @Override
 82  
     public void execute() throws MojoExecutionException, MojoFailureException {
 83  0
         AuthenticationInfo referenceInfo = wagonManager.getAuthenticationInfo(referenceServer);
 84  0
         if (referenceInfo != null) {
 85  0
             referenceUsername = referenceInfo.getUserName();
 86  0
             referencePassword = referenceInfo.getPassword();
 87  
         }
 88  
 
 89  0
         super.execute();
 90  0
     }
 91  
 
 92  
     @Override
 93  
     protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
 94  0
         ClassLoader cl = null;
 95  
         try {
 96  0
             cl = getMavenArtifactClassLoader();
 97  0
             Thread.currentThread().setContextClassLoader(cl);
 98  0
         } catch (MojoExecutionException e) {
 99  0
             throw new LiquibaseException("Could not create the class loader, " + e, e);
 100  0
         }
 101  
 
 102  0
         Database db = liquibase.getDatabase();
 103  0
         Database referenceDatabase = CommandLineUtils.createDatabaseObject(cl, referenceUrl, referenceUsername,
 104  
                 referencePassword, referenceDriver, referenceDefaultSchemaName, null, null);
 105  
 
 106  0
         getLog().info("Performing Diff on database " + db.toString());
 107  0
         if (diffChangeLogFile != null) {
 108  
             try {
 109  0
                 CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db);
 110  0
                 getLog().info("Differences written to Change Log File, " + diffChangeLogFile);
 111  0
             } catch (IOException e) {
 112  0
                 throw new LiquibaseException(e);
 113  0
             } catch (ParserConfigurationException e) {
 114  0
                 throw new LiquibaseException(e);
 115  0
             }
 116  
         } else {
 117  0
             CommandLineUtils.doDiff(referenceDatabase, db);
 118  
         }
 119  0
     }
 120  
 
 121  
     @Override
 122  
     protected void printSettings(String indent) {
 123  0
         super.printSettings(indent);
 124  0
         getLog().info(indent + "referenceDriver: " + referenceDriver);
 125  0
         getLog().info(indent + "referenceUrl: " + referenceUrl);
 126  0
         getLog().info(indent + "referenceUsername: " + referenceUsername);
 127  0
         getLog().info(indent + "referencePassword: " + referencePassword);
 128  0
         getLog().info(indent + "referenceDefaultSchema: " + referenceDefaultSchemaName);
 129  0
         getLog().info(indent + "diffChangeLogFile: " + diffChangeLogFile);
 130  0
     }
 131  
 
 132  
     @Override
 133  
     protected void checkRequiredParametersAreSpecified() throws MojoFailureException {
 134  0
         super.checkRequiredParametersAreSpecified();
 135  
 
 136  0
         if (referenceUrl == null) {
 137  0
             throw new MojoFailureException("A reference database or hibernate configuration file "
 138  
                     + "must be provided to perform a diff.");
 139  
         }
 140  
 
 141  0
         if (referencePassword == null) {
 142  0
             referencePassword = "";
 143  
         }
 144  0
     }
 145  
 
 146  
     @Override
 147  
     protected boolean isPromptOnNonLocalDatabase() {
 148  0
         return false;
 149  
     }
 150  
 }