use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.
the class Liquibase method rollback.
/**
* Rollback count
*
* @param changesToRollback
* @param rollbackScript
* @param contexts
* @param labelExpression
* @throws LiquibaseException
*/
public void rollback(int changesToRollback, String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
changeLogParameters.setContexts(contexts);
changeLogParameters.setLabels(labelExpression);
runInScope(new Scope.ScopedRunner() {
@Override
public void run() throws Exception {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
Operation rollbackOperation = null;
BufferedLogService bufferLog = new BufferedLogService();
DatabaseChangeLog changeLog = null;
Date startTime = new Date();
HubUpdater hubUpdater = null;
try {
changeLog = getDatabaseChangeLog();
checkLiquibaseTables(false, changeLog, contexts, labelExpression);
changeLog.validate(database, contexts, labelExpression);
//
// Let the user know that they can register for Hub
//
hubUpdater = new HubUpdater(startTime, changeLog, database);
hubUpdater.register(changeLogFile);
//
// Create an iterator which will be used with a ListVisitor
// to grab the list of change sets for the update
//
ChangeLogIterator listLogIterator = new ChangeLogIterator(database.getRanChangeSetList(), changeLog, new AlreadyRanChangeSetFilter(database.getRanChangeSetList()), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new CountChangeSetFilter(changesToRollback));
//
// Create or retrieve the Connection
// Make sure the Hub is available here by checking the return
//
Connection connection = getConnection(changeLog);
if (connection != null) {
rollbackOperation = hubUpdater.preUpdateHub("ROLLBACK", "rollback-count", connection, changeLogFile, contexts, labelExpression, listLogIterator);
}
//
if (connection != null) {
changeExecListener = new HubChangeExecListener(rollbackOperation, changeExecListener);
}
//
// Create another iterator to run
//
ChangeLogIterator logIterator = new ChangeLogIterator(database.getRanChangeSetList(), changeLog, new AlreadyRanChangeSetFilter(database.getRanChangeSetList()), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new CountChangeSetFilter(changesToRollback));
CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
if (rollbackScript == null) {
Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
logIterator.run(createRollbackVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
});
} else {
List<ChangeSet> changeSets = determineRollbacks(logIterator, contexts, labelExpression);
Map<String, Object> values = new HashMap<>();
values.put(Scope.Attr.logService.name(), compositeLogService);
values.put(BufferedLogService.class.getName(), bufferLog);
Scope.child(values, () -> {
executeRollbackScript(rollbackScript, changeSets, contexts, labelExpression);
});
removeRunStatus(changeSets, contexts, labelExpression);
}
hubUpdater.postUpdateHub(rollbackOperation, bufferLog);
} catch (Throwable t) {
if (hubUpdater != null) {
hubUpdater.postUpdateHubExceptionHandling(rollbackOperation, bufferLog, t.getMessage());
}
throw t;
} finally {
try {
lockService.releaseLock();
} catch (LockException e) {
LOG.severe("Error releasing lock", e);
}
resetServices();
setChangeExecListener(null);
}
}
});
}
use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.
the class Liquibase method update.
/**
* Update to tag
*
* @param tag Tag to update for
* @param contexts
* @param labelExpression
* @throws LiquibaseException
*/
public void update(String tag, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
if (tag == null) {
update(contexts, labelExpression);
return;
}
changeLogParameters.setContexts(contexts);
changeLogParameters.setLabels(labelExpression);
runInScope(new Scope.ScopedRunner() {
@Override
public void run() throws Exception {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
HubUpdater hubUpdater = null;
Operation updateOperation = null;
BufferedLogService bufferLog = new BufferedLogService();
DatabaseChangeLog changeLog = null;
try {
changeLog = getDatabaseChangeLog();
checkLiquibaseTables(true, changeLog, contexts, labelExpression);
ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
changeLog.validate(database, contexts, labelExpression);
//
// Let the user know that they can register for Hub
//
hubUpdater = new HubUpdater(new Date(), changeLog, database);
hubUpdater.register(changeLogFile);
//
// Create an iterator which will be used with a ListVisitor
// to grab the list of change sets for the update
//
List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
ChangeLogIterator listLogIterator = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new UpToTagChangeSetFilter(tag, ranChangeSetList));
//
// Create or retrieve the Connection
// Make sure the Hub is available here by checking the return
//
Connection connection = getConnection(changeLog);
if (connection != null) {
updateOperation = hubUpdater.preUpdateHub("UPDATE", "update-to-tag", connection, changeLogFile, contexts, labelExpression, listLogIterator);
}
//
if (connection != null) {
changeExecListener = new HubChangeExecListener(updateOperation, changeExecListener);
}
//
// Create another iterator to run
//
ChangeLogIterator runChangeLogIterator = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new UpToTagChangeSetFilter(tag, ranChangeSetList));
CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
runChangeLogIterator.run(createUpdateVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
});
hubUpdater.postUpdateHub(updateOperation, bufferLog);
} catch (Throwable e) {
if (hubUpdater != null) {
hubUpdater.postUpdateHubExceptionHandling(updateOperation, bufferLog, e.getMessage());
}
throw e;
} finally {
database.setObjectQuotingStrategy(ObjectQuotingStrategy.LEGACY);
try {
lockService.releaseLock();
} catch (LockException e) {
LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
}
resetServices();
setChangeExecListener(null);
}
}
});
}
use of liquibase.hub.listener.HubChangeExecListener in project liquibase by liquibase.
the class Liquibase method changeLogSync.
/**
* Changelogsync or changelogsync to tag
*
* @param tag
* @param contexts
* @param labelExpression
* @throws LiquibaseException
*/
public void changeLogSync(String tag, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
changeLogParameters.setContexts(contexts);
changeLogParameters.setLabels(labelExpression);
runInScope(new Scope.ScopedRunner() {
@Override
public void run() throws Exception {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
Operation changeLogSyncOperation = null;
BufferedLogService bufferLog = new BufferedLogService();
DatabaseChangeLog changeLog = null;
HubUpdater hubUpdater = null;
try {
changeLog = getDatabaseChangeLog();
checkLiquibaseTables(true, changeLog, contexts, labelExpression);
ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).generateDeploymentId();
changeLog.validate(database, contexts, labelExpression);
//
// Let the user know that they can register for Hub
//
hubUpdater = new HubUpdater(new Date(), changeLog, database);
hubUpdater.register(changeLogFile);
//
// Create an iterator which will be used with a ListVisitor
// to grab the list of change sets for the update
//
ChangeLogIterator listLogIterator = buildChangeLogIterator(tag, changeLog, contexts, labelExpression);
//
// Create or retrieve the Connection
// Make sure the Hub is available here by checking the return
//
Connection connection = getConnection(changeLog);
if (connection != null) {
String operationCommand = (tag == null ? "changelog-sync" : "changelog-sync-to-tag");
changeLogSyncOperation = hubUpdater.preUpdateHub("CHANGELOGSYNC", operationCommand, connection, changeLogFile, contexts, labelExpression, listLogIterator);
}
//
if (connection != null) {
changeLogSyncListener = new HubChangeExecListener(changeLogSyncOperation, changeExecListener);
}
ChangeLogIterator runChangeLogIterator = buildChangeLogIterator(tag, changeLog, contexts, labelExpression);
CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
runChangeLogIterator.run(new ChangeLogSyncVisitor(database, changeLogSyncListener), new RuntimeEnvironment(database, contexts, labelExpression));
});
hubUpdater.postUpdateHub(changeLogSyncOperation, bufferLog);
} catch (Exception e) {
if (changeLogSyncOperation != null) {
hubUpdater.postUpdateHubExceptionHandling(changeLogSyncOperation, bufferLog, e.getMessage());
}
throw e;
} finally {
try {
lockService.releaseLock();
} catch (LockException e) {
LOG.severe(MSG_COULD_NOT_RELEASE_LOCK, e);
}
resetServices();
setChangeExecListener(null);
}
}
});
}
Aggregations