Search in sources :

Example 6 with MigrationInfo

use of org.flywaydb.core.api.MigrationInfo in project flyway by flyway.

the class FlywayMediumTest method outOfOrder.

@Test
public void outOfOrder() {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_out_of_order;DB_CLOSE_DELAY=-1", "sa", "", null);
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setLocations("migration/sql");
    flyway.setTarget(MigrationVersion.fromVersion("1.2"));
    assertEquals(4, flyway.info().all().length);
    assertEquals(3, flyway.info().pending().length);
    flyway.clean();
    assertEquals(3, flyway.migrate());
    assertEquals("1.2", flyway.info().current().getVersion().getVersion());
    assertEquals(0, flyway.info().pending().length);
    flyway.setTarget(MigrationVersion.LATEST);
    assertEquals(1, flyway.migrate());
    assertEquals("2.0", flyway.info().current().getVersion().getVersion());
    flyway.setLocations("migration/sql", "migration/outoforder");
    assertEquals(5, flyway.info().all().length);
    assertEquals(MigrationState.IGNORED, flyway.info().all()[2].getState());
    flyway.setValidateOnMigrate(false);
    assertEquals(0, flyway.migrate());
    flyway.setValidateOnMigrate(true);
    flyway.setOutOfOrder(true);
    assertEquals(MigrationState.PENDING, flyway.info().all()[4].getState());
    assertEquals(1, flyway.migrate());
    assertEquals("2.0", flyway.info().current().getVersion().getVersion());
    MigrationInfo[] all = flyway.info().all();
    assertEquals(MigrationState.SUCCESS, all[3].getState());
    assertEquals(MigrationState.OUT_OF_ORDER, all[4].getState());
}
Also used : MigrationInfo(org.flywaydb.core.api.MigrationInfo) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 7 with MigrationInfo

use of org.flywaydb.core.api.MigrationInfo in project flyway by flyway.

the class FlywayMediumTest method noDescription.

@Test
public void noDescription() throws Exception {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_no_description;DB_CLOSE_DELAY=-1", "sa", "", null, "SET AUTOCOMMIT OFF");
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setSqlMigrationSeparator(".sql");
    flyway.setSqlMigrationSuffix("");
    flyway.setLocations("migration/no_description");
    flyway.migrate();
    MigrationInfo current = flyway.info().current();
    assertEquals("1.1", current.getVersion().toString());
    assertEquals(MigrationState.SUCCESS, current.getState());
}
Also used : MigrationInfo(org.flywaydb.core.api.MigrationInfo) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 8 with MigrationInfo

use of org.flywaydb.core.api.MigrationInfo in project flyway by flyway.

the class FlywayMediumTest method repeatable.

@Test
public void repeatable() {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_repeatable;DB_CLOSE_DELAY=-1", "sa", "", null);
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setTargetAsString("1.1");
    flyway.setLocations("migration/sql", "migration/repeatable");
    assertEquals(4, flyway.migrate());
    assertEquals(0, flyway.info().pending().length);
    MigrationInfo[] all = flyway.info().all();
    assertEquals(MigrationState.SUCCESS, all[0].getState());
    assertEquals(MigrationState.SUCCESS, all[1].getState());
    assertEquals(MigrationState.SUCCESS, all[2].getState());
    assertEquals(MigrationState.SUCCESS, all[3].getState());
    assertEquals(MigrationState.ABOVE_TARGET, all[4].getState());
    assertEquals(MigrationState.ABOVE_TARGET, all[5].getState());
    flyway.setTarget(MigrationVersion.LATEST);
    flyway.setLocations("migration/sql", "migration/repeatable2");
    all = flyway.info().all();
    assertEquals(MigrationState.SUCCESS, all[0].getState());
    assertEquals(MigrationState.SUCCESS, all[1].getState());
    assertEquals(MigrationState.OUTDATED, all[2].getState());
    assertEquals(MigrationState.OUTDATED, all[3].getState());
    assertEquals(MigrationState.PENDING, all[4].getState());
    assertEquals(MigrationState.PENDING, all[5].getState());
    assertEquals(MigrationState.PENDING, all[6].getState());
    assertEquals(MigrationState.PENDING, all[7].getState());
    assertNotNull(all[0].getVersion());
    assertNotNull(all[1].getVersion());
    assertNull(all[2].getVersion());
    assertNull(all[3].getVersion());
    assertNotNull(all[4].getVersion());
    assertNotNull(all[5].getVersion());
    assertNull(all[6].getVersion());
    assertNull(all[7].getVersion());
    assertEquals(4, flyway.info().pending().length);
    assertEquals(4, flyway.migrate());
    assertEquals(0, flyway.info().pending().length);
    all = flyway.info().all();
    assertEquals(MigrationState.SUCCESS, all[0].getState());
    assertEquals(MigrationState.SUCCESS, all[1].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[2].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[3].getState());
    assertEquals(MigrationState.SUCCESS, all[4].getState());
    assertEquals(MigrationState.SUCCESS, all[5].getState());
    assertEquals(MigrationState.SUCCESS, all[6].getState());
    assertEquals(MigrationState.SUCCESS, all[7].getState());
    assertNotNull(all[0].getVersion());
    assertNotNull(all[1].getVersion());
    assertNull(all[2].getVersion());
    assertNull(all[3].getVersion());
    assertNotNull(all[4].getVersion());
    assertNotNull(all[5].getVersion());
    assertNull(all[6].getVersion());
    assertNull(all[7].getVersion());
    assertEquals(0, flyway.migrate());
    // Revert to original repeatable migrations, which should now be reapplied
    flyway.setLocations("migration/sql", "migration/repeatable");
    all = flyway.info().all();
    assertEquals(MigrationState.SUCCESS, all[0].getState());
    assertEquals(MigrationState.SUCCESS, all[1].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[2].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[3].getState());
    assertEquals(MigrationState.SUCCESS, all[4].getState());
    assertEquals(MigrationState.SUCCESS, all[5].getState());
    assertEquals(MigrationState.OUTDATED, all[6].getState());
    assertEquals(MigrationState.OUTDATED, all[7].getState());
    assertEquals(MigrationState.PENDING, all[8].getState());
    assertEquals(MigrationState.PENDING, all[9].getState());
    assertEquals(2, flyway.migrate());
    assertEquals(0, flyway.info().pending().length);
    all = flyway.info().all();
    assertEquals(MigrationState.SUCCESS, all[0].getState());
    assertEquals(MigrationState.SUCCESS, all[1].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[2].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[3].getState());
    assertEquals(MigrationState.SUCCESS, all[4].getState());
    assertEquals(MigrationState.SUCCESS, all[5].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[6].getState());
    assertEquals(MigrationState.SUPERSEEDED, all[7].getState());
    assertEquals(MigrationState.SUCCESS, all[8].getState());
    assertEquals(MigrationState.SUCCESS, all[9].getState());
    assertNotNull(all[0].getVersion());
    assertNotNull(all[1].getVersion());
    assertNull(all[2].getVersion());
    assertNull(all[3].getVersion());
    assertNotNull(all[4].getVersion());
    assertNotNull(all[5].getVersion());
    assertNull(all[6].getVersion());
    assertNull(all[7].getVersion());
    assertNull(all[8].getVersion());
    assertNull(all[9].getVersion());
}
Also used : MigrationInfo(org.flywaydb.core.api.MigrationInfo) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 9 with MigrationInfo

use of org.flywaydb.core.api.MigrationInfo in project flyway by flyway.

the class SybaseASEMigrationMediumTest method failedMigration.

@Override
public void failedMigration() throws Exception {
    String tableName = "before_the_error";
    flyway.setLocations("migration/dbsupport/sybaseASE/failed");
    Map<String, String> placeholders = new HashMap<String, String>();
    placeholders.put("tableName", dbSupport.quote(tableName));
    flyway.setPlaceholders(placeholders);
    try {
        flyway.migrate();
        fail();
    } catch (FlywaySqlScriptException e) {
        // root cause of exception must be defined, and it should be FlywaySqlScriptException
        assertNotNull(e.getCause());
        assertTrue(e.getCause() instanceof SQLException);
        // and make sure the failed statement was properly recorded
        // It is line 22 as a go statement is added for Sybase
        assertEquals(22, e.getLineNumber());
        String statement = e.getStatement();
        if (statement.indexOf('\n') != -1) {
            statement = statement.replace("\n", "");
        }
        assertEquals("THIS IS NOT VALID SQL", statement);
    }
    MigrationInfo migration = flyway.info().current();
    assertEquals(dbSupport.supportsDdlTransactions(), !dbSupport.getSchema(dbSupport.getCurrentSchemaName()).getTable(tableName).exists());
    if (dbSupport.supportsDdlTransactions()) {
        assertNull(migration);
    } else {
        MigrationVersion version = migration.getVersion();
        assertEquals("1", version.toString());
        assertEquals("Should Fail", migration.getDescription());
        assertEquals(MigrationState.FAILED, migration.getState());
        assertEquals(1, flyway.info().applied().length);
    }
}
Also used : MigrationInfo(org.flywaydb.core.api.MigrationInfo) MigrationVersion(org.flywaydb.core.api.MigrationVersion) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) FlywaySqlScriptException(org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)

Example 10 with MigrationInfo

use of org.flywaydb.core.api.MigrationInfo in project openremote by openremote.

the class PersistenceService method prepareSchema.

protected void prepareSchema(String connectionUrl, String databaseUsername, String databasePassword) {
    LOG.fine("Preparing database schema");
    flyway = new Flyway();
    flyway.setDataSource(connectionUrl, databaseUsername, databasePassword);
    flyway.setSchemas(SCHEMA_NAME);
    List<String> locations = new ArrayList<>();
    appendSchemaLocations(locations);
    flyway.setLocations(locations.toArray(new String[locations.size()]));
    if (forceClean) {
        LOG.warning("!!! Cleaning database !!!");
        flyway.clean();
    } else {
        LOG.fine("Not cleaning, using existing database");
    }
    for (MigrationInfo i : flyway.info().pending()) {
        LOG.info("Pending task: " + i.getVersion() + ", " + i.getDescription() + ", " + i.getScript());
    }
    int applied = flyway.migrate();
    LOG.info("Applied database schema migrations: " + applied);
    flyway.validate();
}
Also used : Flyway(org.flywaydb.core.Flyway) MigrationInfo(org.flywaydb.core.api.MigrationInfo) ArrayList(java.util.ArrayList) MapAccess.getString(org.openremote.container.util.MapAccess.getString)

Aggregations

MigrationInfo (org.flywaydb.core.api.MigrationInfo)29 Test (org.junit.Test)14 MigrationVersion (org.flywaydb.core.api.MigrationVersion)10 Flyway (org.flywaydb.core.Flyway)5 DriverDataSource (org.flywaydb.core.internal.util.jdbc.DriverDataSource)5 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 MigrationInfoService (org.flywaydb.core.api.MigrationInfoService)4 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)3 MigrationInfoImpl (org.flywaydb.core.internal.info.MigrationInfoImpl)3 ArrayList (java.util.ArrayList)2 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)2 AppliedMigration (org.flywaydb.core.internal.schemahistory.AppliedMigration)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 PrintWriter (java.io.PrintWriter)1 Connection (java.sql.Connection)1 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1 DataSource (javax.sql.DataSource)1 MigrationState (org.flywaydb.core.api.MigrationState)1