| 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 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 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 | |
|
| 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 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 0 | Connection connection = driverObject.connect(url, driverProperties); |
| 87 | |
|
| 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 | |
} |