Coverage Report - liquibase.integration.commandline.CommandLineUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CommandLineUtils
0%
0/60
0%
0/22
4.8
CommandLineUtils$1
N/A
N/A
4.8
CommandLineUtils$OutDiffStatusListener
0%
0/3
N/A
4.8
 
 1  
 package liquibase.integration.commandline;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.IOException;
 6  
 import java.io.PrintStream;
 7  
 import java.sql.Connection;
 8  
 import java.sql.Driver;
 9  
 import java.util.Properties;
 10  
 
 11  
 import javax.xml.parsers.ParserConfigurationException;
 12  
 
 13  
 import liquibase.database.Database;
 14  
 import liquibase.database.DatabaseFactory;
 15  
 import liquibase.database.jvm.JdbcConnection;
 16  
 import liquibase.diff.Diff;
 17  
 import liquibase.diff.DiffResult;
 18  
 import liquibase.diff.DiffStatusListener;
 19  
 import liquibase.exception.DatabaseException;
 20  
 import liquibase.logging.LogFactory;
 21  
 import liquibase.util.StringUtils;
 22  
 
 23  
 /**
 24  
  * Common Utilitiy methods used in the CommandLine application and the Maven plugin. These methods were orignally moved
 25  
  * from {@link Main} so they could be shared.
 26  
  * 
 27  
  * @author Peter Murray
 28  
  */
 29  0
 public class CommandLineUtils {
 30  
 
 31  
     public static Database createDatabaseObject(ClassLoader classLoader, String url, String username, String password,
 32  
             String driver, String defaultSchemaName, String databaseClass, String driverPropertiesFile)
 33  
             throws DatabaseException {
 34  0
         if (driver == null) {
 35  0
             driver = DatabaseFactory.getInstance().findDefaultDriver(url);
 36  
         }
 37  
 
 38  
         try {
 39  
             Driver driverObject;
 40  0
             DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
 41  0
             if (databaseClass != null) {
 42  0
                 databaseFactory.clearRegistry();
 43  0
                 databaseFactory.register((Database) Class.forName(databaseClass, true, classLoader).newInstance());
 44  
             }
 45  
 
 46  
             try {
 47  0
                 if (driver == null) {
 48  0
                     driver = databaseFactory.findDefaultDriver(url);
 49  
                 }
 50  
 
 51  0
                 if (driver == null) {
 52  0
                     throw new RuntimeException(
 53  
                             "Driver class was not specified and could not be determined from the url (" + url + ")");
 54  
                 }
 55  
 
 56  0
                 driverObject = (Driver) Class.forName(driver, true, classLoader).newInstance();
 57  0
             } catch (Exception e) {
 58  0
                 throw new RuntimeException("Cannot find database driver: " + e.getMessage());
 59  0
             }
 60  
 
 61  0
             Properties driverProperties = new Properties();
 62  
 
 63  0
             if (username != null) {
 64  0
                 driverProperties.put("user", username);
 65  
             }
 66  0
             if (password != null) {
 67  0
                 driverProperties.put("password", password);
 68  
             }
 69  0
             if (null != driverPropertiesFile) {
 70  0
                 File propertiesFile = new File(driverPropertiesFile);
 71  0
                 if (propertiesFile.exists()) {
 72  
                     // System.out.println("Loading properties from the file:'" + driverPropertiesFile + "'");
 73  0
                     driverProperties.load(new FileInputStream(propertiesFile));
 74  
                 } else {
 75  0
                     throw new RuntimeException("Can't open JDBC Driver specific properties from the file: '"
 76  
                             + driverPropertiesFile + "'");
 77  
                 }
 78  
             }
 79  
 
 80  
             // System.out.println("Properties:");
 81  
             // for (Map.Entry entry : driverProperties.entrySet()) {
 82  
             // System.out.println("Key:'"+entry.getKey().toString()+"' Value:'"+entry.getValue().toString()+"'");
 83  
             // }
 84  
 
 85  
             // System.out.println("Connecting to the URL:'"+url+"' using driver:'"+driverObject.getClass().getName()+"'");
 86  0
             Connection connection = driverObject.connect(url, driverProperties);
 87  
             // System.out.println("Connection has been created");
 88  0
             if (connection == null) {
 89  0
                 throw new DatabaseException("Connection could not be created to " + url + " with driver "
 90  
                         + driverObject.getClass().getName() + ".  Possibly the wrong driver for the given database URL");
 91  
             }
 92  
 
 93  0
             Database database = databaseFactory.findCorrectDatabaseImplementation(new JdbcConnection(connection));
 94  0
             database.setDefaultSchemaName(StringUtils.trimToNull(defaultSchemaName));
 95  0
             return database;
 96  0
         } catch (Exception e) {
 97  0
             throw new DatabaseException(e);
 98  
         }
 99  
     }
 100  
 
 101  
     public static void doDiff(Database referenceDatabase, Database targetDatabase) throws DatabaseException {
 102  0
         Diff diff = new Diff(referenceDatabase, targetDatabase);
 103  0
         diff.addStatusListener(new OutDiffStatusListener());
 104  0
         DiffResult diffResult = diff.compare();
 105  
 
 106  0
         System.out.println("");
 107  0
         System.out.println("Diff Results:");
 108  0
         diffResult.printResult(System.out);
 109  0
     }
 110  
 
 111  
     public static void doDiffToChangeLog(String changeLogFile, Database referenceDatabase, Database targetDatabase)
 112  
             throws DatabaseException, IOException, ParserConfigurationException {
 113  0
         Diff diff = new Diff(referenceDatabase, targetDatabase);
 114  0
         diff.addStatusListener(new OutDiffStatusListener());
 115  0
         DiffResult diffResult = diff.compare();
 116  
 
 117  0
         if (changeLogFile == null) {
 118  0
             diffResult.printChangeLog(System.out, targetDatabase);
 119  
         } else {
 120  0
             diffResult.printChangeLog(changeLogFile, targetDatabase);
 121  
         }
 122  0
     }
 123  
 
 124  
     public static void doGenerateChangeLog(String changeLogFile, Database originalDatabase, String defaultSchemaName,
 125  
             String diffTypes, String author, String context, String dataDir) throws DatabaseException, IOException,
 126  
             ParserConfigurationException {
 127  0
         Diff diff = new Diff(originalDatabase, defaultSchemaName);
 128  0
         diff.setDiffTypes(diffTypes);
 129  
 
 130  0
         diff.addStatusListener(new OutDiffStatusListener());
 131  0
         DiffResult diffResult = diff.compare();
 132  0
         diffResult.setChangeSetAuthor(author);
 133  0
         diffResult.setChangeSetContext(context);
 134  0
         diffResult.setDataDir(dataDir);
 135  
 
 136  0
         if (StringUtils.trimToNull(changeLogFile) != null) {
 137  0
             diffResult.printChangeLog(changeLogFile, originalDatabase);
 138  
         } else {
 139  0
             PrintStream outputStream = System.out;
 140  0
             diffResult.printChangeLog(outputStream, originalDatabase);
 141  
         }
 142  0
     }
 143  
 
 144  0
     private static class OutDiffStatusListener implements DiffStatusListener {
 145  
 
 146  
         @Override
 147  
         public void statusUpdate(String message) {
 148  0
             LogFactory.getLogger().info(message);
 149  
 
 150  0
         }
 151  
 
 152  
     }
 153  
 
 154  
 }