Last active
January 4, 2016 06:39
-
-
Save andrewrlee/8582940 to your computer and use it in GitHub Desktop.
dropwizard flyway module.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.sourceforge.argparse4j.inf.Namespace; | |
import com.googlecode.flyway.core.Flyway; | |
import com.googlecode.flyway.core.api.MigrationInfoService; | |
import com.yammer.dropwizard.Bundle; | |
import com.yammer.dropwizard.cli.ConfiguredCommand; | |
import com.yammer.dropwizard.config.Bootstrap; | |
import com.yammer.dropwizard.config.Configuration; | |
import com.yammer.dropwizard.config.Environment; | |
import com.yammer.dropwizard.db.ConfigurationStrategy; | |
import com.yammer.dropwizard.db.DatabaseConfiguration; | |
import com.yammer.dropwizard.util.Generics; | |
public abstract class FlywayBundle<T extends Configuration> implements Bundle, ConfigurationStrategy<T> { | |
@SuppressWarnings("unused") | |
@Override | |
public final void initialize(Bootstrap<?> bootstrap) { | |
Class<T> klass = Generics.getTypeParameter(getClass(), Configuration.class); | |
bootstrap.addCommand(new MigrationCommand<T>(this, klass)); | |
bootstrap.addCommand(new CleanCommand<T>(this, klass)); | |
} | |
@Override | |
public final void run(Environment environment) { | |
// nothing doing | |
} | |
private static class MigrationCommand<T extends Configuration> extends ConfiguredCommand<T> { | |
private final ConfigurationStrategy<T> strategy; | |
private final Class<T> klass; | |
public MigrationCommand(ConfigurationStrategy<T> strategy, Class<T> klass) { | |
super("migrate", "Starts the database migration. All pending migrations will be applied in order."); | |
this.strategy = strategy; | |
this.klass = klass; | |
} | |
@Override | |
protected Class<T> getConfigurationClass() { | |
return klass; | |
} | |
@Override | |
protected void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration) throws Exception { | |
DatabaseConfiguration config = strategy.getDatabaseConfiguration(configuration); | |
Flyway flyway = new Flyway(); | |
flyway.setOutOfOrder(true); | |
flyway.setDataSource(config.getUrl(), config.getUser(), config.getPassword()); | |
MigrationInfoService info = flyway.info(); | |
if (info.applied().length == 0) { | |
flyway.setInitDescription("base"); | |
flyway.setInitVersion("0.5"); | |
flyway.setInitOnMigrate(true); | |
} | |
flyway.migrate(); | |
} | |
} | |
private static class CleanCommand<T extends Configuration> extends ConfiguredCommand<T> { | |
private final ConfigurationStrategy<T> strategy; | |
private final Class<T> klass; | |
public CleanCommand(ConfigurationStrategy<T> strategy, Class<T> klass) { | |
super("destroy", "Drops all objects (tables, views, procedures, triggers, ...) in the configured schemas."); | |
this.strategy = strategy; | |
this.klass = klass; | |
} | |
@Override | |
protected Class<T> getConfigurationClass() { | |
return klass; | |
} | |
@Override | |
protected void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration) throws Exception { | |
DatabaseConfiguration config = strategy.getDatabaseConfiguration(configuration); | |
Flyway flyway = new Flyway(); | |
flyway.setDataSource(config.getUrl(), config.getUser(), config.getPassword()); | |
MigrationInfoService info = flyway.info(); | |
if (info.applied().length == 0) { | |
flyway.setInitDescription("base"); | |
flyway.setInitVersion("0.5"); | |
flyway.setInitOnMigrate(true); | |
} | |
flyway.clean(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment