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

DAT-16798: Add new parameters to rollback report #5630

Merged
merged 10 commits into from
Mar 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static void doRollback(Database database, String changelogFile, String ro
rollbackReportParameters.getChangesetInfo().setChangesetCount(failedChangeSets.size() + rolledBackChangeSets.size());
rollbackReportParameters.getChangesetInfo().addAllToChangesetInfoList(rolledBackChangeSets, true);
rollbackReportParameters.getChangesetInfo().addAllToChangesetInfoList(failedChangeSets, true);
rollbackReportParameters.getChangesetInfo().setFailedChangesetCount(failedChangeSets.size());
}
}
}
Expand Down Expand Up @@ -243,6 +244,7 @@ private static void handleRollbackException(String operationName, RollbackReport
Scope.getCurrentScope().getLog(AbstractRollbackCommandStep.class).info(operationName + " command encountered an exception.");
if (rollbackReportParameters != null) {
rollbackReportParameters.getOperationInfo().setOperationOutcome(MdcValue.COMMAND_FAILED);
rollbackReportParameters.setSuccess(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
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());
rollbackReportParameters.setupDatabaseInfo(database);
rollbackReportParameters.setRollbackTag(tagToRollBackTo);

List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
TagVersionEnum tagVersion = TagVersionEnum.valueOf(commandScope.getArgumentValue(TAG_VERSION_ARG));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
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());
rollbackReportParameters.setupDatabaseInfo(database);
rollbackReportParameters.setRollbackCount(changesToRollback);

List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();
Date dateToRollBackTo = commandScope.getArgumentValue(DATE_ARG);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
Scope.getCurrentScope().addMdcValue(MdcKey.ROLLBACK_TO_DATE, formatter.format(dateToRollBackTo));
String stringDateToRollbackTo = formatter.format(dateToRollBackTo);
Scope.getCurrentScope().addMdcValue(MdcKey.ROLLBACK_TO_DATE, stringDateToRollbackTo);

RollbackReportParameters rollbackReportParameters = new RollbackReportParameters();
rollbackReportParameters.setCommandTitle(
Expand All @@ -43,9 +44,8 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
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());
rollbackReportParameters.setupDatabaseInfo(database);
rollbackReportParameters.setRollbackDate(stringDateToRollbackTo);

List<RanChangeSet> ranChangeSetList = database.getRanChangeSetList();
Scope.child(Collections.singletonMap("rollbackReport", rollbackReportParameters), () -> this.doRollback(resultsBuilder, ranChangeSetList, new ExecutedAfterChangeSetFilter(dateToRollBackTo, ranChangeSetList), rollbackReportParameters));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package liquibase.report;

import liquibase.changelog.ChangeSet;
import liquibase.util.CollectionUtil;
import liquibase.util.NetUtil;
import liquibase.util.StringUtil;
import lombok.AllArgsConstructor;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Data
public class RollbackReportParameters implements UpdateRollbackReportParameters {
private String changelogArgValue;
private String jdbcUrl;
private Integer rollbackCount;
private String rollbackTag;
private String rollbackDate;
private Boolean success = Boolean.TRUE; // assume success until we know we failed
private String commandTitle = "Rollback";
private final DatabaseInfo databaseInfo = new DatabaseInfo();
private final RuntimeInfo runtimeInfo = new RuntimeInfo();
private final OperationInfo operationInfo = new OperationInfo();
private final CustomData customData = new CustomData();
private final ChangesetInfo changesetInfo = new ChangesetInfo();
private final Date date = new Date();

/**
* Setup database related info used in rollback report.
*
* @param database the database the report is run against
* @throws DatabaseException if unable to determine the database product version
*/
public void setupDatabaseInfo(Database database) throws DatabaseException {
this.getDatabaseInfo().setDatabaseType(database.getDatabaseProductName());
this.getDatabaseInfo().setVersion(database.getDatabaseProductVersion());
this.getDatabaseInfo().setDatabaseUrl(database.getConnection().getURL());
this.setJdbcUrl(database.getConnection().getURL());
}
}