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

org.liquibase.maven.plugins.LiquibaseRollbackOneChangeSetSQL Maven / Gradle / Ivy

The newest version!
package org.liquibase.maven.plugins;

import liquibase.Liquibase;
import liquibase.changelog.ChangeLogParameters;
import liquibase.command.*;
import liquibase.database.Database;
import liquibase.exception.LiquibaseException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.liquibase.maven.property.PropertyElement;

import java.util.HashMap;
import java.util.Map;

/**
 * Displays the SQL which will be executed when the corresponding rollbackOneChangeSet command is
 * executed.  This command does not perform the actual rollback.  A Liquibase Pro license key is required.
 *
 * @goal rollbackOneChangeSetSQL
 */
public class LiquibaseRollbackOneChangeSetSQL extends AbstractLiquibaseChangeLogMojo {

    /**
     * The changeset ID to rollback
     *
     * @parameter property="liquibase.changeSetId"
     */
    @PropertyElement
    protected String changeSetId;

    /**
     * Specifies the author of the changeSet you want to rollback.
     *
     * @parameter property="liquibase.changeSetAuthor"
     */
    @PropertyElement
    protected String changeSetAuthor;

    /**
     * Specifies the path to the changelog which contains the change-set you want to rollback.
     *
     * @parameter property="liquibase.changeSetPath"
     */
    @PropertyElement
    protected String changeSetPath;

    /**
     * Specifies the path to a rollback script
     *
     * @parameter property="liquibase.rollbackScript"
     */
    @PropertyElement
    protected String rollbackScript;

    /**
     * Specifies the path to the generated SQL output file.
     *
     * @parameter property="liquibase.outputFile"
     */
    @PropertyElement
    protected String outputFile;

    @Override
    protected void printSettings(String indent) {
        super.printSettings(indent);
        getLog().info(indent + "Changeset ID:     " + changeSetId);
        getLog().info(indent + "Changeset Author: " + changeSetAuthor);
        getLog().info(indent + "Changeset Path:   " + changeSetPath);
        getLog().info(indent + "Rollback script:   " + rollbackScript);
    }

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        //
        // We override in order to set the command name for later use
        //
        commandName = "rollbackOneChangeSetSQL";
        super.execute();
    }

    @Override
    protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
        //
        // Check the Pro license
        //
        Database database = liquibase.getDatabase();
        CommandScope liquibaseCommand = new CommandScope("rollbackOneChangeSetSQL");

        Map argsMap = getCommandArgsObjectMap(liquibase);
        ChangeLogParameters clp = new ChangeLogParameters(database);
        argsMap.put("changeLogParameters", clp);
        argsMap.put("liquibase", liquibase);

        for (Map.Entry entry : argsMap.entrySet()) {
            liquibaseCommand.addArgumentValue(entry.getKey(), entry.getValue());
        }
        liquibaseCommand.setOutput(getOutputStream(outputFile));
        liquibaseCommand.execute();
    }

    private Map getCommandArgsObjectMap(Liquibase liquibase) throws LiquibaseException {
        Database database = liquibase.getDatabase();
        Map argsMap = new HashMap<>();
        argsMap.put("force", true);
        argsMap.put("changeLogFile", this.changeLogFile);
        argsMap.put("database", database);
        argsMap.put("changeLog", liquibase.getDatabaseChangeLog());
        argsMap.put("resourceAccessor", liquibase.getResourceAccessor());
        argsMap.put("changesetId", this.changeSetId);
        argsMap.put("changesetAuthor", this.changeSetAuthor);
        argsMap.put("changesetPath", this.changeSetPath);
        argsMap.put("rollbackScript", this.rollbackScript);
        argsMap.put("outputFile", this.outputFile);
        return argsMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy