Search in sources :

Example 1 with EmptyDatabaseSnapshot

use of liquibase.snapshot.EmptyDatabaseSnapshot in project liquibase by liquibase.

the class AbstractJdbcDatabase method dropDatabaseObjects.

@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() || getShortName().equals("postgresql")) {
                // We do not remove ForeignKey because they will be disabled and removed as parts of tables.
                // Postgress is treated as if we can disable foreign keys because we can't drop
                // the foreign keys of a partitioned table, as discovered in
                // https://github.com/liquibase/liquibase/issues/1212
                typesToInclude.remove(ForeignKey.class);
            }
            final long createSnapshotStarted = System.currentTimeMillis();
            snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(schemaToDrop, this, snapshotControl);
            Scope.getCurrentScope().getLog(getClass()).fine(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();
        CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(CatalogAndSchema.DEFAULT, schemaToDrop) }, snapshot.getSnapshotControl().getTypesToInclude());
        DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(new EmptyDatabaseSnapshot(this), snapshot, compareControl);
        List<ChangeSet> changeSets = new DiffToChangeLog(diffResult, new DiffOutputControl(true, true, false, null).addIncludedSchema(schemaToDrop)).generateChangeSets();
        Scope.getCurrentScope().getLog(getClass()).fine(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) {
                        Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", this).execute(statement);
                    }
                }
                this.commit();
            }
        } finally {
            if (reEnableFK) {
                enableForeignKeyChecks();
            }
        }
        ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(this).destroy();
        LockServiceFactory.getInstance().getLockService(this).destroy();
        this.setAutoCommit(previousAutoCommit);
        Scope.getCurrentScope().getLog(getClass()).info(String.format("Successfully deleted all supported object types in schema %s.", schemaToDrop.toString()));
    } finally {
        this.setObjectQuotingStrategy(currentStrategy);
        this.commit();
    }
}
Also used : DiffOutputControl(liquibase.diff.output.DiffOutputControl) DropTableChange(liquibase.change.core.DropTableChange) Change(liquibase.change.Change) SqlStatement(liquibase.statement.SqlStatement) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseObject(liquibase.structure.DatabaseObject) CompareControl(liquibase.diff.compare.CompareControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DropTableChange(liquibase.change.core.DropTableChange) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException) DiffResult(liquibase.diff.DiffResult) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet)

Example 2 with EmptyDatabaseSnapshot

use of liquibase.snapshot.EmptyDatabaseSnapshot in project liquibase by liquibase.

the class StandardDiffGenerator method compare.

@Override
public DiffResult compare(DatabaseSnapshot referenceSnapshot, DatabaseSnapshot comparisonSnapshot, CompareControl compareControl) throws DatabaseException {
    if (comparisonSnapshot == null) {
        try {
            // , compareControl.toSnapshotControl(CompareControl.DatabaseRole.REFERENCE));
            comparisonSnapshot = new EmptyDatabaseSnapshot(referenceSnapshot.getDatabase());
        } catch (InvalidExampleException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }
    DiffResult diffResult = new DiffResult(referenceSnapshot, comparisonSnapshot, compareControl);
    checkVersionInfo(referenceSnapshot, comparisonSnapshot, diffResult);
    Set<Class<? extends DatabaseObject>> typesToCompare = compareControl.getComparedTypes();
    typesToCompare.retainAll(referenceSnapshot.getSnapshotControl().getTypesToInclude());
    typesToCompare.retainAll(comparisonSnapshot.getSnapshotControl().getTypesToInclude());
    for (Class<? extends DatabaseObject> typeToCompare : typesToCompare) {
        compareObjectType(typeToCompare, referenceSnapshot, comparisonSnapshot, diffResult);
    }
    return diffResult;
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseObject(liquibase.structure.DatabaseObject) DiffResult(liquibase.diff.DiffResult) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 3 with EmptyDatabaseSnapshot

use of liquibase.snapshot.EmptyDatabaseSnapshot in project liquibase by liquibase.

the class DiffToChangeLogTest method getOrderedOutputTypes_isConsistant.

@Test
public void getOrderedOutputTypes_isConsistant() throws Exception {
    MySQLDatabase database = new MySQLDatabase();
    DiffToChangeLog obj = new DiffToChangeLog(new DiffResult(new EmptyDatabaseSnapshot(database), new EmptyDatabaseSnapshot(database), new CompareControl()), null);
    for (Class<? extends ChangeGenerator> type : new Class[] { UnexpectedObjectChangeGenerator.class, MissingObjectChangeGenerator.class, ChangedObjectChangeGenerator.class }) {
        List<Class<? extends DatabaseObject>> orderedOutputTypes = obj.getOrderedOutputTypes(type);
        for (int i = 0; i < 50; i++) {
            assertThat("Error checking " + type.getName(), orderedOutputTypes, contains(obj.getOrderedOutputTypes(type).toArray()));
        }
    }
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test)

Example 4 with EmptyDatabaseSnapshot

use of liquibase.snapshot.EmptyDatabaseSnapshot in project liquibase by liquibase.

the class DiffToChangeLogTest method getOrderedOutputTypes_hasDependencies.

@Test
public void getOrderedOutputTypes_hasDependencies() throws Exception {
    MySQLDatabase database = new MySQLDatabase();
    Class<? extends DatabaseObject>[] typesArray = new Class[5];
    typesArray[0] = Schema.class;
    typesArray[1] = View.class;
    typesArray[2] = Catalog.class;
    typesArray[3] = Table.class;
    typesArray[4] = Column.class;
    SnapshotControl control = new SnapshotControl(database, typesArray);
    EmptyDatabaseSnapshot emptyDatabaseSnapshot = new EmptyDatabaseSnapshot(database, control);
    DiffToChangeLog obj = new DiffToChangeLog(new DiffResult(emptyDatabaseSnapshot, emptyDatabaseSnapshot, new CompareControl()), null);
    for (Class<? extends ChangeGenerator> type : new Class[] { UnexpectedObjectChangeGenerator.class, MissingObjectChangeGenerator.class, ChangedObjectChangeGenerator.class }) {
        List<Class<? extends DatabaseObject>> orderedOutputTypes = obj.getOrderedOutputTypes(type);
        assertThat("There should be some types", orderedOutputTypes, hasSize(greaterThan(5)));
    }
    List<Class<? extends DatabaseObject>> unexpectedOrderedOutputTypes = obj.getOrderedOutputTypes(UnexpectedObjectChangeGenerator.class);
    assertThat("There should be some types", unexpectedOrderedOutputTypes, hasSize(7));
    List<Class<? extends DatabaseObject>> missingOrderedOutputTypes = obj.getOrderedOutputTypes(MissingObjectChangeGenerator.class);
    assertThat("There should be some types", missingOrderedOutputTypes, hasSize(6));
    List<Class<? extends DatabaseObject>> changedOrderedOutputTypes = obj.getOrderedOutputTypes(ChangedObjectChangeGenerator.class);
    assertThat("There should be some types", changedOrderedOutputTypes, hasSize(6));
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseObject(liquibase.structure.DatabaseObject) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test)

Example 5 with EmptyDatabaseSnapshot

use of liquibase.snapshot.EmptyDatabaseSnapshot in project liquibase by liquibase.

the class DiffToChangeLogTest method getOrderedOutputTypes_isConsistent.

@Test
public void getOrderedOutputTypes_isConsistent() throws Exception {
    MySQLDatabase database = new MySQLDatabase();
    DiffToChangeLog obj = new DiffToChangeLog(new DiffResult(new EmptyDatabaseSnapshot(database), new EmptyDatabaseSnapshot(database), new CompareControl()), null);
    for (Class<? extends ChangeGenerator> type : new Class[] { UnexpectedObjectChangeGenerator.class, MissingObjectChangeGenerator.class, ChangedObjectChangeGenerator.class }) {
        List<Class<? extends DatabaseObject>> orderedOutputTypes = obj.getOrderedOutputTypes(type);
        for (int i = 0; i < 50; i++) {
            assertThat("Error checking " + type.getName(), orderedOutputTypes, contains(obj.getOrderedOutputTypes(type).toArray()));
        }
    }
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test)

Aggregations

EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)6 DatabaseObject (liquibase.structure.DatabaseObject)6 DiffResult (liquibase.diff.DiffResult)5 CompareControl (liquibase.diff.compare.CompareControl)4 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)3 Test (org.junit.Test)3 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)2 SnapshotControl (liquibase.snapshot.SnapshotControl)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Scope (liquibase.Scope)1 Change (liquibase.change.Change)1 DropTableChange (liquibase.change.core.DropTableChange)1 ChangeSet (liquibase.changelog.ChangeSet)1 RanChangeSet (liquibase.changelog.RanChangeSet)1 DiffOutputControl (liquibase.diff.output.DiffOutputControl)1 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)1 DatabaseException (liquibase.exception.DatabaseException)1 LiquibaseException (liquibase.exception.LiquibaseException)1 InvalidExampleException (liquibase.snapshot.InvalidExampleException)1