Skip to content

Commit

Permalink
New branch to test build (liquibase#4235)
Browse files Browse the repository at this point in the history
* Handle logical file path settings for changelogFile key in MDC

DAT-13879

* Add changelogFile to MDC in ChangeSet to handle includes and
logical path

DAT-13879

* Fix changelogFile MDC value

DAT-13879

* Fix test argument

DAT-13879

* DAT-13879

Fix potential NPE

* Added constants for MDC Flow
Test method for creating random file path

DAT-13124

* Fix master build.

* Fix test

DAT-13879

* Fix test

DAT-13879

* Test fixes

DAT-13879

* Last change was not correct

DAT-13879

* Add changelogFile key in the correct place

DAT-13879

* Fix index descending column snapshot test

* Added changelogFile MDC attribute to rollback and changelogSync commands
DAT-13879

* Forced a commit

* Add the changelogFile to the MDC on exception

* Fix merge conflict

---------

Co-authored-by: filipe <[email protected]>
  • Loading branch information
wwillard7800 and filipelautert authored May 17, 2023
1 parent 5bcd1f9 commit dfc6312
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ derby.log

atlassian-ide-plugin.xml
_issues
/liquibase-core/tmp
/liquibase-standard/tmp
/liquibase-integration-tests/tmp
/liquibase-integration-tests/liquibase
/liquibase-integration-tests/liquibase.trace.db
Expand Down
6 changes: 5 additions & 1 deletion liquibase-standard/src/main/java/liquibase/Liquibase.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,17 @@ public DatabaseChangeLog getDatabaseChangeLog() throws LiquibaseException {
*/
private DatabaseChangeLog getDatabaseChangeLog(boolean shouldWarnOnMismatchedXsdVersion) throws LiquibaseException {
if (databaseChangeLog == null && changeLogFile != null) {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changeLogFile);
ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser(changeLogFile, resourceAccessor);
if (parser instanceof XMLChangeLogSAXParser) {
((XMLChangeLogSAXParser) parser).setShouldWarnOnMismatchedXsdVersion(shouldWarnOnMismatchedXsdVersion);
}
databaseChangeLog = parser.parse(changeLogFile, changeLogParameters, resourceAccessor);
Scope.getCurrentScope().getLog(Liquibase.class).info("Parsed changelog file '" + changeLogFile + "'");
if (StringUtil.isNotEmpty(databaseChangeLog.getLogicalFilePath())) {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, databaseChangeLog.getLogicalFilePath());
} else {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changeLogFile);
}
}

return databaseChangeLog;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,9 @@ public void addChangeSetMdcProperties() {
String commentMdc = comments != null ? comments : "";
String labelMdc = labels != null ? labels.toString() : "";
String contextsMdc = contextFilter != null && contextFilter.getOriginalString() != null ? contextFilter.getOriginalString() : "";

String changelogPath = (getChangeLog() != null ? getChangeLog().getLogicalFilePath() : null);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changelogPath);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_COMMENT, commentMdc);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_LABEL, labelMdc);
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_CONTEXT, contextsMdc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ protected void handleChildNode(ParsedNode node, ResourceAccessor resourceAccesso
throw new UnexpectedLiquibaseException("No 'file' attribute on 'include'");
}
path = path.replace('\\', '/');
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, path);
ContextExpression includeContextFilter = new ContextExpression(node.getChildValue(null, "contextFilter", String.class));
if (includeContextFilter.isEmpty()) {
includeContextFilter = new ContextExpression(node.getChildValue(null, "context", String.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import liquibase.changelog.visitor.ChangeSetVisitor;
import liquibase.changelog.visitor.RollbackVisitor;
import liquibase.command.*;
import liquibase.command.core.helpers.DatabaseChangelogCommandStep;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
Expand All @@ -21,6 +22,7 @@
import liquibase.logging.mdc.customobjects.ChangesetsRolledback;
import liquibase.resource.Resource;
import liquibase.util.StreamUtil;
import liquibase.util.StringUtil;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -49,6 +51,7 @@ private enum RollbackMessageType {
protected void doRollback(CommandResultsBuilder resultsBuilder, List<RanChangeSet> ranChangeSetList,
ChangeSetFilter changeSetFilter) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();
String changelogFile = commandScope.getArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_FILE_ARG);
String rollbackScript = commandScope.getArgumentValue(ROLLBACK_SCRIPT_ARG);
Scope.getCurrentScope().addMdcValue(MdcKey.ROLLBACK_SCRIPT, rollbackScript);

Expand All @@ -66,7 +69,7 @@ protected void doRollback(CommandResultsBuilder resultsBuilder, List<RanChangeSe
new DbmsChangeSetFilter(database),
changeSetFilter);

doRollback(database, rollbackScript, logIterator, changeLogParameters, databaseChangeLog,
doRollback(database, changelogFile, rollbackScript, logIterator, changeLogParameters, databaseChangeLog,
(ChangeExecListener) commandScope.getDependency(ChangeExecListener.class));
}
catch (Throwable t) {
Expand All @@ -81,13 +84,12 @@ protected void doRollback(CommandResultsBuilder resultsBuilder, List<RanChangeSe
* Actually perform the rollback operation. Determining which changesets to roll back is the responsibility of the
* logIterator.
*/
public static void doRollback(Database database, 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.");
Expand All @@ -96,13 +98,22 @@ public static void doRollback(Database database, String rollbackScript, ChangeLo
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.");
}
}

private static void addChangelogFileToMdc(String changelogFile, DatabaseChangeLog databaseChangeLog) {
if (StringUtil.isNotEmpty(databaseChangeLog.getLogicalFilePath())) {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, databaseChangeLog.getLogicalFilePath());
} else {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changelogFile);
}
}

private static List<ChangeSet> determineRollbacks(Database database, ChangeLogIterator logIterator, ChangeLogParameters changeLogParameters)
throws LiquibaseException {
List<ChangeSet> changeSetsToRollback = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import liquibase.logging.mdc.MdcValue;
import liquibase.logging.mdc.customobjects.ChangesetsUpdated;
import liquibase.util.ShowSummaryUtil;
import liquibase.util.StringUtil;

import java.io.IOException;
import java.io.OutputStream;
Expand Down Expand Up @@ -86,9 +87,12 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
ShowSummaryUtil.showUpdateSummary(databaseChangeLog, getShowSummary(commandScope), statusVisitor, resultsBuilder.getOutputStream());
});
resultsBuilder.addResult("statusCode", 0);
addChangelogFileToMdc(getChangelogFileArg(commandScope), databaseChangeLog);
logDeploymentOutcomeMdc(defaultChangeExecListener, true);
postUpdateLog();
} catch (Exception e) {
DatabaseChangeLog databaseChangeLog = (DatabaseChangeLog) commandScope.getDependency(DatabaseChangeLog.class);
addChangelogFileToMdc(getChangelogFileArg(commandScope), databaseChangeLog);
logDeploymentOutcomeMdc(defaultChangeExecListener, false);
resultsBuilder.addResult("statusCode", 1);
throw e;
Expand All @@ -103,6 +107,14 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
}
}

private void addChangelogFileToMdc(String changeLogFile, DatabaseChangeLog databaseChangeLog) {
if (StringUtil.isNotEmpty(databaseChangeLog.getLogicalFilePath())) {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, databaseChangeLog.getLogicalFilePath());
} else {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changeLogFile);
}
}

protected void customMdcLogging(CommandScope commandScope) {
// do nothing by default
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import liquibase.logging.mdc.MdcKey;
import liquibase.logging.mdc.MdcObject;
import liquibase.logging.mdc.MdcValue;
import liquibase.util.StringUtil;

import java.util.Arrays;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import liquibase.parser.ChangeLogParserFactory;
import liquibase.parser.core.xml.XMLChangeLogSAXParser;
import liquibase.resource.ResourceAccessor;
import liquibase.util.StringUtil;

import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -98,12 +99,17 @@ public static void addCommandFiltersMdc(LabelExpression labelExpression, Context

private DatabaseChangeLog getDatabaseChangeLog(String changeLogFile, ChangeLogParameters changeLogParameters) throws LiquibaseException {
ResourceAccessor resourceAccessor = Scope.getCurrentScope().getResourceAccessor();
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changeLogFile);
ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser(changeLogFile, resourceAccessor);
if (parser instanceof XMLChangeLogSAXParser) {
((XMLChangeLogSAXParser) parser).setShouldWarnOnMismatchedXsdVersion(false);
}
return parser.parse(changeLogFile, changeLogParameters, resourceAccessor);
DatabaseChangeLog changelog = parser.parse(changeLogFile, changeLogParameters, resourceAccessor);
if (StringUtil.isNotEmpty(changelog.getLogicalFilePath())) {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changelog.getLogicalFilePath());
} else {
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGELOG_FILE, changeLogFile);
}
return changelog;
}

private void checkLiquibaseTables(boolean updateExistingNullChecksums, DatabaseChangeLog databaseChangeLog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public class MdcKey {
public static final String OPERATION_STOP = "operationStop";
public static final String DIFF_CHANGELOG_FILE = "diffChangelogFile";
public static final String DIFF_CHANGELOG_OUTCOME = "diffChangelogOutcome";

public static final String CHANGESET_SYNC_OUTCOME = "changesetSyncOutcome";

public static final String FLOW_FILE_ROOT = "flowFileRoot";
Expand Down

0 comments on commit dfc6312

Please sign in to comment.