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();
}
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);
}
}
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);
}
}
}
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;
}
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);
}
}
Aggregations