use of liquibase.diff.DiffResult in project liquibase by liquibase.
the class AbstractJdbcDatabase method dropDatabaseObjects.
// ------- DATABASE OBJECT DROPPING METHODS ---- //
/**
* Drops all objects owned by the connected user.
*/
@Override
public void dropDatabaseObjects(final CatalogAndSchema schemaToDrop) throws LiquibaseException {
ObjectQuotingStrategy currentStrategy = this.getObjectQuotingStrategy();
this.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
try {
DatabaseSnapshot snapshot;
try {
final SnapshotControl snapshotControl = new SnapshotControl(this);
final Set<Class<? extends DatabaseObject>> typesToInclude = snapshotControl.getTypesToInclude();
//We do not need to remove indexes and primary/unique keys explicitly. They should be removed
//as part of tables.
typesToInclude.remove(Index.class);
typesToInclude.remove(PrimaryKey.class);
typesToInclude.remove(UniqueConstraint.class);
if (supportsForeignKeyDisable()) {
//We do not remove ForeignKey because they will be disabled and removed as parts of tables.
typesToInclude.remove(ForeignKey.class);
}
final long createSnapshotStarted = System.currentTimeMillis();
snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(schemaToDrop, this, snapshotControl);
LogFactory.getLogger().debug(String.format("Database snapshot generated in %d ms. Snapshot includes: %s", System.currentTimeMillis() - createSnapshotStarted, typesToInclude));
} catch (LiquibaseException e) {
throw new UnexpectedLiquibaseException(e);
}
final long changeSetStarted = System.currentTimeMillis();
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(new EmptyDatabaseSnapshot(this), snapshot, new CompareControl(snapshot.getSnapshotControl().getTypesToInclude()));
List<ChangeSet> changeSets = new DiffToChangeLog(diffResult, new DiffOutputControl(true, true, false, null).addIncludedSchema(schemaToDrop)).generateChangeSets();
LogFactory.getLogger().debug(String.format("ChangeSet to Remove Database Objects generated in %d ms.", System.currentTimeMillis() - changeSetStarted));
boolean previousAutoCommit = this.getAutoCommitMode();
//clear out currently executed statements
this.commit();
//some DDL doesn't work in autocommit mode
this.setAutoCommit(false);
final boolean reEnableFK = supportsForeignKeyDisable() && disableForeignKeyChecks();
try {
for (ChangeSet changeSet : changeSets) {
changeSet.setFailOnError(false);
for (Change change : changeSet.getChanges()) {
if (change instanceof DropTableChange) {
((DropTableChange) change).setCascadeConstraints(true);
}
SqlStatement[] sqlStatements = change.generateStatements(this);
for (SqlStatement statement : sqlStatements) {
ExecutorService.getInstance().getExecutor(this).execute(statement);
}
}
this.commit();
}
} finally {
if (reEnableFK) {
enableForeignKeyChecks();
}
}
ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(this).destroy();
LockServiceFactory.getInstance().getLockService(this).destroy();
this.setAutoCommit(previousAutoCommit);
} finally {
this.setObjectQuotingStrategy(currentStrategy);
this.commit();
}
}
use of liquibase.diff.DiffResult in project liquibase by liquibase.
the class Liquibase method generateChangeLog.
public void generateChangeLog(CatalogAndSchema catalogAndSchema, DiffToChangeLog changeLogWriter, PrintStream outputStream, ChangeLogSerializer changeLogSerializer, Class<? extends DatabaseObject>... snapshotTypes) throws DatabaseException, IOException, ParserConfigurationException {
Set<Class<? extends DatabaseObject>> finalCompareTypes = null;
if (snapshotTypes != null && snapshotTypes.length > 0) {
finalCompareTypes = new HashSet<Class<? extends DatabaseObject>>(Arrays.asList(snapshotTypes));
}
SnapshotControl snapshotControl = new SnapshotControl(this.getDatabase(), snapshotTypes);
CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, finalCompareTypes);
// compareControl.addStatusListener(new OutDiffStatusListener());
DatabaseSnapshot originalDatabaseSnapshot = null;
try {
originalDatabaseSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), getDatabase(), snapshotControl);
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(originalDatabaseSnapshot, SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), null, snapshotControl), compareControl);
changeLogWriter.setDiffResult(diffResult);
if (changeLogSerializer != null) {
changeLogWriter.print(outputStream, changeLogSerializer);
} else {
changeLogWriter.print(outputStream);
}
} catch (InvalidExampleException e) {
throw new UnexpectedLiquibaseException(e);
}
}
use of liquibase.diff.DiffResult in project liquibase by liquibase.
the class H2IntegrationTest method diffToChangeLog.
@Test
public void diffToChangeLog() throws Exception {
if (getDatabase() == null) {
return;
}
runCompleteChangeLog();
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(getDatabase(), null, new CompareControl());
new DiffToChangeLog(diffResult, new DiffOutputControl(true, true, true, null)).print(System.out);
}
use of liquibase.diff.DiffResult in project liquibase by liquibase.
the class H2IntegrationTest method diffToPrintStream.
@Test
public void diffToPrintStream() throws Exception {
if (getDatabase() == null) {
return;
}
runCompleteChangeLog();
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(getDatabase(), null, new CompareControl());
new DiffToReport(diffResult, System.out).print();
}
use of liquibase.diff.DiffResult in project liquibase by liquibase.
the class AbstractIntegrationTest method testDiff.
@Test
public void testDiff() throws Exception {
if (database == null) {
return;
}
runCompleteChangeLog();
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");
DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, database, compareControl);
try {
assertTrue(diffResult.areEqual());
} catch (AssertionError e) {
new DiffToReport(diffResult, System.err).print();
throw e;
}
}
Aggregations