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

Implement "author" command argument for diffChangelog command #3990

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ public class DiffChangelogCommandStep extends AbstractCommandStep {

public static final String[] COMMAND_NAME = {"diffChangelog"};

public static final CommandArgumentDefinition<String> AUTHOR_ARG;
public static final CommandArgumentDefinition<String> CHANGELOG_FILE_ARG;

static {
final CommandBuilder builder = new CommandBuilder(COMMAND_NAME);
CHANGELOG_FILE_ARG = builder.argument(CommonArgumentNames.CHANGELOG_FILE, String.class)
.description("Changelog file to write results").required().build();
AUTHOR_ARG = builder.argument("author", String.class)
.description("Specifies the author for changesets in the generated changelog").build();
}

@Override
Expand Down Expand Up @@ -70,10 +73,13 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
String changeLogFile = commandScope.getArgumentValue(CHANGELOG_FILE_ARG);
Scope.getCurrentScope().addMdcValue(MdcKey.DIFF_CHANGELOG_FILE, changeLogFile);
referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);

DiffToChangeLog changeLogWriter = createDiffToChangeLogObject(diffResult, diffOutputControl);
changeLogWriter.setChangeSetAuthor(commandScope.getArgumentValue(AUTHOR_ARG));
if (StringUtil.trimToNull(changeLogFile) == null) {
createDiffToChangeLogObject(diffResult, diffOutputControl).print(outputStream);
changeLogWriter.print(outputStream);
} else {
createDiffToChangeLogObject(diffResult, diffOutputControl).print(changeLogFile);
changeLogWriter.print(changeLogFile);
}
}
finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,19 @@ public static void doDiff(Database referenceDatabase, Database targetDatabase, S
public static void doDiffToChangeLog(String changeLogFile,
Database referenceDatabase,
Database targetDatabase,
String author,
DiffOutputControl diffOutputControl,
ObjectChangeFilter objectChangeFilter,
String snapshotTypes)
throws LiquibaseException, IOException, ParserConfigurationException {
doDiffToChangeLog(changeLogFile, referenceDatabase, targetDatabase, diffOutputControl, objectChangeFilter,
doDiffToChangeLog(changeLogFile, referenceDatabase, targetDatabase, author, diffOutputControl, objectChangeFilter,
snapshotTypes, null);
}

public static void doDiffToChangeLog(String changeLogFile,
Database referenceDatabase,
Database targetDatabase,
String author,
DiffOutputControl diffOutputControl,
ObjectChangeFilter objectChangeFilter,
String snapshotTypes,
Expand All @@ -219,7 +221,8 @@ public static void doDiffToChangeLog(String changeLogFile,
.addArgumentValue(DiffChangelogCommandStep.CHANGELOG_FILE_ARG, changeLogFile)
.addArgumentValue(DiffOutputControlCommandStep.INCLUDE_CATALOG_ARG, diffOutputControl.getIncludeCatalog())
.addArgumentValue(DiffOutputControlCommandStep.INCLUDE_SCHEMA_ARG, diffOutputControl.getIncludeSchema())
.addArgumentValue(DiffOutputControlCommandStep.INCLUDE_TABLESPACE_ARG, diffOutputControl.getIncludeTablespace());
.addArgumentValue(DiffOutputControlCommandStep.INCLUDE_TABLESPACE_ARG, diffOutputControl.getIncludeTablespace())
.addArgumentValue(DiffChangelogCommandStep.AUTHOR_ARG, author);
command.setOutput(System.out);
try {
command.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,7 @@ private void runDiffChangelogCommandStep() throws CommandExecutionException, Com
.addArgumentValue(DiffOutputControlCommandStep.INCLUDE_CATALOG_ARG, includeCatalog)
.addArgumentValue(DiffOutputControlCommandStep.INCLUDE_SCHEMA_ARG, includeSchema)
.addArgumentValue(DiffOutputControlCommandStep.INCLUDE_TABLESPACE_ARG, includeTablespace)
.addArgumentValue(DiffChangelogCommandStep.AUTHOR_ARG, StringUtil.trimToNull(changeSetAuthor))
.setOutput(getOutputStream());

this.setPreCompareArgumentsToCommand(diffChangelogCommand);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package liquibase.extension.testing.command

import liquibase.change.AddColumnConfig

import liquibase.change.ColumnConfig
import liquibase.change.ConstraintsConfig
import liquibase.change.core.AddColumnChange
import liquibase.change.core.AddPrimaryKeyChange
import liquibase.change.core.CreateTableChange
import liquibase.exception.CommandExecutionException
import liquibase.exception.CommandValidationException
import liquibase.extension.testing.setup.SetupCleanResources
import liquibase.structure.core.Column

import java.util.regex.Pattern

Expand All @@ -24,6 +21,8 @@ Required Args:
url (String) The JDBC database connection URL
OBFUSCATED
Optional Args:
author (String) Specifies the author for changesets in the generated changelog
Default: null
defaultCatalogName (String) The default catalog name to use for the database connection
Default: null
defaultSchemaName (String) The default schema name to use for the database connection
Expand Down Expand Up @@ -114,8 +113,9 @@ Optional Args:
]
}

run "Running diffChangelog should add changesets", {
run "Running diffChangelog should add changesets with specified author", {
arguments = [
author : "Test Author",
url : { it.url },
username : { it.username },
password : { it.password },
Expand Down Expand Up @@ -168,7 +168,8 @@ Optional Args:
}
expectedFileContent = [
"target/test-classes/diffChangeLog-test.xml" : [CommandTests.assertContains("<changeSet ", 5),
CommandTests.assertContains("<dropTable ", 1)]
CommandTests.assertContains("<dropTable ", 1),
CommandTests.assertContains("author=\"Test Author\"", 5)]
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ public class LiquibaseDatabaseDiff extends AbstractLiquibaseChangeLogMojo {
*/
@PropertyElement
protected String diffTypes;
/**
* The author to be specified for Changesets in the generated Change Log.
*
* @parameter property="liquibase.changeSetAuthor"
*/
@PropertyElement
protected String changeSetAuthor;
/**
* Objects to be excluded from the changelog. Example filters: "table_name", "table:main_.*", "column:*._lock, table:primary.*".
*
Expand Down Expand Up @@ -254,7 +261,7 @@ protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseExcepti
try {
DiffOutputControl diffOutputControl = new DiffOutputControl(diffIncludeCatalog, diffIncludeSchema, diffIncludeTablespace, null).addIncludedSchema(new CatalogAndSchema(referenceDefaultCatalogName, referenceDefaultSchemaName));
diffOutputControl.setObjectChangeFilter(objectChangeFilter);
CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db, diffOutputControl,
CommandLineUtils.doDiffToChangeLog(diffChangeLogFile, referenceDatabase, db, changeSetAuthor, diffOutputControl,
objectChangeFilter, StringUtil.trimToNull(diffTypes), schemaComparisons);
if (new File(diffChangeLogFile).exists()) {
getLog().info("Differences written to Change Log File, " + diffChangeLogFile);
Expand Down