use of liquibase.change.Change in project liquibase by liquibase.
the class ChangeSet method rollback.
public void rollback(Database database) throws RollbackFailedException {
try {
Executor executor = ExecutorService.getInstance().getExecutor(database);
executor.comment("Rolling Back ChangeSet: " + toString());
database.setObjectQuotingStrategy(objectQuotingStrategy);
// set auto-commit based on runInTransaction if database supports DDL in transactions
if (database.supportsDDLInTransaction()) {
database.setAutoCommit(!runInTransaction);
}
RanChangeSet ranChangeSet = database.getRanChangeSet(this);
if (hasCustomRollbackChanges()) {
final List<SqlStatement> statements = new LinkedList<SqlStatement>();
for (Change change : rollback.getChanges()) {
if (((change instanceof DbmsTargetedChange)) && !DatabaseList.definitionMatches(((DbmsTargetedChange) change).getDbms(), database, true)) {
continue;
}
ValidationErrors errors = change.validate(database);
if (errors.hasErrors()) {
throw new RollbackFailedException("Rollback statement failed validation: " + errors.toString());
}
SqlStatement[] changeStatements = change.generateStatements(database);
if (changeStatements != null) {
statements.addAll(Arrays.asList(changeStatements));
}
}
if (!statements.isEmpty()) {
database.executeRollbackStatements(statements.toArray(new SqlStatement[] {}), sqlVisitors);
}
} else {
List<Change> changes = getChanges();
for (int i = changes.size() - 1; i >= 0; i--) {
Change change = changes.get(i);
database.executeRollbackStatements(change, sqlVisitors);
}
}
if (runInTransaction) {
database.commit();
}
log.debug("ChangeSet " + toString() + " has been successfully rolled back.");
} catch (Exception e) {
try {
database.rollback();
} catch (DatabaseException e1) {
//ok
}
throw new RollbackFailedException(e);
} finally {
// but only if the database supports DDL in transactions
if (!runInTransaction && database.supportsDDLInTransaction()) {
try {
database.setAutoCommit(false);
} catch (DatabaseException e) {
throw new RollbackFailedException("Could not resetInternalState autocommit", e);
}
}
}
}
use of liquibase.change.Change in project liquibase by liquibase.
the class UnexpectedForeignKeyChangeGenerator method fixUnexpected.
@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
ForeignKey fk = (ForeignKey) unexpectedObject;
DropForeignKeyConstraintChange change = new DropForeignKeyConstraintChange();
change.setConstraintName(fk.getName());
change.setBaseTableName(fk.getForeignKeyTable().getName());
if (control.getIncludeCatalog()) {
change.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
}
Index backingIndex = fk.getBackingIndex();
// if (backingIndex == null) {
// Index exampleIndex = new Index().setTable(fk.getForeignKeyTable());
// for (String col : fk.getForeignKeyColumns().split("\\s*,\\s*")) {
// exampleIndex.getColumns().add(col);
// }
// control.setAlreadyHandledUnexpected(exampleIndex);
// } else {
control.setAlreadyHandledUnexpected(backingIndex);
return new Change[] { change };
}
use of liquibase.change.Change in project liquibase by liquibase.
the class UnexpectedPrimaryKeyChangeGenerator method fixUnexpected.
@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
// if (!diffResult.getObjectDiff(Table.class).getUnexpected().contains(pk.getTable())) {
PrimaryKey pk = (PrimaryKey) unexpectedObject;
DropPrimaryKeyChange change = new DropPrimaryKeyChange();
change.setTableName(pk.getTable().getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(pk.getTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(pk.getTable().getSchema().getName());
}
change.setConstraintName(pk.getName());
Index backingIndex = pk.getBackingIndex();
control.setAlreadyHandledUnexpected(backingIndex);
return new Change[] { change };
// }
}
use of liquibase.change.Change 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 };
}
use of liquibase.change.Change in project liquibase by liquibase.
the class UnexpectedViewChangeGenerator method fixUnexpected.
@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
View view = (View) unexpectedObject;
DropViewChange change = new DropViewChange();
change.setViewName(view.getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(view.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(view.getSchema().getName());
}
for (Column column : view.getColumns()) {
control.setAlreadyHandledUnexpected(column);
}
;
return new Change[] { change };
}
Aggregations