Search in sources :

Example 16 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class RemoveChangeSetRanStatusGenerator method generateSql.

@Override
public Sql[] generateSql(RemoveChangeSetRanStatusStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    ChangeSet changeSet = statement.getChangeSet();
    ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
    try {
        return SqlGeneratorFactory.getInstance().generateSql(new DeleteStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).setWhere(database.escapeObjectName("ID", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", LiquibaseColumn.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getStoredFilePath()), database);
    } finally {
        database.setObjectQuotingStrategy(currentStrategy);
    }
}
Also used : LiquibaseColumn(liquibase.changelog.column.LiquibaseColumn) ChangeSet(liquibase.changelog.ChangeSet) DeleteStatement(liquibase.statement.core.DeleteStatement) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 17 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class UpdateChangeSetChecksumGenerator method generateSql.

@Override
public Sql[] generateSql(UpdateChangeSetChecksumStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    ChangeSet changeSet = statement.getChangeSet();
    ObjectQuotingStrategy currentStrategy = database.getObjectQuotingStrategy();
    database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
    try {
        SqlStatement runStatement = null;
        runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).setWhereClause(database.escapeObjectName("ID", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", LiquibaseColumn.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", LiquibaseColumn.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
        return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
    } finally {
        database.setObjectQuotingStrategy(currentStrategy);
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) LiquibaseColumn(liquibase.changelog.column.LiquibaseColumn) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 18 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class UpdateVisitor method visit.

@Override
public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
    Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    if (!(executor instanceof LoggingExecutor)) {
        Scope.getCurrentScope().getUI().sendMessage("Running Changeset: " + changeSet);
    }
    ChangeSet.RunStatus runStatus = this.database.getRunStatus(changeSet);
    Scope.getCurrentScope().getLog(getClass()).fine("Running Changeset: " + changeSet);
    fireWillRun(changeSet, databaseChangeLog, database, runStatus);
    ExecType execType = null;
    ObjectQuotingStrategy previousStr = this.database.getObjectQuotingStrategy();
    try {
        execType = changeSet.execute(databaseChangeLog, execListener, this.database);
    } catch (MigrationFailedException e) {
        fireRunFailed(changeSet, databaseChangeLog, database, e);
        throw e;
    }
    if (!runStatus.equals(ChangeSet.RunStatus.NOT_RAN)) {
        execType = ChangeSet.ExecType.RERAN;
    }
    fireRan(changeSet, databaseChangeLog, database, execType);
    // reset object quoting strategy after running changeset
    this.database.setObjectQuotingStrategy(previousStr);
    this.database.markChangeSetExecStatus(changeSet, execType);
    this.database.commit();
}
Also used : LoggingExecutor(liquibase.executor.LoggingExecutor) Executor(liquibase.executor.Executor) MigrationFailedException(liquibase.exception.MigrationFailedException) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) RunStatus(liquibase.changelog.ChangeSet.RunStatus) ExecType(liquibase.changelog.ChangeSet.ExecType) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 19 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class DBDocVisitor method visit.

@Override
public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
    ChangeSet.RunStatus runStatus = this.database.getRunStatus(changeSet);
    if (rootChangeLogName == null) {
        rootChangeLogName = changeSet.getFilePath();
    }
    if (rootChangeLog == null) {
        this.rootChangeLog = databaseChangeLog;
    }
    if (!changesByAuthor.containsKey(changeSet.getAuthor())) {
        changesByAuthor.put(changeSet.getAuthor(), new ArrayList<Change>());
    }
    if (!changesToRunByAuthor.containsKey(changeSet.getAuthor())) {
        changesToRunByAuthor.put(changeSet.getAuthor(), new ArrayList<Change>());
    }
    boolean toRun = runStatus.equals(ChangeSet.RunStatus.NOT_RAN) || runStatus.equals(ChangeSet.RunStatus.RUN_AGAIN);
    for (Change change : changeSet.getChanges()) {
        if (toRun) {
            changesToRunByAuthor.get(changeSet.getAuthor()).add(change);
            changesToRun.add(change);
        } else {
            changesByAuthor.get(changeSet.getAuthor()).add(change);
            recentChanges.add(0, change);
        }
    }
    ChangeLogInfo changeLogInfo = new ChangeLogInfo(changeSet.getChangeLog().getLogicalFilePath(), changeSet.getChangeLog().getPhysicalFilePath());
    if (!changeLogs.contains(changeLogInfo)) {
        changeLogs.add(changeLogInfo);
    }
    for (Change change : changeSet.getChanges()) {
        Set<DatabaseObject> affectedDatabaseObjects = change.getAffectedDatabaseObjects(database);
        if (affectedDatabaseObjects != null) {
            for (DatabaseObject dbObject : affectedDatabaseObjects) {
                if (toRun) {
                    if (!changesToRunByObject.containsKey(dbObject)) {
                        changesToRunByObject.put(dbObject, new ArrayList<Change>());
                    }
                    changesToRunByObject.get(dbObject).add(change);
                } else {
                    if (!changesByObject.containsKey(dbObject)) {
                        changesByObject.put(dbObject, new ArrayList<Change>());
                    }
                    changesByObject.get(dbObject).add(change);
                }
            }
        }
    }
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet)

Example 20 with ChangeSet

use of liquibase.changelog.ChangeSet 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)

Aggregations

ChangeSet (liquibase.changelog.ChangeSet)72 Test (org.junit.Test)41 Contexts (liquibase.Contexts)12 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)11 RanChangeSet (liquibase.changelog.RanChangeSet)10 Liquibase (liquibase.Liquibase)9 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 LiquibaseException (liquibase.exception.LiquibaseException)8 ArrayList (java.util.ArrayList)7 DiffOutputControl (liquibase.diff.output.DiffOutputControl)7 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)7 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)6 DiffResult (liquibase.diff.DiffResult)6 CompareControl (liquibase.diff.compare.CompareControl)6 IOException (java.io.IOException)5 Sql (liquibase.sql.Sql)5 MarkChangeSetRanStatement (liquibase.statement.core.MarkChangeSetRanStatement)5 Date (java.util.Date)4 Labels (liquibase.Labels)4