All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.command.core.RollbackSqlCommandStep Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
package liquibase.command.core;

import liquibase.TagVersionEnum;
import liquibase.command.*;
import liquibase.command.core.helpers.DatabaseChangelogCommandStep;
import liquibase.database.Database;
import liquibase.util.LoggingExecutorTextUtil;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * RollbackSqlCommandStep performs the rollback-to-tag-sql logic.
 */
public class RollbackSqlCommandStep extends RollbackCommandStep {

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

    public static final CommandArgumentDefinition OUTPUT_DEFAULT_SCHEMA_ARG;
    public static final CommandArgumentDefinition OUTPUT_DEFAULT_CATALOG_ARG;

    static {
        CommandBuilder builder = new CommandBuilder(COMMAND_NAME);
        OUTPUT_DEFAULT_SCHEMA_ARG = builder.argument("outputDefaultSchema", Boolean.class)
                .description("Control whether names of objects in the default schema are fully qualified or not. If true they are. If false, only objects outside the default schema are fully qualified")
                .defaultValue(true)
                .build();
        OUTPUT_DEFAULT_CATALOG_ARG = builder.argument("outputDefaultCatalog", Boolean.class)
                .description("Control whether names of objects in the default catalog are fully qualified or not. If true they are. If false, only objects outside the default catalog are fully qualified")
                .defaultValue(true)
                .build();

        builder.addArgument(TAG_ARG).build();
        builder.addArgument(TAG_VERSION_ARG).setValueHandler(TagVersionEnum::handleTagVersionInput).build();
        builder.addArgument(AbstractRollbackCommandStep.ROLLBACK_SCRIPT_ARG).build();
    }

    @Override
    public List> requiredDependencies() {
        List> dependencies = new ArrayList<>();
        dependencies.add(Writer.class);
        dependencies.addAll(super.requiredDependencies());
        return dependencies;
    }

    @Override
    public void run(CommandResultsBuilder resultsBuilder) throws Exception {
        CommandScope commandScope = resultsBuilder.getCommandScope();
        String tagToRollBackTo = commandScope.getArgumentValue(RollbackCommandStep.TAG_ARG);
        String changeLogFile = commandScope.getArgumentValue(DatabaseChangelogCommandStep.CHANGELOG_FILE_ARG);
        Database database = (Database) commandScope.getDependency(Database.class);
        LoggingExecutorTextUtil.outputHeader("Rollback to '" + tagToRollBackTo + "' Script", database, changeLogFile);
        super.run(resultsBuilder);
    }

    @Override
    public String[][] defineCommandNames() {
        return new String[][] { COMMAND_NAME };
    }


    @Override
    public void adjustCommandDefinition(CommandDefinition commandDefinition) {
        commandDefinition.setShortDescription("Generate the SQL to rollback changes made to the database based on the specific tag");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy