Search in sources :

Example 1 with ObjectQuotingStrategy

use of liquibase.database.ObjectQuotingStrategy 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 {
    ChangeSet.RunStatus runStatus = this.database.getRunStatus(changeSet);
    log.debug("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 : MigrationFailedException(liquibase.exception.MigrationFailedException) RunStatus(liquibase.changelog.ChangeSet.RunStatus) ExecType(liquibase.changelog.ChangeSet.ExecType) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 2 with ObjectQuotingStrategy

use of liquibase.database.ObjectQuotingStrategy in project liquibase by liquibase.

the class DiffCommand method createReferenceSnapshot.

protected DatabaseSnapshot createReferenceSnapshot() throws DatabaseException, InvalidExampleException {
    CatalogAndSchema[] schemas;
    if (compareControl == null || compareControl.getSchemaComparisons() == null) {
        schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
    } else {
        schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
        int i = 0;
        for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
            CatalogAndSchema schema;
            if (referenceDatabase.supportsSchemas()) {
                schema = new CatalogAndSchema(referenceDatabase.getDefaultCatalogName(), comparison.getReferenceSchema().getSchemaName());
            } else {
                schema = new CatalogAndSchema(comparison.getReferenceSchema().getSchemaName(), comparison.getReferenceSchema().getSchemaName());
            }
            schemas[i++] = schema;
        }
    }
    SnapshotControl snapshotControl = getReferenceSnapshotControl();
    if (snapshotControl == null) {
        snapshotControl = new SnapshotControl(referenceDatabase, snapshotTypes);
    }
    if (getSnapshotListener() != null) {
        snapshotControl.setSnapshotListener(getSnapshotListener());
    }
    ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
    try {
        referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
        return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, referenceDatabase, snapshotControl);
    } finally {
        referenceDatabase.setObjectQuotingStrategy(originalStrategy);
    }
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) CatalogAndSchema(liquibase.CatalogAndSchema) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 3 with ObjectQuotingStrategy

use of liquibase.database.ObjectQuotingStrategy in project liquibase by liquibase.

the class StandardLockService method releaseLock.

@Override
public void releaseLock() throws LockException {
    ObjectQuotingStrategy incomingQuotingStrategy = null;
    if (this.quotingStrategy != null) {
        incomingQuotingStrategy = database.getObjectQuotingStrategy();
        database.setObjectQuotingStrategy(this.quotingStrategy);
    }
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    try {
        if (this.hasDatabaseChangeLogLockTable()) {
            executor.comment("Release Database Lock");
            database.rollback();
            int updatedRows = executor.update(new UnlockDatabaseChangeLogStatement());
            if (updatedRows == -1 && database instanceof MSSQLDatabase) {
                LogFactory.getLogger().debug("Database did not return a proper row count (Might have NOCOUNT enabled.)");
                database.rollback();
                Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(new UnlockDatabaseChangeLogStatement(), database);
                if (sql.length != 1) {
                    throw new UnexpectedLiquibaseException("Did not expect " + sql.length + " statements");
                }
                updatedRows = executor.update(new RawSqlStatement("EXEC sp_executesql N'SET NOCOUNT OFF " + sql[0].toSql().replace("'", "''") + "'"));
            }
            if (updatedRows != 1) {
                throw new LockException("Did not update change log lock correctly.\n\n" + updatedRows + " rows were updated instead of the expected 1 row using executor " + executor.getClass().getName() + " there are " + executor.queryForInt(new RawSqlStatement("select count(*) from " + database.getDatabaseChangeLogLockTableName())) + " rows in the table");
            }
            database.commit();
        }
    } catch (Exception e) {
        throw new LockException(e);
    } finally {
        try {
            hasChangeLogLock = false;
            database.setCanCacheLiquibaseTableInfo(false);
            LogFactory.getLogger().info("Successfully released change log lock");
            database.rollback();
        } catch (DatabaseException e) {
            ;
        }
        if (incomingQuotingStrategy != null) {
            database.setObjectQuotingStrategy(incomingQuotingStrategy);
        }
    }
}
Also used : Executor(liquibase.executor.Executor) LockException(liquibase.exception.LockException) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) LockException(liquibase.exception.LockException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) Sql(liquibase.sql.Sql)

Example 4 with ObjectQuotingStrategy

use of liquibase.database.ObjectQuotingStrategy in project liquibase by liquibase.

the class DiffToChangeLog method generateChangeSets.

public List<ChangeSet> generateChangeSets() {
    final ChangeGeneratorFactory changeGeneratorFactory = ChangeGeneratorFactory.getInstance();
    DatabaseObjectComparator comparator = new DatabaseObjectComparator();
    String created = null;
    if (LiquibaseConfiguration.getInstance().getProperty(GlobalConfiguration.class, GlobalConfiguration.GENERATE_CHANGESET_CREATED_VALUES).getValue(Boolean.class)) {
        created = new SimpleDateFormat("yyyy-MM-dd HH:mmZ").format(new Date());
    }
    List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
    List<Class<? extends DatabaseObject>> types = getOrderedOutputTypes(MissingObjectChangeGenerator.class);
    List<DatabaseObject> missingObjects = new ArrayList<DatabaseObject>();
    for (Class<? extends DatabaseObject> type : types) {
        for (DatabaseObject object : diffResult.getMissingObjects(type, new DatabaseObjectComparator() {

            @Override
            public int compare(DatabaseObject o1, DatabaseObject o2) {
                if (o1 instanceof Column && o1.getAttribute("order", Integer.class) != null && o2.getAttribute("order", Integer.class) != null) {
                    int i = o1.getAttribute("order", Integer.class).compareTo(o2.getAttribute("order", Integer.class));
                    if (i != 0) {
                        return i;
                    }
                }
                return super.compare(o1, o2);
            }
        })) {
            if (object == null) {
                continue;
            }
            if (!diffResult.getReferenceSnapshot().getDatabase().isLiquibaseObject(object) && !diffResult.getReferenceSnapshot().getDatabase().isSystemObject(object)) {
                missingObjects.add(object);
            }
        }
    }
    for (DatabaseObject object : sortMissingObjects(missingObjects, diffResult.getReferenceSnapshot().getDatabase())) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        Change[] changes = changeGeneratorFactory.fixMissing(object, diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
        addToChangeSets(changes, changeSets, quotingStrategy, created);
    }
    types = getOrderedOutputTypes(UnexpectedObjectChangeGenerator.class);
    for (Class<? extends DatabaseObject> type : types) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        for (DatabaseObject object : sortUnexpectedObjects(diffResult.getUnexpectedObjects(type, comparator), diffResult.getReferenceSnapshot().getDatabase())) {
            if (!diffResult.getComparisonSnapshot().getDatabase().isLiquibaseObject(object) && !diffResult.getComparisonSnapshot().getDatabase().isSystemObject(object)) {
                Change[] changes = changeGeneratorFactory.fixUnexpected(object, diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
                addToChangeSets(changes, changeSets, quotingStrategy, created);
            }
        }
    }
    types = getOrderedOutputTypes(ChangedObjectChangeGenerator.class);
    for (Class<? extends DatabaseObject> type : types) {
        ObjectQuotingStrategy quotingStrategy = diffOutputControl.getObjectQuotingStrategy();
        for (Map.Entry<? extends DatabaseObject, ObjectDifferences> entry : diffResult.getChangedObjects(type, comparator).entrySet()) {
            if (!diffResult.getReferenceSnapshot().getDatabase().isLiquibaseObject(entry.getKey()) && !diffResult.getReferenceSnapshot().getDatabase().isSystemObject(entry.getKey())) {
                Change[] changes = changeGeneratorFactory.fixChanged(entry.getKey(), entry.getValue(), diffOutputControl, diffResult.getReferenceSnapshot().getDatabase(), diffResult.getComparisonSnapshot().getDatabase());
                addToChangeSets(changes, changeSets, quotingStrategy, created);
            }
        }
    }
    return changeSets;
}
Also used : Column(liquibase.structure.core.Column) DatabaseObjectComparator(liquibase.structure.DatabaseObjectComparator) DatabaseObject(liquibase.structure.DatabaseObject) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) GlobalConfiguration(liquibase.configuration.GlobalConfiguration) Change(liquibase.change.Change) ObjectDifferences(liquibase.diff.ObjectDifferences) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with ObjectQuotingStrategy

use of liquibase.database.ObjectQuotingStrategy in project liquibase by liquibase.

the class ChangeSetExecutedPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    ObjectQuotingStrategy objectQuotingStrategy = null;
    if (changeSet == null) {
        objectQuotingStrategy = ObjectQuotingStrategy.LEGACY;
    } else {
        objectQuotingStrategy = changeSet.getObjectQuotingStrategy();
    }
    String changeLogFile = getChangeLogFile();
    if (changeLogFile == null) {
        changeLogFile = changeLog.getLogicalFilePath();
    }
    ChangeSet interestedChangeSet = new ChangeSet(getId(), getAuthor(), false, false, changeLogFile, null, null, false, objectQuotingStrategy, changeLog);
    RanChangeSet ranChangeSet;
    try {
        ranChangeSet = database.getRanChangeSet(interestedChangeSet);
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
    if (ranChangeSet == null || ranChangeSet.getExecType() == null || !ranChangeSet.getExecType().ran) {
        throw new PreconditionFailedException("Change Set '" + interestedChangeSet.toString(false) + "' has not been run", changeLog, this);
    }
}
Also used : PreconditionFailedException(liquibase.exception.PreconditionFailedException) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) RanChangeSet(liquibase.changelog.RanChangeSet) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Aggregations

ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)8 CatalogAndSchema (liquibase.CatalogAndSchema)4 ChangeSet (liquibase.changelog.ChangeSet)3 CompareControl (liquibase.diff.compare.CompareControl)2 DatabaseException (liquibase.exception.DatabaseException)2 InvalidExampleException (liquibase.snapshot.InvalidExampleException)2 SQLException (java.sql.SQLException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Change (liquibase.change.Change)1 ExecType (liquibase.changelog.ChangeSet.ExecType)1 RunStatus (liquibase.changelog.ChangeSet.RunStatus)1 RanChangeSet (liquibase.changelog.RanChangeSet)1 GlobalConfiguration (liquibase.configuration.GlobalConfiguration)1 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)1 Database (liquibase.database.Database)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 ObjectDifferences (liquibase.diff.ObjectDifferences)1 LiquibaseException (liquibase.exception.LiquibaseException)1 LockException (liquibase.exception.LockException)1 MigrationFailedException (liquibase.exception.MigrationFailedException)1