use of liquibase.diff.output.DiffOutputControl in project liquibase by liquibase.
the class LiquibaseGenerateChangeLogMojo method performLiquibaseTask.
@Override
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
ClassLoader cl = null;
try {
cl = getClassLoaderIncludingProjectClasspath();
Thread.currentThread().setContextClassLoader(cl);
} catch (MojoExecutionException e) {
throw new LiquibaseException("Could not create the class loader, " + e, e);
}
Database database = liquibase.getDatabase();
getLog().info("Generating Change Log from database " + database.toString());
try {
DiffOutputControl diffOutputControl = new DiffOutputControl(outputDefaultCatalog, outputDefaultSchema, true, null);
if (diffExcludeObjects != null && diffIncludeObjects != null) {
throw new UnexpectedLiquibaseException("Cannot specify both excludeObjects and includeObjects");
}
if (diffExcludeObjects != null) {
diffOutputControl.setObjectChangeFilter(new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.EXCLUDE, diffExcludeObjects));
}
if (diffIncludeObjects != null) {
diffOutputControl.setObjectChangeFilter(new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.INCLUDE, diffIncludeObjects));
}
CommandLineUtils.doGenerateChangeLog(outputChangeLogFile, database, defaultCatalogName, defaultSchemaName, StringUtils.trimToNull(diffTypes), StringUtils.trimToNull(changeSetAuthor), StringUtils.trimToNull(changeSetContext), StringUtils.trimToNull(dataDir), diffOutputControl);
getLog().info("Output written to Change Log file, " + outputChangeLogFile);
} catch (IOException e) {
throw new LiquibaseException(e);
} catch (ParserConfigurationException e) {
throw new LiquibaseException(e);
}
}
use of liquibase.diff.output.DiffOutputControl in project liquibase by liquibase.
the class DiffDatabaseToChangeLogTask method executeWithLiquibaseClassloader.
@Override
protected void executeWithLiquibaseClassloader() throws BuildException {
for (ChangeLogOutputFile changeLogOutputFile : changeLogOutputFiles) {
PrintStream printStream = null;
String encoding = getOutputEncoding(changeLogOutputFile);
try {
FileResource outputFile = changeLogOutputFile.getOutputFile();
ChangeLogSerializer changeLogSerializer = changeLogOutputFile.getChangeLogSerializer();
printStream = new PrintStream(outputFile.getOutputStream(), true, encoding);
DiffResult diffResult = getDiffResult();
DiffOutputControl diffOutputControl = getDiffOutputControl();
DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffResult, diffOutputControl);
diffToChangeLog.print(printStream, changeLogSerializer);
} catch (UnsupportedEncodingException e) {
throw new BuildException("Unable to diff databases to change log file. Encoding [" + encoding + "] is not supported.", e);
} catch (IOException e) {
throw new BuildException("Unable to diff databases to change log file. Error creating output stream.", e);
} catch (ParserConfigurationException e) {
throw new BuildException("Unable to diff databases to change log file. Error configuring parser.", e);
} catch (DatabaseException e) {
throw new BuildException("Unable to diff databases to change log file. " + e.toString(), e);
} finally {
FileUtils.close(printStream);
}
}
}
use of liquibase.diff.output.DiffOutputControl in project liquibase by liquibase.
the class GenerateChangeLogTask method executeWithLiquibaseClassloader.
@Override
public void executeWithLiquibaseClassloader() throws BuildException {
Liquibase liquibase = getLiquibase();
Database database = liquibase.getDatabase();
CatalogAndSchema catalogAndSchema = buildCatalogAndSchema(database);
DiffOutputControl diffOutputControl = getDiffOutputControl();
DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffOutputControl);
for (ChangeLogOutputFile changeLogOutputFile : changeLogOutputFiles) {
String encoding = getOutputEncoding(changeLogOutputFile);
PrintStream printStream = null;
try {
FileResource outputFile = changeLogOutputFile.getOutputFile();
ChangeLogSerializer changeLogSerializer = changeLogOutputFile.getChangeLogSerializer();
log("Writing change log file " + outputFile.toString(), Project.MSG_INFO);
printStream = new PrintStream(outputFile.getOutputStream(), true, encoding);
liquibase.generateChangeLog(catalogAndSchema, diffToChangeLog, printStream, changeLogSerializer);
} catch (UnsupportedEncodingException e) {
throw new BuildException("Unable to generate a change log. Encoding [" + encoding + "] is not supported.", e);
} catch (IOException e) {
throw new BuildException("Unable to generate a change log. Error creating output stream.", e);
} catch (ParserConfigurationException e) {
throw new BuildException("Unable to generate a change log. Error configuring parser.", e);
} catch (DatabaseException e) {
throw new BuildException("Unable to generate a change log. " + e.toString(), e);
} finally {
FileUtils.close(printStream);
}
}
}
use of liquibase.diff.output.DiffOutputControl in project liquibase by liquibase.
the class AbstractIntegrationTest method testRerunDiffChangeLogAltSchema.
@Test
public void testRerunDiffChangeLogAltSchema() throws Exception {
if (database == null) {
return;
}
if (!database.supportsSchemas()) {
return;
}
Liquibase liquibase = createLiquibase(includedChangeLog);
clearDatabase(liquibase);
database.setDefaultSchemaName("lbcat2");
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.forceReleaseLock();
liquibase.update(includedChangeLog);
DatabaseSnapshot originalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(CatalogAndSchema.DEFAULT, new CatalogAndSchema(null, "lbcat2")) }, originalSnapshot.getSnapshotControl().getTypesToInclude());
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, null, compareControl);
File tempFile = File.createTempFile("liquibase-test", ".xml");
FileOutputStream output = new FileOutputStream(tempFile);
try {
new DiffToChangeLog(diffResult, new DiffOutputControl()).print(new PrintStream(output));
output.flush();
} finally {
output.close();
}
liquibase = createLiquibase(tempFile.getName());
clearDatabase(liquibase);
//run again to test changelog testing logic
Executor executor = ExecutorService.getInstance().getExecutor(database);
try {
executor.execute(new DropTableStatement("lbcat2", "lbcat2", database.getDatabaseChangeLogTableName(), false));
} catch (DatabaseException e) {
//ok
}
try {
executor.execute(new DropTableStatement("lbcat2", "lbcat2", database.getDatabaseChangeLogLockTableName(), false));
} catch (DatabaseException e) {
//ok
}
database.commit();
DatabaseConnection connection = DatabaseTestContext.getInstance().getConnection(url);
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
database.setDefaultSchemaName("lbcat2");
liquibase = createLiquibase(tempFile.getName());
try {
liquibase.update(this.contexts);
} catch (ValidationFailedException e) {
e.printDescriptiveError(System.out);
throw e;
}
tempFile.deleteOnExit();
DatabaseSnapshot finalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
CompareControl finalCompareControl = new CompareControl();
finalCompareControl.addSuppressedField(Column.class, "autoIncrementInformation");
DiffResult finalDiffResult = DiffGeneratorFactory.getInstance().compare(originalSnapshot, finalSnapshot, finalCompareControl);
new DiffToReport(finalDiffResult, System.out).print();
assertTrue(finalDiffResult.areEqual());
}
use of liquibase.diff.output.DiffOutputControl in project liquibase by liquibase.
the class AbstractIntegrationTest method testRerunDiffChangeLog.
@Test
public void testRerunDiffChangeLog() throws Exception {
if (database == null) {
return;
}
for (int run = 0; run < 2; run++) {
//run once outputting data as insert, once as csv
boolean outputCsv = run == 1;
runCompleteChangeLog();
SnapshotControl snapshotControl = new SnapshotControl(database);
//todo compareControl.setDiffData(true);
DatabaseSnapshot originalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, snapshotControl);
CompareControl compareControl = new CompareControl();
//database returns different data even if the same
compareControl.addSuppressedField(Column.class, "defaultValue");
//database returns different data even if the same
compareControl.addSuppressedField(Column.class, "autoIncrementInformation");
if (database instanceof OracleDatabase) {
//database returns different nvarchar2 info even though they are the same
compareControl.addSuppressedField(Column.class, "type");
}
DiffOutputControl diffOutputControl = new DiffOutputControl();
File tempFile = File.createTempFile("liquibase-test", ".xml");
deleteOnExit(tempFile);
if (outputCsv) {
diffOutputControl.setDataDir(new File(tempFile.getParentFile(), "liquibase-data").getCanonicalPath().replaceFirst("\\w:", ""));
}
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, null, compareControl);
FileOutputStream output = new FileOutputStream(tempFile);
try {
new DiffToChangeLog(diffResult, new DiffOutputControl()).print(new PrintStream(output));
output.flush();
} finally {
output.close();
}
Liquibase liquibase = createLiquibase(tempFile.getName());
clearDatabase(liquibase);
DatabaseSnapshot emptySnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
//run again to test changelog testing logic
liquibase = createLiquibase(tempFile.getName());
try {
liquibase.update(this.contexts);
} catch (ValidationFailedException e) {
e.printDescriptiveError(System.out);
throw e;
}
// tempFile.deleteOnExit();
DatabaseSnapshot migratedSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
DiffResult finalDiffResult = DiffGeneratorFactory.getInstance().compare(originalSnapshot, migratedSnapshot, compareControl);
try {
assertTrue(finalDiffResult.areEqual());
} catch (AssertionError e) {
new DiffToReport(finalDiffResult, System.err).print();
throw e;
}
//diff to empty and drop all
DiffResult emptyDiffResult = DiffGeneratorFactory.getInstance().compare(emptySnapshot, migratedSnapshot, compareControl);
output = new FileOutputStream(tempFile);
try {
new DiffToChangeLog(emptyDiffResult, new DiffOutputControl(true, true, true, null)).print(new PrintStream(output));
output.flush();
} finally {
output.close();
}
liquibase = createLiquibase(tempFile.getName());
System.out.println("updating from " + tempFile.getCanonicalPath());
try {
liquibase.update(this.contexts);
} catch (LiquibaseException e) {
throw e;
}
DatabaseSnapshot emptyAgainSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
assertEquals(2, emptyAgainSnapshot.get(Table.class).size());
assertEquals(0, emptyAgainSnapshot.get(View.class).size());
}
}
Aggregations