use of liquibase.diff.Diff in project collect by openforis.
the class LiquibaseRelationalSchemaCreator method createRelationalSchema.
@Override
public void createRelationalSchema(RelationalSchema schema, Connection targetConn) throws CollectRdbException {
PrintStream ps = null;
try {
LiquidbaseDatabaseSnapshotBuilder snapshotGen = new LiquidbaseDatabaseSnapshotBuilder();
Database rdb = getDatabaseImplementation(targetConn);
boolean dbSupportsFKs = rdb instanceof SQLiteDatabase ? false : true;
DatabaseSnapshot generatedSnapshot = snapshotGen.createSnapshot(schema, dbSupportsFKs);
String targetSchema = schema.getName();
rdb.setDefaultSchemaName(targetSchema);
DatabaseSnapshot emptyDbSnapshot = new DatabaseSnapshot(rdb, targetSchema);
// Generate change set
Diff diff = new Diff(generatedSnapshot, emptyDbSnapshot);
DiffResult diffResult = diff.compare();
File tmpFile = File.createTempFile("collect-schemagen", ".xml");
ps = new PrintStream(new FileOutputStream(tmpFile));
diffResult.setChangeSetAuthor("collect3");
diffResult.setChangeSetContext("schemagen");
System.out.println("Writing change log to " + tmpFile.getAbsolutePath());
diffResult.printChangeLog(ps, rdb);
ps.flush();
// Execute change set
Liquibase liq = new Liquibase(tmpFile.getName(), new FileSystemResourceAccessor(tmpFile.getParent()), rdb);
liq.update("schemagen");
} catch (LiquibaseException e) {
throw new CollectRdbException("Failed to update schema", e);
} catch (IOException e) {
throw new CollectRdbException("Failed to create temp db changelog file", e);
} catch (ParserConfigurationException e) {
throw new CollectRdbException("Failed to write temp db changelog file", e);
} finally {
if (ps != null) {
ps.close();
}
}
}
Aggregations