@Test public void multipleSetDataSourceCalls() throws Exception { DriverDataSource dataSource1 = new DriverDataSource(null, "jdbc:h2:mem:flyway_db_1;DB_CLOSE_DELAY=-1", "sa", ""); DriverDataSource dataSource2 = new DriverDataSource(null, "jdbc:h2:mem:flyway_db_2;DB_CLOSE_DELAY=-1", "sa", ""); Connection connection1 = dataSource1.getConnection(); Connection connection2 = dataSource2.getConnection(); Schema schema1 = new H2DbSupport(connection1).getSchema("PUBLIC"); Schema schema2 = new H2DbSupport(connection2).getSchema("PUBLIC"); assertTrue(schema1.empty()); assertTrue(schema2.empty()); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource1); flyway.setDataSource(dataSource2); flyway.setLocations("migration/sql"); flyway.migrate(); assertTrue(schema1.empty()); assertFalse(schema2.empty()); connection1.close(); connection2.close(); }
@Test public void emptyLocations() { Flyway flyway = new Flyway(); flyway.setDataSource("jdbc:h2:mem:flyway_empty;DB_CLOSE_DELAY=-1", "sa", ""); flyway.setLocations("migration/empty"); assertEquals(0, flyway.migrate()); // Used to fail with exception due to non-empty schema and empty metadata table. assertEquals(0, flyway.migrate()); }
@Test public void noConnectionLeak() { OpenConnectionCountDriverDataSource dataSource = new OpenConnectionCountDriverDataSource(); assertEquals(0, dataSource.getOpenConnectionCount()); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setLocations("migration/sql"); flyway.clean(); assertEquals(0, dataSource.getOpenConnectionCount()); assertEquals(4, flyway.migrate()); assertEquals(0, dataSource.getOpenConnectionCount()); }
@Test(expected = FlywayException.class) public void initAgainWithDifferentVersion() throws Exception { DriverDataSource dataSource = new DriverDataSource( null, "jdbc:h2:mem:flyway_db_init_different;DB_CLOSE_DELAY=-1", "sa", ""); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.init(); flyway.setInitVersion("2"); flyway.init(); }
@Test public void failed() { StringLogCreator logCreator = new StringLogCreator(); LogFactory.setLogCreator(logCreator); try { Flyway flyway = new Flyway(); flyway.setDataSource("jdbc:h2:mem:flyway_failed;DB_CLOSE_DELAY=-1", "sa", ""); flyway.setLocations("migration/failed"); flyway.migrate(); fail(); } catch (FlywayException e) { System.out.println(logCreator.getOutput()); } finally { LogFactory.setLogCreator(null); } }
@Test public void noConnectionLeakWithException() { OpenConnectionCountDriverDataSource dataSource = new OpenConnectionCountDriverDataSource(); assertEquals(0, dataSource.getOpenConnectionCount()); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setLocations("migration/failed"); try { flyway.clean(); assertEquals(0, dataSource.getOpenConnectionCount()); flyway.migrate(); fail(); } catch (FlywayException e) { // Expected -> Ignore } assertEquals(0, dataSource.getOpenConnectionCount()); }
@Test public void initOnMigrateOnCleanOnValidate() throws Exception { DriverDataSource dataSource = new DriverDataSource( null, "jdbc:h2:mem:flyway_db_init_validate;DB_CLOSE_DELAY=-1", "sa", ""); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setSchemas("new1"); flyway.setLocations("migration/validate"); flyway.migrate(); flyway.setCleanOnValidationError(true); flyway.setValidateOnMigrate(true); flyway.setSqlMigrationPrefix("CheckValidate"); flyway.migrate(); assertEquals("1", flyway.info().current().getVersion().toString()); }
@Test public void futureMigrations() { Flyway flyway = new Flyway(); flyway.setDataSource("jdbc:h2:mem:flyway_future;DB_CLOSE_DELAY=-1", "sa", ""); flyway.setLocations("migration/sql"); flyway.migrate(); flyway.setLocations("migration/empty"); assertEquals(MigrationState.FUTURE_SUCCESS, flyway.info().applied()[0].getState()); }
@Test public void infoInit() throws Exception { DriverDataSource dataSource = new DriverDataSource(null, "jdbc:h2:mem:flyway_db_info_init;DB_CLOSE_DELAY=-1", "sa", ""); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.init(); flyway.setLocations(); assertEquals(1, flyway.info().all().length); assertEquals("1", flyway.info().current().getVersion().toString()); assertEquals(MigrationState.SUCCESS, flyway.info().current().getState()); }
@Test public void repairFirst() throws Exception { DriverDataSource dataSource = new DriverDataSource(null, "jdbc:h2:mem:flyway_db_repair;DB_CLOSE_DELAY=-1", "sa", ""); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setLocations("migration/failed"); assertEquals(1, flyway.info().all().length); try { flyway.migrate(); } catch (FlywayException e) { // Should happen } assertEquals("1", flyway.info().current().getVersion().toString()); assertEquals(MigrationState.FAILED, flyway.info().current().getState()); flyway.repair(); assertNull(flyway.info().current()); }
@Test public void initAgainWithSameVersion() throws Exception { DriverDataSource dataSource = new DriverDataSource(null, "jdbc:h2:mem:flyway_db_init_same;DB_CLOSE_DELAY=-1", "sa", ""); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setLocations("migration/sql"); flyway.setInitVersion("0.5"); flyway.init(); flyway.init(); assertEquals(1, flyway.info().applied().length); MigrationInfo current = flyway.info().current(); assertEquals("0.5", current.getVersion().toString()); assertEquals(MigrationType.INIT, current.getType()); assertEquals(MigrationState.SUCCESS, current.getState()); }
@Test public void info() throws Exception { DriverDataSource dataSource = new DriverDataSource(null, "jdbc:h2:mem:flyway_db_info;DB_CLOSE_DELAY=-1", "sa", null); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setLocations("migration/sql"); assertEquals(4, flyway.info().all().length); assertEquals(4, flyway.info().pending().length); flyway.setTarget(new MigrationVersion("1.1")); assertEquals(4, flyway.info().all().length); assertEquals(2, flyway.info().pending().length); assertEquals(MigrationState.ABOVE_TARGET, flyway.info().all()[2].getState()); assertEquals(MigrationState.ABOVE_TARGET, flyway.info().all()[3].getState()); flyway.migrate(); assertEquals("1.1", flyway.info().current().getVersion().toString()); assertEquals(MigrationState.SUCCESS, flyway.info().current().getState()); assertEquals(4, flyway.info().all().length); assertEquals(0, flyway.info().pending().length); flyway.setTarget(MigrationVersion.LATEST); assertEquals(4, flyway.info().all().length); assertEquals(2, flyway.info().pending().length); flyway.migrate(); assertEquals("2.0", flyway.info().current().getVersion().toString()); assertEquals(MigrationState.SUCCESS, flyway.info().current().getState()); assertEquals(4, flyway.info().all().length); assertEquals(0, flyway.info().pending().length); }
@Test public void outOfOrder() { DriverDataSource dataSource = new DriverDataSource(null, "jdbc:h2:mem:flyway_out_of_order;DB_CLOSE_DELAY=-1", "sa", ""); Flyway flyway = new Flyway(); flyway.setDataSource(dataSource); flyway.setLocations("migration/sql"); flyway.setTarget(new MigrationVersion("1.2")); assertEquals(4, flyway.info().all().length); assertEquals(3, flyway.info().pending().length); flyway.clean(); assertEquals(3, flyway.migrate()); assertEquals(0, flyway.info().pending().length); flyway.setLocations("migration/sql", "migration/outoforder"); assertEquals(5, flyway.info().all().length); assertEquals(MigrationState.IGNORED, flyway.info().all()[2].getState()); assertEquals(0, flyway.migrate()); flyway.setTarget(MigrationVersion.LATEST); flyway.setOutOfOrder(true); assertEquals(MigrationState.PENDING, flyway.info().all()[2].getState()); assertEquals(2, flyway.migrate()); assertEquals(MigrationState.OUT_OF_ORDER, flyway.info().all()[2].getState()); assertEquals(MigrationState.SUCCESS, flyway.info().all()[4].getState()); }