FlywaySchemaInitializer implements SchemaInitializer { @Override public Map<String, String> init() throws SchemaInitializationException { final Map<String, String> initResult = new HashMap<>(); try (final Connection conn = dataSource.getConnection()) { final Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setLocations(locations); flyway.setClassLoader(Thread.currentThread().getContextClassLoader()); final DbSupport dbSupport = DbSupportFactory.createDbSupport(conn, true); final MetaDataTable mt = new MetaDataTableImpl( dbSupport, dbSupport.getOriginalSchema().getTable(flyway.getTable()), flyway.getInstalledBy()); initResult.put(BARE_DB_INIT_PROPERTY_NAME, String.valueOf(!mt.hasAppliedMigrations())); final String productName = conn.getMetaData().getDatabaseProductName().toLowerCase(); flyway.setResolvers( new CustomSqlMigrationResolver(productName, dbSupport, placeholderReplacer)); flyway.setSkipDefaultResolvers(true); flyway.setBaselineOnMigrate(baselineOnMigrate); if (baselineOnMigrate) { flyway.setBaselineVersionAsString(baselineVersion); } flyway.setSqlMigrationSeparator(versionSeparator); flyway.setSqlMigrationSuffix(scriptsSuffix); flyway.setSqlMigrationPrefix(scriptsPrefix); flyway.migrate(); } catch (SQLException | RuntimeException x) { throw new SchemaInitializationException(x.getLocalizedMessage(), x); } return initResult; } @Inject  FlywaySchemaInitializer(
      @Named("db.schema.flyway.scripts.locations") String[] scriptsLocations,
      @Named("db.schema.flyway.scripts.prefix") String scriptsPrefix,
      @Named("db.schema.flyway.scripts.suffix") String scriptsSuffix,
      @Named("db.schema.flyway.scripts.version_separator") String versionSeparator,
      @Named("db.schema.flyway.baseline.enabled") boolean baselineOnMigrate,
      @Named("db.schema.flyway.baseline.version") String baselineVersion,
      DataSource dataSource,
      PlaceholderReplacer placeholderReplacer);  FlywaySchemaInitializer(DataSource dataSource, String... locations); @Override Map<String, String> init();  }
@Test public void initializesSchemaWhenDatabaseIsEmpty() throws Exception { createScript("1.0/1__init.sql", "CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));"); createScript( "1.0/2__add_data.sql", "INSERT INTO test VALUES(1, 'test1');" + "INSERT INTO test VALUES(2, 'test2');" + "INSERT INTO test VALUES(3, 'test3');"); createScript("2.0/1__add_more_data.sql", "INSERT INTO test VALUES(4, 'test4');"); createScript( "2.0/postgresql/1__add_more_data.sql", "INSERT INTO test VALUES(4, 'postgresql-data');"); final SchemaInitializer initializer = FlywayInitializerBuilder.from(dataSource).build(); initializer.init(); assertEquals( queryEntities(), Sets.newHashSet( new TestEntity(1, "test1"), new TestEntity(2, "test2"), new TestEntity(3, "test3"), new TestEntity(4, "test4"))); initializer.init(); }
@Test(expectedExceptions = SchemaInitializationException.class) public void failsIfBaseLineIsNotConfiguredProperly() throws Exception { execQuery( "CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));" + "INSERT INTO test VALUES(1, 'test1');" + "INSERT INTO test VALUES(2, 'test2');" + "INSERT INTO test VALUES(3, 'test3');"); createScript("1.0/1__init.sql", "CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));"); FlywayInitializerBuilder.from(dataSource) .setBaselineOnMigrate(true) .setBaselineVersion("1.0") .build() .init(); }
@Test public void executesOnlyThoseMigrationsWhichGoAfterBaseline() throws Exception { execQuery("CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));"); createScript("1.0/1__init.sql", "CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));"); createScript( "2.0/1__add_data.sql", "INSERT INTO test VALUES(1, 'test1');" + "INSERT INTO test VALUES(2, 'test2');" + "INSERT INTO test VALUES(3, 'test3');"); final FlywaySchemaInitializer initializer = FlywayInitializerBuilder.from(dataSource) .setBaselineOnMigrate(true) .setBaselineVersion("1.0.1") .build(); initializer.init(); assertEquals( queryEntities(), Sets.newHashSet( new TestEntity(1, "test1"), new TestEntity(2, "test2"), new TestEntity(3, "test3"))); initializer.init(); }
@Test public void initializesSchemaWhenDatabaseIsEmptyAndBaselineIsConfigured() throws Exception { createScript("1.0/1__init.sql", "CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));"); createScript( "2.0/1__add_data.sql", "INSERT INTO test VALUES(1, 'test1');" + "INSERT INTO test VALUES(2, 'test2');" + "INSERT INTO test VALUES(3, 'test3');"); final FlywaySchemaInitializer initializer = FlywayInitializerBuilder.from(dataSource) .setBaselineOnMigrate(true) .setBaselineVersion("1.0.1") .build(); initializer.init(); assertEquals( queryEntities(), Sets.newHashSet( new TestEntity(1, "test1"), new TestEntity(2, "test2"), new TestEntity(3, "test3"))); initializer.init(); }
@Test public void selectsProviderSpecificScriptsInPreferenceToDefaultOnes() throws Exception { createScript("1.0/1__init.sql", "CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));"); createScript("2.0/1__add_data.sql", "INSERT INTO test VALUES(1, 'default data');"); createScript("2.0/h2/1__add_data.sql", "INSERT INTO test VALUES(1, 'h2 data');"); final FlywaySchemaInitializer initializer = FlywayInitializerBuilder.from(dataSource).build(); initializer.init(); assertEquals(queryEntities(), Sets.newHashSet(new TestEntity(1, "h2 data"))); initializer.init(); }
@Test public void replacesVariablesWhenPlaceholderReplacerIsConfigured() throws Exception { createScript( "1.0/1__init.sql", "CREATE TABLE test (id INT, text TEXT, PRIMARY KEY (id));" + "INSERT INTO test VALUES(1, '${variable}');"); FlywayInitializerBuilder.from(dataSource) .setReplacer(new PlaceholderReplacer(ImmutableMap.of("variable", "test"), "${", "}")) .build() .init(); assertEquals(queryEntities(), Sets.newHashSet(new TestEntity(1, "test"))); }