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

org.liquibase.maven.plugins.LiquibaseRollbackOneUpdateMojo 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;

/**
 *
 * Rolls back all changesets from any specific update, if all changesets can be rolled back.
 * By default, the last update is rolled back, but an optional deployentId parameter can target any update.
 * (Liquibase Pro only).
 *
 * @goal rollbackOneUpdate
 *
 */
public class LiquibaseRollbackOneUpdateMojo extends AbstractLiquibaseChangeLogMojo {
    /**
     *
     * Specifies the update your want to rollback.  A list of the updates's
     * changesets grouped by their deploymentId can be found by using the history command.
     *
     * @parameter property="liquibase.deploymentId"
     *
     */
    @PropertyElement
    protected String deploymentId;

    /**
     *
     * A required flag for rollbackOneUpdate.
     *
     * @parameter property="liquibase.force"
     *
     */
    @PropertyElement
    protected String force;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        commandName = "rollbackOneUpdate";
        super.execute();
    }

    @Override
    protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException {
        super.performLiquibaseTask(liquibase);

        //
        // Check the Pro license
        //
        Database database = liquibase.getDatabase();
        CommandScope liquibaseCommand = new CommandScope("rollbackOneUpdate");
        Map argsMap = getCommandArgsObjectMap(liquibase);
        ChangeLogParameters clp = new ChangeLogParameters(database);
        argsMap.put("changeLogParameters", clp);
        if (force == null || (force != null && ! Boolean.parseBoolean(force))) {
            throw new LiquibaseException("Invalid value for --force.  You must specify 'liquibase.force=true' to use rollbackOneUpdate.");
        }
        argsMap.put("force", Boolean.TRUE);
        argsMap.put("liquibase", liquibase);
        for (Map.Entry entry : argsMap.entrySet()) {
            liquibaseCommand.addArgumentValue(entry.getKey(), entry.getValue());
        }

        liquibaseCommand.execute();
    }

    private Map getCommandArgsObjectMap(Liquibase liquibase) throws LiquibaseException {
        Database database = liquibase.getDatabase();
        Map argsMap = new HashMap<>();
        argsMap.put("deploymentId", this.deploymentId);
        argsMap.put("force", this.force);
        argsMap.put("database", database);
        argsMap.put("changeLog", liquibase.getDatabaseChangeLog());
        argsMap.put("resourceAccessor", liquibase.getResourceAccessor());
        argsMap.put("changeLogFile", changeLogFile);
        return argsMap;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy