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

Allow override of snapshot control creation for snapshotReference command DAT-16683 #5548

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -28,6 +28,7 @@ public class InternalSnapshotCommandStep extends AbstractCommandStep {
public static final CommandArgumentDefinition<CatalogAndSchema[]> SCHEMAS_ARG;
public static final CommandArgumentDefinition<String> SERIALIZER_FORMAT_ARG;
public static final CommandArgumentDefinition<SnapshotListener> SNAPSHOT_LISTENER_ARG;
public static final CommandArgumentDefinition<SnapshotControl> SNAPSHOT_CONTROL_ARG;

private Map<String, Object> snapshotMetadata;

Expand All @@ -38,6 +39,7 @@ public class InternalSnapshotCommandStep extends AbstractCommandStep {
SCHEMAS_ARG = builder.argument("schemas", CatalogAndSchema[].class).build();
SERIALIZER_FORMAT_ARG = builder.argument("serializerFormat", String.class).build();
SNAPSHOT_LISTENER_ARG = builder.argument("snapshotListener", SnapshotListener.class).build();
SNAPSHOT_CONTROL_ARG = builder.argument("snapshotControl", SnapshotControl.class).hidden().build();
}

@Override
Expand Down Expand Up @@ -82,7 +84,10 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
CatalogAndSchema[] schemas = commandScope.getArgumentValue(SCHEMAS_ARG);

InternalSnapshotCommandStep.logUnsupportedDatabase(database, this.getClass());
SnapshotControl snapshotControl = new SnapshotControl(database);
SnapshotControl snapshotControl = commandScope.getArgumentValue(SNAPSHOT_CONTROL_ARG);
if (snapshotControl == null) {
snapshotControl = new SnapshotControl(database);
}
snapshotControl.setSnapshotListener(snapshotListener);

if (schemas == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,13 @@ public void adjustCommandDefinition(CommandDefinition commandDefinition) {
commandDefinition.setShortDescription("Capture the current state of the database");
}

private CatalogAndSchema[] parseSchemas(Database database, String... schemas) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was duplicated? Nice catch

if ((schemas == null) || (schemas.length == 0) || (schemas[0] == null)) {
return null;
}

schemas = StringUtil.join(schemas, ",").split("\\s*,\\s*");
List<CatalogAndSchema> finalList = new ArrayList<>();
for (String schema : schemas) {
finalList.add(new CatalogAndSchema(null, schema).customize(database));
}

return finalList.toArray(new CatalogAndSchema[0]);
}

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();

Database database = (Database) commandScope.getDependency(Database.class);

CatalogAndSchema[] schemas = parseSchemas(database, commandScope.getArgumentValue(SCHEMAS_ARG));
CatalogAndSchema[] schemas = InternalSnapshotCommandStep.parseSchemas(database, commandScope.getArgumentValue(SCHEMAS_ARG));

InternalSnapshotCommandStep.logUnsupportedDatabase(database, this.getClass());
SnapshotControl snapshotControl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import liquibase.command.core.helpers.ReferenceDatabaseOutputWriterCommandStep;
import liquibase.command.providers.ReferenceDatabase;
import liquibase.database.Database;
import liquibase.snapshot.SnapshotControl;

import java.io.Writer;
import java.util.Arrays;
Expand All @@ -12,13 +13,15 @@
public class SnapshotReferenceCommandStep extends AbstractCommandStep {

public static final String[] COMMAND_NAME = {"snapshotReference"};
public static final CommandArgumentDefinition<SnapshotControl> SNAPSHOT_CONTROL_ARG;

public static final CommandArgumentDefinition<String> SNAPSHOT_FORMAT_ARG;

static {
CommandBuilder builder = new CommandBuilder(COMMAND_NAME);
SNAPSHOT_FORMAT_ARG = builder.argument("snapshotFormat", String.class)
.description("Output format to use (JSON or YAML)").build();
SNAPSHOT_CONTROL_ARG = builder.argument("snapshotControl", SnapshotControl.class).hidden().build();
}

@Override
Expand All @@ -36,15 +39,21 @@
commandDefinition.setShortDescription("Capture the current state of the reference database");
}

protected SnapshotControl createSnapshotControl(CommandScope commandScope, Database database) {
Dismissed Show dismissed Hide dismissed
return new SnapshotControl(database);
}

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
CommandScope snapshotReferenceCommand = resultsBuilder.getCommandScope();
Database referenceDatabase = (Database) snapshotReferenceCommand.getDependency(ReferenceDatabase.class);
Writer outputWriter = ((Writer) snapshotReferenceCommand.getDependency(ReferenceDatabaseOutputWriterCommandStep.ReferenceDatabaseWriter.class));

SnapshotControl snapshotControl = createSnapshotControl(snapshotReferenceCommand, referenceDatabase);
CommandScope snapshotCommand = new CommandScope(InternalSnapshotCommandStep.COMMAND_NAME);
snapshotCommand
.addArgumentValue(InternalSnapshotCommandStep.DATABASE_ARG, referenceDatabase)
.addArgumentValue(InternalSnapshotCommandStep.SNAPSHOT_CONTROL_ARG, snapshotControl)
.addArgumentValue(InternalSnapshotCommandStep.SERIALIZER_FORMAT_ARG, snapshotCommand.getArgumentValue(SNAPSHOT_FORMAT_ARG));

outputWriter.write(InternalSnapshotCommandStep.printSnapshot(snapshotCommand, snapshotCommand.execute()));
Expand Down