@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); } } }
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; } }
@Override public void run(T configuration, Environment environment) throws Exception { CloseableLiquibase liquibase = new CloseableLiquibaseWithClassPathMigrationsFile(getDataSourceFactory(configuration) .build(environment.metrics(), name()), getMigrationsFileName()); liquibase.update(""); }
@Override public boolean initialize() throws Exception { try (CloseableLiquibase liquibase = new CloseableLiquibase(configuration .getDataSourceFactory() .build(new MetricRegistry(), "liquibase"))) { liquibase.update(""); return true; } catch (Exception err) { LOGGER.error("Failed to create liquibase", err); throw new IllegalStateException(err); } }
@Before public void setupH2Test() throws Exception { liquibase = new CloseableLiquibase(hsqlConfig .getDataSourceFactory() .build(new MetricRegistry(), "liquibase")); liquibase.update(""); database = new DBIFactory().build(environment(), hsqlConfig.getDataSourceFactory(), "h2test"); database.registerArgumentFactory(new DependencyIdArgumentFactory()); database.registerArgumentFactory(new ServiceIdArgumentFactory()); database.registerArgumentFactory(new TenacityConfigurationArgumentFactory(Jackson.newObjectMapper())); database.registerArgumentFactory(new DateTimeArgumentFactory()); }
@Override public void start() throws Exception { LOGGER.info("begin migration"); final ManagedDataSource dataSource = dataSourceFactory.build(new MetricRegistry(), "liquibase"); try(CloseableLiquibase liquibase = new CloseableLiquibaseWithClassPathMigrationsFile(dataSource, "migrations.xml")) { liquibase.update("migrations"); } LOGGER.info("finish migration"); }
@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); } }
CloseableLiquibase openLiquibase(DataSourceFactory dataSourceFactory, Namespace namespace) throws ClassNotFoundException, SQLException, LiquibaseException { final ManagedDataSource dataSource = dataSourceFactory.build(new MetricRegistry(), "liquibase"); final String migrationsFile = (String) namespace.get("migrations-file"); if (migrationsFile == null) { return new CloseableLiquibase(dataSource); } LOGGER.warn("Open Liquibase with migrations-file: {}", migrationsFile ); return new CloseableLiquibase(dataSource, migrationsFile); }
private static CloseableLiquibase createLiquibase(DataSourceFactory dbConfig) throws SQLException, LiquibaseException { ManagedDataSource dataSource = dbConfig.build(new MetricRegistry(), "liquibase"); return new CloseableLiquibase(dataSource); }