io.dropwizard.migrations.CloseableLiquibase Java Examples

The following examples show how to use io.dropwizard.migrations.CloseableLiquibase. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DbDiffCommand.java    From dropwizard-experiment with MIT License 7 votes vote down vote up
@Override
protected void run(Bootstrap<T> bootstrap, Namespace namespace, T configuration) throws Exception {
    // The existing database with migrations managed by Liquibase.
    DataSourceFactory outdatedDb = configuration.getDatabaseConfig();

    try (CloseableLiquibase outdatedLiquibase = createLiquibase(outdatedDb)) {
        // A temporary database that starts out empty and then gets the autogenerated Ebean table definitions applied.
        DataSourceFactory freshDb = EbeanConfigUtils.clone(outdatedDb);
        String url = outdatedDb.getUrl();
        freshDb.setUrl(url.substring(0, url.lastIndexOf("/")) + "/migrationdiff");

        // Creating the Ebean server makes it apply its table definitions to the database immediately.
        ServerConfig serverConfig = EbeanConfigUtils.createServerConfig(freshDb);
        serverConfig.setDdlGenerate(true);
        serverConfig.setDdlRun(true);
        EbeanServer ebeanServer = EbeanServerFactory.create(serverConfig);

        try (CloseableLiquibase freshLiquibase = createLiquibase(freshDb)) {
            // Create and print the differences between the two databases, i.e. a migration that should be applied to update to the newest Ebean definitions.
            DiffResult diff = outdatedLiquibase.diff(freshLiquibase.getDatabase(), outdatedLiquibase.getDatabase(), CompareControl.STANDARD);
            DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diff, new DiffOutputControl(false, false, true));
            diffToChangeLog.print(System.out);
        }
    }
}
 
Example #2
Source File: EbeanBundle.java    From dropwizard-experiment with MIT License 6 votes vote down vote up
private static void applyMigrations(DataSourceFactory dbConfig, MetricRegistry metrics) throws Exception {
    Stopwatch migrationsTimer = Stopwatch.createStarted();

    // Borrowed from AbstractLiquibaseCommand.
    DataSourceFactory lbConfig = EbeanConfigUtils.clone(dbConfig);
    lbConfig.setMaxSize(1);
    lbConfig.setMinSize(1);
    lbConfig.setInitialSize(1);

    try (CloseableLiquibase liquibase = new CloseableLiquibase(dbConfig.build(metrics, "liquibase"))) {
        log.info("Checking for database migrations.");
        liquibase.update("");

        migrationsTimer.stop();
        metrics.timer(MetricRegistry.name(EbeanBundle.class, "migrations")).update(migrationsTimer.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS);
        log.info("Database migrations complete in {} ms.", migrationsTimer.elapsed(TimeUnit.MILLISECONDS));
    } catch (ValidationFailedException e) {
        e.printDescriptiveError(System.err);
        throw e;
    }
}
 
Example #3
Source File: JdbiStore.java    From breakerbox with Apache License 2.0 5 votes vote down vote up
@Override
public boolean initialize() {
    try (CloseableLiquibase liquibase = new CloseableLiquibaseWithClassPathMigrationsFile(configuration
            .getDataSourceFactory()
            .build(metricRegistry, "liquibase"), MIGRATIONS_FILENAME)) {
        liquibase.update("");
        return true;
    } catch (Exception err) {
        LOGGER.error("Failed to create liquibase", err);
        throw new IllegalStateException(err);
    }
}
 
Example #4
Source File: DbDiffCommand.java    From dropwizard-experiment with MIT License 4 votes vote down vote up
private static CloseableLiquibase createLiquibase(DataSourceFactory dbConfig) throws SQLException, LiquibaseException {
    ManagedDataSource dataSource = dbConfig.build(new MetricRegistry(), "liquibase");
    return new CloseableLiquibase(dataSource);
}