use of liquibase.logging.core.BufferedLogService in project liquibase by liquibase.
the class InternalDropAllCommandStep method run.
@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();
BufferedLogService bufferLog = new BufferedLogService();
validateConnectionAndProjectIdsDependingOnApiKey(commandScope);
Operation dropAllOperation;
LockService lockService = LockServiceFactory.getInstance().getLockService(commandScope.getArgumentValue(DATABASE_ARG));
HubUpdater hubUpdater;
try {
lockService.waitForLock();
DatabaseChangeLog changeLog;
HubRegisterResponse hubRegisterResponse = null;
if (StringUtil.isNotEmpty(commandScope.getArgumentValue(CHANGELOG_FILE_ARG))) {
// Let the user know they can register for Hub
changeLog = parseChangeLogFile(commandScope.getArgumentValue(CHANGELOG_FILE_ARG));
hubUpdater = new HubUpdater(new Date(), changeLog, commandScope.getArgumentValue(DATABASE_ARG));
hubRegisterResponse = hubUpdater.register(commandScope.getArgumentValue(CHANGELOG_FILE_ARG));
// Access the HubChangeLog and check to see if we should run syncHub
HubChangeLog hubChangeLog = getHubChangeLog(changeLog);
checkForRegisteredChangeLog(changeLog, hubChangeLog);
} else {
hubUpdater = new HubUpdater(new Date(), commandScope.getArgumentValue(DATABASE_ARG));
hubRegisterResponse = hubUpdater.register(null);
}
Connection hubConnection = getHubConnection(commandScope);
attachProjectToConnection(commandScope, hubConnection, hubRegisterResponse);
dropAllOperation = hubUpdater.preUpdateHub("DROPALL", "drop-all", hubConnection);
try {
for (CatalogAndSchema schema : commandScope.getArgumentValue(SCHEMAS_ARG)) {
log.info("Dropping Database Objects in schema: " + schema);
checkLiquibaseTables(commandScope.getArgumentValue(DATABASE_ARG));
commandScope.getArgumentValue(DATABASE_ARG).dropDatabaseObjects(schema);
}
} catch (LiquibaseException liquibaseException) {
hubUpdater.postUpdateHubExceptionHandling(dropAllOperation, bufferLog, liquibaseException.getMessage());
return;
}
final HubServiceFactory hubServiceFactory = Scope.getCurrentScope().getSingleton(HubServiceFactory.class);
String apiKey = StringUtil.trimToNull(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue());
if (apiKey != null && hubServiceFactory.isOnline()) {
hubUpdater.syncHub(commandScope.getArgumentValue(CHANGELOG_FILE_ARG), hubConnection);
hubUpdater.postUpdateHub(dropAllOperation, bufferLog);
}
} catch (DatabaseException e) {
throw e;
} catch (Exception e) {
throw new DatabaseException(e);
} finally {
lockService.releaseLock();
lockService.destroy();
resetServices();
}
Scope.getCurrentScope().getUI().sendMessage("All objects dropped from " + commandScope.getArgumentValue(DATABASE_ARG).getConnection().getConnectionUserName() + "@" + commandScope.getArgumentValue(DATABASE_ARG).getConnection().getURL());
resultsBuilder.addResult("statusCode", 0);
}
use of liquibase.logging.core.BufferedLogService in project liquibase by liquibase.
the class Liquibase method update.
/**
* Liquibase update
*
* @param contexts
* @param labelExpression
* @param checkLiquibaseTables
* @throws LiquibaseException
*/
public void update(Contexts contexts, LabelExpression labelExpression, boolean checkLiquibaseTables) throws LiquibaseException {
runInScope(() -> {
LockService lockService = LockServiceFactory.getInstance().getLockService(database);
lockService.waitForLock();
changeLogParameters.setContexts(contexts);
changeLogParameters.setLabels(labelExpression);
Operation updateOperation = null;
BufferedLogService bufferLog = new BufferedLogService();
DatabaseChangeLog changeLog = null;
HubUpdater hubUpdater = null;
try {
changeLog = getDatabaseChangeLog();
if (checkLiquibaseTables) {
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 or retrieve the Connection if this is not SQL generation
// Make sure the Hub is available here by checking the return
// We do not need a connection if we are using a LoggingExecutor
//
ChangeLogIterator changeLogIterator = getStandardChangelogIterator(contexts, labelExpression, changeLog);
Connection connection = getConnection(changeLog);
if (connection != null) {
updateOperation = hubUpdater.preUpdateHub("UPDATE", "update", connection, changeLogFile, contexts, labelExpression, changeLogIterator);
}
//
if (connection != null) {
changeExecListener = new HubChangeExecListener(updateOperation, changeExecListener);
}
//
// Create another iterator to run
//
ChangeLogIterator runChangeLogIterator = getStandardChangelogIterator(contexts, labelExpression, changeLog);
CompositeLogService compositeLogService = new CompositeLogService(true, bufferLog);
Scope.child(Scope.Attr.logService.name(), compositeLogService, () -> {
runChangeLogIterator.run(createUpdateVisitor(), new RuntimeEnvironment(database, contexts, labelExpression));
});
//
// Update Hub with the operation information
//
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.logging.core.BufferedLogService in project liquibase by liquibase.
the class Liquibase method rollback.
/**
* Rollback to date
*
* @param dateToRollBackTo
* @param rollbackScript
* @param contexts
* @param labelExpression
* @throws LiquibaseException
*/
public void rollback(Date dateToRollBackTo, 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
//
List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
ChangeLogIterator listLogIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new ExecutedAfterChangeSetFilter(dateToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
//
// 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-to-date", connection, changeLogFile, contexts, labelExpression, listLogIterator);
}
//
if (connection != null) {
changeExecListener = new HubChangeExecListener(rollbackOperation, changeExecListener);
}
//
// Create another iterator to run
//
ChangeLogIterator logIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new ExecutedAfterChangeSetFilter(dateToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
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(MSG_COULD_NOT_RELEASE_LOCK, e);
}
resetServices();
setChangeExecListener(null);
}
}
});
}
use of liquibase.logging.core.BufferedLogService in project liquibase by liquibase.
the class Liquibase method update.
/**
* Update to count
*
* @param changesToApply
* @param contexts
* @param labelExpression
* @throws LiquibaseException
*/
public void update(int changesToApply, 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 updateOperation = 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 = new ChangeLogIterator(changeLog, new ShouldRunChangeSetFilter(database), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new DbmsChangeSetFilter(database), new IgnoreChangeSetFilter(), new CountChangeSetFilter(changesToApply));
//
// 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-count", 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 CountChangeSetFilter(changesToApply));
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.logging.core.BufferedLogService in project liquibase by liquibase.
the class Liquibase method rollback.
/**
* Rollback to tag
*
* @param tagToRollBackTo
* @param rollbackScript
* @param contexts
* @param labelExpression
* @throws LiquibaseException
*/
public void rollback(String tagToRollBackTo, 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
//
List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
ChangeLogIterator listLogIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new AfterTagChangeSetFilter(tagToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
//
// 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", connection, changeLogFile, contexts, labelExpression, listLogIterator);
}
//
if (connection != null) {
changeExecListener = new HubChangeExecListener(rollbackOperation, changeExecListener);
}
//
// Create another iterator to run
//
ChangeLogIterator logIterator = new ChangeLogIterator(ranChangeSetList, changeLog, new AfterTagChangeSetFilter(tagToRollBackTo, ranChangeSetList), new AlreadyRanChangeSetFilter(ranChangeSetList), new ContextChangeSetFilter(contexts), new LabelChangeSetFilter(labelExpression), new IgnoreChangeSetFilter(), new DbmsChangeSetFilter(database));
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(MSG_COULD_NOT_RELEASE_LOCK, e);
}
}
resetServices();
setChangeExecListener(null);
}
});
}
Aggregations