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 update report (DAT-15576) #4692

Merged
merged 12 commits into from
Aug 31, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import liquibase.exception.CommandLineParsingException;
import liquibase.exception.CommandValidationException;
import liquibase.exception.LiquibaseException;
import liquibase.integration.IntegrationDetails;
import liquibase.license.LicenseInfo;
import liquibase.license.LicenseService;
import liquibase.license.LicenseServiceFactory;
Expand Down Expand Up @@ -641,6 +642,10 @@ private Map<String, Object> configureScope(String[] args) throws Exception {
Map<String, Object> returnMap = new HashMap<>();
returnMap.put(COMMAND_ARGUMENTS, args);

final IntegrationDetails integrationDetails = new IntegrationDetails();
integrationDetails.setName("cli");
returnMap.put("integrationDetails", integrationDetails);

final ClassLoader classLoader = configureClassLoader();

returnMap.putAll(configureLogging());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

expectedDatabaseContent = [
Expand Down Expand Up @@ -89,7 +90,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

expectedDatabaseContent = [
Expand Down Expand Up @@ -122,7 +124,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/ignoredChangeset.txt")
Expand Down Expand Up @@ -153,7 +156,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/changeSetWithLabels.txt")
Expand Down Expand Up @@ -205,7 +209,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/changeSetWithLabels.txt")
Expand Down Expand Up @@ -245,7 +250,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/changeSetWithComplicatedLabelsAndContext.txt")
Expand Down Expand Up @@ -277,7 +283,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/mismatchedDBMS.txt")
Expand Down Expand Up @@ -313,7 +320,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

expectedDatabaseContent = [
Expand Down Expand Up @@ -378,7 +386,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

expectedLogs = [
Expand All @@ -397,7 +406,8 @@ Optional Args:

expectedResults = [
statusCode: 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand All @@ -65,7 +66,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand All @@ -83,7 +85,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/labelsAndContent.txt")
Expand All @@ -106,7 +109,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

expectedUI = [
Expand All @@ -127,7 +131,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/mismatchedDBMS.txt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

expectedOutput = [
Expand Down Expand Up @@ -94,7 +95,8 @@ INSERT INTO PUBLIC.DATABASECHANGELOG (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDERE

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand Down Expand Up @@ -79,7 +80,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand All @@ -68,7 +69,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/labelsAndContent.txt")
Expand Down Expand Up @@ -101,7 +103,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]

outputFile = new File("target/test-classes/mismatchedDBMS.txt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand Down Expand Up @@ -78,7 +79,8 @@ Optional Args:

expectedResults = [
statusCode : 0,
defaultChangeExecListener: 'not_null'
defaultChangeExecListener: 'not_null',
updateReport: 'not_null'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import liquibase.util.SqlUtil;
import liquibase.util.StreamUtil;
import liquibase.util.StringUtil;
import lombok.Getter;
import lombok.Setter;

import java.time.Instant;
import java.util.*;
Expand Down Expand Up @@ -238,6 +240,18 @@ public String toString() {
*/
private String deploymentId;

@Getter
@Setter
private List<String> generatedSql = new ArrayList<>();

@Getter
@Setter
private ExecType execType;

@Getter
StevenMassaro marked this conversation as resolved.
Show resolved Hide resolved
@Setter
private String errorMsg;

public boolean shouldAlwaysRun() {
return alwaysRun;
}
Expand Down Expand Up @@ -611,8 +625,6 @@ public ExecType execute(DatabaseChangeLog databaseChangeLog, ChangeExecListener
long startTime = new Date().getTime();
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OPERATION_START_TIME, Instant.ofEpochMilli(startTime).toString());

ExecType execType = null;

boolean skipChange = false;

Executor originalExecutor = setupCustomExecutorIfNecessary(database);
Expand Down Expand Up @@ -723,7 +735,8 @@ public ExecType execute(DatabaseChangeLog databaseChangeLog, ChangeExecListener
executor.comment("WARNING The following SQL may change each run and therefore is possibly incorrect and/or invalid:");
}

addSqlMdc(change, database, false);
String sql = addSqlMdc(change, database, false);
this.getGeneratedSql().add(sql);

database.executeStatements(change, databaseChangeLog, sqlVisitors);
log.info(change.getConfirmationMessage());
Expand Down Expand Up @@ -752,6 +765,7 @@ public ExecType execute(DatabaseChangeLog databaseChangeLog, ChangeExecListener
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OPERATION_STOP_TIME, Instant.ofEpochMilli(new Date().getTime()).toString());
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_OUTCOME, ExecType.FAILED.value.toLowerCase());
log.severe(String.format("ChangeSet %s encountered an exception.", toString(false)));
setErrorMsg(e.getMessage());
try {
database.rollback();
} catch (Exception e1) {
Expand Down Expand Up @@ -876,7 +890,8 @@ public void rollback(Database database, ChangeExecListener listener) throws Roll
}
//
SqlStatement[] changeStatements = change.generateStatements(database);
addSqlMdc(change, database, false);
String sql = addSqlMdc(change, database, false);
this.getGeneratedSql().add(sql);
if (change instanceof SQLFileChange) {
addSqlFileMdc((SQLFileChange) change);
}
Expand All @@ -899,7 +914,8 @@ public void rollback(Database database, ChangeExecListener listener) throws Roll
throw new RollbackFailedException("Liquibase does not support automatic rollback generation for raw " +
"sql changes (did you mean to specify keyword \"empty\" to ignore rolling back this change?)");
}
addSqlMdc(change, database, true);
String sql = addSqlMdc(change, database, true);
this.getGeneratedSql().add(sql);
database.executeRollbackStatements(change, sqlVisitors);
}
}
Expand Down Expand Up @@ -1558,13 +1574,13 @@ public void addChangeSetMdcProperties() {
* @throws RollbackImpossibleException if you cannot generate rollback statements
*
*/
private void addSqlMdc(Change change, Database database, boolean generateRollbackStatements) throws Exception {
private String addSqlMdc(Change change, Database database, boolean generateRollbackStatements) throws Exception {
//
// If the change is for this Database
// add a Boolean flag to Scope to indicate that the Change should not be executed when adding MDC context
//
if (! change.supports(database)) {
return;
return null;
}
AtomicReference<SqlStatement[]> statementsReference = new AtomicReference<>();
Map<String, Object> scopeValues = new HashMap<>();
Expand All @@ -1575,5 +1591,6 @@ private void addSqlMdc(Change change, Database database, boolean generateRollbac
.map(statement -> SqlUtil.getSqlString(statement, SqlGeneratorFactory.getInstance(), database))
.collect(Collectors.joining("\n"));
Scope.getCurrentScope().addMdcValue(MdcKey.CHANGESET_SQL, sqlStatementsMdc);
return sqlStatementsMdc;
}
}