Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rollback-to-date report (DAT-15660) #5041

Merged
merged 6 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ public String toString() {
@Setter
private String errorMsg;

@Getter
@Setter
private ExecType rollbackExecType;

public boolean shouldAlwaysRun() {
return alwaysRun;
}
Expand Down Expand Up @@ -926,12 +930,15 @@ public void rollback(Database database, ChangeExecListener listener) throws Roll
if (runInTransaction) {
database.commit();
}
rollbackExecType = ExecType.EXECUTED;
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OUTCOME, ExecType.EXECUTED.value.toLowerCase());
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OPERATION_STOP_TIME, Instant.ofEpochMilli(new Date().getTime()).toString());
Scope.getCurrentScope().getLog(getClass()).fine("ChangeSet " + toString() + " has been successfully rolled back.");
} catch (Exception e) {
setErrorMsg(e.getMessage());
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OPERATION_STOP_TIME, Instant.ofEpochMilli(new Date().getTime()).toString());
try {
rollbackExecType = ExecType.FAILED;
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OUTCOME, ExecType.FAILED.value.toLowerCase());
Scope.getCurrentScope().getLog(getClass()).fine("ChangeSet " + this + " rollback failed.");
database.rollback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import liquibase.exception.PreconditionErrorException;
import liquibase.exception.PreconditionFailedException;
import liquibase.precondition.core.PreconditionContainer;
import lombok.Getter;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -20,7 +21,11 @@
public class DefaultChangeExecListener implements ChangeExecListener, ChangeLogSyncListener {
private final List<ChangeExecListener> listeners;
private final List<ChangeSet> deployedChangeSets = new LinkedList<>();
@Getter
private final List<ChangeSet> rolledBackChangeSets = new LinkedList<>();
private final List<ChangeSet> failedChangeSets = new LinkedList<>();
@Getter
private final List<ChangeSet> failedRollbackChangeSets = new LinkedList<>();
private final Map<ChangeSet, List<Change>> deployedChangesPerChangeSet = new ConcurrentHashMap<>();

public DefaultChangeExecListener(ChangeExecListener... listeners) {
Expand All @@ -47,6 +52,7 @@ public void willRollback(ChangeSet changeSet, DatabaseChangeLog databaseChangeLo

@Override
public void rolledBack(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database) {
rolledBackChangeSets.add(changeSet);
listeners.forEach(listener -> listener.rolledBack(changeSet, databaseChangeLog, database));
}

Expand Down Expand Up @@ -79,6 +85,7 @@ public void runFailed(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog,

@Override
public void rollbackFailed(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Exception exception) {
failedRollbackChangeSets.add(changeSet);
listeners.forEach(listener -> listener.rollbackFailed(changeSet, databaseChangeLog, database, exception));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RollbackVisitor implements ChangeSetVisitor {
private final Database database;

private ChangeExecListener execListener;
private List<ChangesetsRolledback.ChangeSet> processedChangesets = new ArrayList<>();
private List<ChangeSet> processedChangesets = new ArrayList<>();

/**
* @deprecated - please use the constructor with ChangeExecListener, which can be null.
Expand All @@ -39,7 +39,7 @@ public RollbackVisitor(Database database, ChangeExecListener listener) {
this.execListener = listener;
}

public RollbackVisitor(Database database, ChangeExecListener listener, List<ChangesetsRolledback.ChangeSet> processedChangesets) {
public RollbackVisitor(Database database, ChangeExecListener listener, List<ChangeSet> processedChangesets) {
this(database);
this.execListener = listener;
this.processedChangesets = processedChangesets;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Data
this.database.commit();
checkForEmptyRollbackFile(changeSet);
if (processedChangesets != null) {
processedChangesets.add(new ChangesetsRolledback.ChangeSet(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath(), changeSet.getDeploymentId()));
processedChangesets.add(changeSet);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import liquibase.changelog.filter.*;
import liquibase.changelog.visitor.ChangeExecListener;
import liquibase.changelog.visitor.ChangeSetVisitor;
import liquibase.changelog.visitor.DefaultChangeExecListener;
import liquibase.changelog.visitor.RollbackVisitor;
import liquibase.command.*;
import liquibase.command.core.helpers.DatabaseChangelogCommandStep;
Expand All @@ -20,6 +21,7 @@
import liquibase.logging.mdc.MdcObject;
import liquibase.logging.mdc.MdcValue;
import liquibase.logging.mdc.customobjects.ChangesetsRolledback;
import liquibase.report.RollbackReportParameters;
import liquibase.resource.Resource;
import liquibase.util.StreamUtil;
import liquibase.util.StringUtil;
Expand Down Expand Up @@ -50,8 +52,16 @@ private enum RollbackMessageType {

protected void doRollback(CommandResultsBuilder resultsBuilder, List<RanChangeSet> ranChangeSetList,
ChangeSetFilter changeSetFilter) throws Exception {
doRollback(resultsBuilder, ranChangeSetList, changeSetFilter, null);
}

protected void doRollback(CommandResultsBuilder resultsBuilder, List<RanChangeSet> ranChangeSetList,
ChangeSetFilter changeSetFilter, RollbackReportParameters rollbackReportParameters) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();
String changelogFile = commandScope.getArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_FILE_ARG);
if (rollbackReportParameters != null) {
rollbackReportParameters.setChangelogArgValue(changelogFile);
}
String rollbackScript = commandScope.getArgumentValue(ROLLBACK_SCRIPT_ARG);
Scope.getCurrentScope().addMdcValue(MdcKey.ROLLBACK_SCRIPT, rollbackScript);

Expand All @@ -70,39 +80,62 @@ protected void doRollback(CommandResultsBuilder resultsBuilder, List<RanChangeSe
changeSetFilter);

doRollback(database, changelogFile, rollbackScript, logIterator, changeLogParameters, databaseChangeLog,
(ChangeExecListener) commandScope.getDependency(ChangeExecListener.class));
(ChangeExecListener) commandScope.getDependency(ChangeExecListener.class), rollbackReportParameters);
}
catch (Throwable t) {
handleRollbackException(defineCommandNames()[0][0]);
handleRollbackException(defineCommandNames()[0][0], rollbackReportParameters);
throw t;
} finally {
Scope.getCurrentScope().getMdcManager().remove(MdcKey.CHANGESETS_ROLLED_BACK);
}
}


/**
* Actually perform the rollback operation. Determining which changesets to roll back is the responsibility of the
* logIterator.
*/
public static void doRollback(Database database, String changelogFile, String rollbackScript, ChangeLogIterator logIterator,ChangeLogParameters changeLogParameters,
public static void doRollback(Database database, String changelogFile, String rollbackScript, ChangeLogIterator logIterator, ChangeLogParameters changeLogParameters,
DatabaseChangeLog databaseChangeLog, ChangeExecListener changeExecListener) throws Exception {
if (rollbackScript == null) {
List<ChangesetsRolledback.ChangeSet> processedChangesets = new ArrayList<>();
logIterator.run(new RollbackVisitor(database, changeExecListener, processedChangesets), new RuntimeEnvironment(database, changeLogParameters.getContexts(), changeLogParameters.getLabels()));
addChangelogFileToMdc(changelogFile, databaseChangeLog);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESETS_ROLLED_BACK, new ChangesetsRolledback(processedChangesets), false);
if (processedChangesets.isEmpty()) {
Scope.getCurrentScope().getUI().sendMessage("INFO: 0 changesets rolled back.");
doRollback(database, changelogFile, rollbackScript, logIterator, changeLogParameters, databaseChangeLog, changeExecListener, null);
}

/**
* Actually perform the rollback operation. Determining which changesets to roll back is the responsibility of the
* logIterator.
*/
public static void doRollback(Database database, String changelogFile, String rollbackScript, ChangeLogIterator logIterator, ChangeLogParameters changeLogParameters,
DatabaseChangeLog databaseChangeLog, ChangeExecListener changeExecListener, RollbackReportParameters rollbackReportParameters) throws Exception {
try {
if (rollbackScript == null) {
List<ChangeSet> processedChangesets = new ArrayList<>();
logIterator.run(new RollbackVisitor(database, changeExecListener, processedChangesets), new RuntimeEnvironment(database, changeLogParameters.getContexts(), changeLogParameters.getLabels()));
addChangelogFileToMdc(changelogFile, databaseChangeLog);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESETS_ROLLED_BACK, ChangesetsRolledback.fromChangesetList(processedChangesets), false);
if (processedChangesets.isEmpty()) {
Scope.getCurrentScope().getUI().sendMessage("INFO: 0 changesets rolled back.");
}
} else {
List<ChangeSet> changeSets = determineRollbacks(database, logIterator, changeLogParameters);
executeRollbackScript(database, rollbackScript, changeSets, databaseChangeLog, changeLogParameters, changeExecListener);
removeRunStatus(changeSets, database);
addChangelogFileToMdc(changelogFile, databaseChangeLog);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESETS_ROLLED_BACK, ChangesetsRolledback.fromChangesetList(changeSets));
}
try (MdcObject deploymentOutcomeMdc = Scope.getCurrentScope().getMdcManager().put(MdcKey.DEPLOYMENT_OUTCOME, MdcValue.COMMAND_SUCCESSFUL)) {
Scope.getCurrentScope().getLog(AbstractRollbackCommandStep.class).info("Rollback command completed successfully.");
if (rollbackReportParameters != null) {
rollbackReportParameters.getOperationInfo().setOperationOutcome(MdcValue.COMMAND_SUCCESSFUL);
}
}
} finally {
if (rollbackReportParameters != null && changeExecListener instanceof DefaultChangeExecListener) {
List<ChangeSet> failedChangeSets = ((DefaultChangeExecListener) changeExecListener).getFailedRollbackChangeSets();
List<ChangeSet> rolledBackChangeSets = ((DefaultChangeExecListener) changeExecListener).getRolledBackChangeSets();
rollbackReportParameters.getChangesetInfo().setChangesetCount(failedChangeSets.size() + rolledBackChangeSets.size());
rollbackReportParameters.getChangesetInfo().addAllToChangesetInfoList(rolledBackChangeSets, true);
rollbackReportParameters.getChangesetInfo().addAllToChangesetInfoList(failedChangeSets, true);
}
} else {
List<ChangeSet> changeSets = determineRollbacks(database, logIterator, changeLogParameters);
executeRollbackScript(database, rollbackScript, changeSets, databaseChangeLog, changeLogParameters, changeExecListener);
removeRunStatus(changeSets, database);
addChangelogFileToMdc(changelogFile, databaseChangeLog);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESETS_ROLLED_BACK, ChangesetsRolledback.fromChangesetList(changeSets));
}
try (MdcObject deploymentOutcomeMdc = Scope.getCurrentScope().getMdcManager().put(MdcKey.DEPLOYMENT_OUTCOME, MdcValue.COMMAND_SUCCESSFUL)) {
Scope.getCurrentScope().getLog(AbstractRollbackCommandStep.class).info("Rollback command completed successfully.");
}
}

Expand Down Expand Up @@ -205,9 +238,10 @@ private static void removeRunStatus(List<ChangeSet> changeSets, Database databas
}
}

private static void handleRollbackException(String operationName) {
private static void handleRollbackException(String operationName, RollbackReportParameters rollbackReportParameters) {
try (MdcObject deploymentOutcomeMdc = Scope.getCurrentScope().addMdcValue(MdcKey.DEPLOYMENT_OUTCOME, MdcValue.COMMAND_FAILED)) {
Scope.getCurrentScope().getLog(AbstractRollbackCommandStep.class).info(operationName + " command encountered an exception.");
rollbackReportParameters.getOperationInfo().setOperationOutcome(MdcValue.COMMAND_FAILED);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ private void logDeploymentOutcomeMdc(ChangeExecListener defaultListener, boolean
int failedChangeSetCount = failedChangeSets.size();
ChangesetsUpdated changesetsUpdated = new ChangesetsUpdated(deployedChangeSets);
updateReportParameters.getChangesetInfo().setChangesetCount(deployedChangeSetCount + failedChangeSetCount);
updateReportParameters.getChangesetInfo().addAllToChangesetInfoList(deployedChangeSets);
updateReportParameters.getChangesetInfo().addAllToChangesetInfoList(failedChangeSets);
updateReportParameters.getChangesetInfo().addAllToChangesetInfoList(deployedChangeSets, false);
updateReportParameters.getChangesetInfo().addAllToChangesetInfoList(failedChangeSets, false);
Scope.getCurrentScope().addMdcValue(MdcKey.DEPLOYMENT_OUTCOME_COUNT, String.valueOf(deployedChangeSetCount));
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESETS_UPDATED, changesetsUpdated);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import liquibase.command.*;
import liquibase.database.Database;
import liquibase.logging.mdc.MdcKey;
import liquibase.report.RollbackReportParameters;
import liquibase.util.StringUtil;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -34,19 +38,30 @@ public class RollbackCommandStep extends AbstractRollbackCommandStep {
builder.addArgument(AbstractRollbackCommandStep.ROLLBACK_SCRIPT_ARG).build();
}


@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
RollbackReportParameters rollbackReportParameters = new RollbackReportParameters();
rollbackReportParameters.setCommandTitle(
StringUtil.upperCaseFirst(Arrays.toString(
defineCommandNames()[0]).replace("[","").replace("]","").replace("rollback", "rollback ").trim()));
resultsBuilder.addResult("rollbackReport", rollbackReportParameters);
Scope.child(Collections.singletonMap("rollbackReport", rollbackReportParameters), () -> doRun(resultsBuilder, rollbackReportParameters));
}

private void doRun(CommandResultsBuilder resultsBuilder, RollbackReportParameters rollbackReportParameters) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();

String tagToRollBackTo = commandScope.getArgumentValue(TAG_ARG);
Scope.getCurrentScope().addMdcValue(MdcKey.ROLLBACK_TO_TAG, tagToRollBackTo);

Database database = (Database) commandScope.getDependency(Database.class);
rollbackReportParameters.getDatabaseInfo().setDatabaseType(database.getDatabaseProductName());
rollbackReportParameters.getDatabaseInfo().setVersion(database.getDatabaseProductVersion());
rollbackReportParameters.setJdbcUrl(database.getConnection().getURL());

List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
TagVersionEnum tagVersion = TagVersionEnum.valueOf(commandScope.getArgumentValue(TAG_VERSION_ARG));
this.doRollback(resultsBuilder, ranChangeSetList, new AfterTagChangeSetFilter(tagToRollBackTo, ranChangeSetList, tagVersion));
this.doRollback(resultsBuilder, ranChangeSetList, new AfterTagChangeSetFilter(tagToRollBackTo, ranChangeSetList, tagVersion), rollbackReportParameters);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import liquibase.command.*;
import liquibase.database.Database;
import liquibase.logging.mdc.MdcKey;
import liquibase.report.RollbackReportParameters;
import liquibase.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

Expand All @@ -32,10 +36,19 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Scope.getCurrentScope().addMdcValue(MdcKey.ROLLBACK_TO_DATE, formatter.format(dateToRollBackTo));

RollbackReportParameters rollbackReportParameters = new RollbackReportParameters();
rollbackReportParameters.setCommandTitle(
StringUtil.upperCaseFirst(StringUtil.toKabobCase(Arrays.toString(
defineCommandNames()[0])).replace("[","").replace("]","").trim()));
resultsBuilder.addResult("rollbackReport", rollbackReportParameters);

Database database = (Database) commandScope.getDependency(Database.class);
rollbackReportParameters.getDatabaseInfo().setDatabaseType(database.getDatabaseProductName());
rollbackReportParameters.getDatabaseInfo().setVersion(database.getDatabaseProductVersion());
rollbackReportParameters.setJdbcUrl(database.getConnection().getURL());

List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
this.doRollback(resultsBuilder, ranChangeSetList, new ExecutedAfterChangeSetFilter(dateToRollBackTo, ranChangeSetList));
Scope.child(Collections.singletonMap("rollbackReport", rollbackReportParameters), () -> this.doRollback(resultsBuilder, ranChangeSetList, new ExecutedAfterChangeSetFilter(dateToRollBackTo, ranChangeSetList), rollbackReportParameters));
}

@Override
Expand Down