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

in.cleartax.dropwizard.sharding.migrations.DbLocksCommand Maven / Gradle / Ivy

There is a newer version: 0.2.198
Show newest version
package in.cleartax.dropwizard.sharding.migrations;

import com.google.common.annotations.VisibleForTesting;
import io.dropwizard.Configuration;
import liquibase.Liquibase;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;

import java.io.PrintStream;

import static com.google.common.base.MoreObjects.firstNonNull;

public class DbLocksCommand extends AbstractLiquibaseCommand {

    private PrintStream printStream = System.out;

    public DbLocksCommand(MultiTenantDatabaseConfiguration strategy, Class configurationClass, String migrationsFileName) {
        super("locks", "Manage database migration locks", strategy, configurationClass, migrationsFileName);
    }

    @VisibleForTesting
    void setPrintStream(PrintStream printStream) {
        this.printStream = printStream;
    }

    @Override
    public void configure(Subparser subparser) {
        super.configure(subparser);

        subparser.addArgument("-l", "--list")
                .dest("list")
                .action(Arguments.storeTrue())
                .setDefault(Boolean.FALSE)
                .help("list all open locks");

        subparser.addArgument("-r", "--force-release")
                .dest("release")
                .action(Arguments.storeTrue())
                .setDefault(Boolean.FALSE)
                .help("forcibly release all open locks");
    }

    @Override
    public void run(Namespace namespace, Liquibase liquibase) throws Exception {
        final boolean list = firstNonNull(namespace.getBoolean("list"), false);
        final boolean release = firstNonNull(namespace.getBoolean("release"), false);

        if (list == release) {
            throw new IllegalArgumentException("Must specify either --list or --force-release");
        } else if (list) {
            liquibase.reportLocks(printStream);
        } else {
            liquibase.forceReleaseLocks();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy