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

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

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

import io.dropwizard.Configuration;
import liquibase.Liquibase;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;

import java.util.SortedMap;
import java.util.TreeMap;

import static java.util.Objects.requireNonNull;

public class DbCommand extends AbstractLiquibaseCommand {
    private static final String COMMAND_NAME_ATTR = "subcommand";
    private final SortedMap> subcommands;

    public DbCommand(String name, MultiTenantDatabaseConfiguration strategy, Class configurationClass, String migrationsFileName) {
        super(name, "Run database migration tasks", strategy, configurationClass, migrationsFileName);
        this.subcommands = new TreeMap<>();
        addSubcommand(new DbCalculateChecksumCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbClearChecksumsCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbDropAllCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbDumpCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbFastForwardCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbGenerateDocsCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbLocksCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbMigrateCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbPrepareRollbackCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbRollbackCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbStatusCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbTagCommand<>(strategy, configurationClass, migrationsFileName));
        addSubcommand(new DbTestCommand<>(strategy, configurationClass, migrationsFileName));
    }

    private void addSubcommand(AbstractLiquibaseCommand subcommand) {
        subcommands.put(subcommand.getName(), subcommand);
    }

    @Override
    public void configure(Subparser subparser) {
        for (AbstractLiquibaseCommand subcommand : subcommands.values()) {
            final Subparser cmdParser = subparser.addSubparsers()
                    .addParser(subcommand.getName())
                    .setDefault(COMMAND_NAME_ATTR, subcommand.getName())
                    .description(subcommand.getDescription());
            subcommand.configure(cmdParser);
        }
    }

    @Override
    public void run(Namespace namespace, Liquibase liquibase) throws Exception {
        final AbstractLiquibaseCommand subcommand =
                requireNonNull(subcommands.get(namespace.getString(COMMAND_NAME_ATTR)), "Unable find the command");
        subcommand.run(namespace, liquibase);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy