Search in sources :

Example 1 with DropTableChange

use of liquibase.change.core.DropTableChange 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();
    }
}
Also used : DiffOutputControl(liquibase.diff.output.DiffOutputControl) Change(liquibase.change.Change) DropTableChange(liquibase.change.core.DropTableChange) 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) DiffResult(liquibase.diff.DiffResult) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl)

Example 2 with DropTableChange

use of liquibase.change.core.DropTableChange in project liquibase by liquibase.

the class UnexpectedTableChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Table unexpectedTable = (Table) unexpectedObject;
    DropTableChange change = new DropTableChange();
    change.setTableName(unexpectedTable.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(unexpectedTable.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(unexpectedTable.getSchema().getName());
    }
    for (Column column : unexpectedTable.getColumns()) {
        control.setAlreadyHandledUnexpected(column);
    }
    ;
    control.setAlreadyHandledUnexpected(unexpectedTable.getPrimaryKey());
    for (Index index : unexpectedTable.getIndexes()) {
        control.setAlreadyHandledUnexpected(index);
    }
    control.setAlreadyHandledUnexpected(unexpectedTable.getPrimaryKey());
    if (unexpectedTable.getPrimaryKey() != null) {
        control.setAlreadyHandledUnexpected(unexpectedTable.getPrimaryKey().getBackingIndex());
    }
    return new Change[] { change };
}
Also used : DropTableChange(liquibase.change.core.DropTableChange) Change(liquibase.change.Change) DropTableChange(liquibase.change.core.DropTableChange)

Aggregations

Change (liquibase.change.Change)2 DropTableChange (liquibase.change.core.DropTableChange)2 DiffResult (liquibase.diff.DiffResult)1 CompareControl (liquibase.diff.compare.CompareControl)1 DiffOutputControl (liquibase.diff.output.DiffOutputControl)1 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)1 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)1 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)1 SnapshotControl (liquibase.snapshot.SnapshotControl)1 SqlStatement (liquibase.statement.SqlStatement)1 DatabaseObject (liquibase.structure.DatabaseObject)1