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