Search in sources :

Example 6 with MigrationVersion

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

the class DB2zOSMigrationMediumTest method failedMigration.

/**
     * Override failedMigration. Setting schema and table space in SQL.
     *
     * @throws Exception
     */
@Override
@Test
public void failedMigration() throws Exception {
    String tableName = "before_the_error";
    flyway.setLocations(getBasedir() + "/failed");
    Map<String, String> placeholders = new HashMap<String, String>();
    placeholders.put("tableName", dbSupport.quote(tableName));
    flyway.setPlaceholders(placeholders);
    flyway.baseline();
    try {
        flyway.migrate();
        fail();
    } catch (FlywaySqlScriptException e) {
        System.out.println(e.getMessage());
        // 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
        assertEquals(23, e.getLineNumber());
        assertEquals("THIS IS NOT VALID SQL", e.getStatement());
    }
    MigrationInfo migration = flyway.info().current();
    assertEquals(dbSupport.supportsDdlTransactions(), !dbSupport.getSchema(dbSupport.getCurrentSchemaName()).getTable(tableName).exists());
    if (dbSupport.supportsDdlTransactions()) {
        assertTrue(migration.getType().toString() == "BASELINE");
    } else {
        MigrationVersion version = migration.getVersion();
        assertEquals("1", version.toString());
        assertEquals("Should Fail", migration.getDescription());
        assertEquals(MigrationState.FAILED, migration.getState());
        // With schema markers, we'll have 2 applied
        if (flyway.info().applied()[0].getType() == MigrationType.SCHEMA) {
            assertEquals(2, flyway.info().applied().length);
        } else {
            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) Test(org.junit.Test)

Example 7 with MigrationVersion

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

the class EnterpriseDBMigrationMediumTest method migrationsWithPlaceholders.

/**
     * Tests migrations containing placeholders.
     */
@Test
public void migrationsWithPlaceholders() throws Exception {
    int countUserObjects1 = jdbcTemplate.queryForInt("SELECT count(*) FROM user_objects");
    Map<String, String> placeholders = new HashMap<String, String>();
    placeholders.put("tableName", "test_user");
    flyway.setPlaceholders(placeholders);
    flyway.setLocations("migration/dbsupport/enterprisedb/sql/placeholders");
    flyway.migrate();
    MigrationVersion version = flyway.info().current().getVersion();
    assertEquals("1.1", version.toString());
    assertEquals("Populate table", flyway.info().current().getDescription());
    assertEquals("Mr. T triggered", jdbcTemplate.queryForString("select name from test_user"));
    flyway.clean();
    int countUserObjects2 = jdbcTemplate.queryForInt("SELECT count(*) FROM user_objects");
    assertEquals(countUserObjects1, countUserObjects2);
    MigrationInfo[] migrationInfos = flyway.info().applied();
    for (MigrationInfo migrationInfo : migrationInfos) {
        assertNotNull(migrationInfo.getScript() + " has no checksum", migrationInfo.getChecksum());
    }
}
Also used : MigrationInfo(org.flywaydb.core.api.MigrationInfo) MigrationVersion(org.flywaydb.core.api.MigrationVersion) Test(org.junit.Test)

Example 8 with MigrationVersion

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

the class H2MigrationMediumTest method domain.

@Test
public void domain() throws Exception {
    flyway.setLocations("migration/dbsupport/h2/sql/domain");
    flyway.migrate();
    MigrationVersion version = flyway.info().current().getVersion();
    assertEquals("1", version.toString());
    assertEquals("Domain", flyway.info().current().getDescription());
    assertEquals("axel@spam.la", jdbcTemplate.queryForString("select address from test_user where name = 'Axel'"));
    flyway.clean();
    flyway.migrate();
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) Test(org.junit.Test)

Example 9 with MigrationVersion

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

the class DbBaselineTest method differentBaselineMarkerVersionPresent.

@Test
public void differentBaselineMarkerVersionPresent() {
    // arrange
    MigrationVersion baselineVersion = MigrationVersion.fromVersion("3.0.0");
    AppliedMigration baseline = new AppliedMigration(baselineVersion, TEST_BASELINE_DESCRIPTION, MigrationType.BASELINE, "V2.0.0__test-migration.sql", 12345, 100, true);
    when(this.metaDataTable.hasBaselineMarker()).thenReturn(true);
    when(this.metaDataTable.getBaselineMarker()).thenReturn(baseline);
    // assert
    this.expectedException.expect(FlywayException.class);
    this.expectedException.expectMessage(TEST_BASELINE_VERSION.toString());
    this.expectedException.expectMessage(TEST_BASELINE_DESCRIPTION.toString());
    this.expectedException.expectMessage(baselineVersion.toString());
    // act
    this.testBaseline.baseline();
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) Test(org.junit.Test)

Example 10 with MigrationVersion

use of org.flywaydb.core.api.MigrationVersion 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)

Aggregations

MigrationVersion (org.flywaydb.core.api.MigrationVersion)40 Test (org.junit.Test)27 SQLException (java.sql.SQLException)9 MigrationInfo (org.flywaydb.core.api.MigrationInfo)8 FlywayException (org.flywaydb.core.api.FlywayException)7 HashMap (java.util.HashMap)6 AppliedMigration (org.flywaydb.core.internal.metadatatable.AppliedMigration)6 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)6 MigrationType (org.flywaydb.core.api.MigrationType)4 ArrayList (java.util.ArrayList)3 MigrationChecksumProvider (org.flywaydb.core.api.migration.MigrationChecksumProvider)3 MigrationInfoProvider (org.flywaydb.core.api.migration.MigrationInfoProvider)3 MigrationExecutor (org.flywaydb.core.api.resolver.MigrationExecutor)3 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)3 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)3 Date (java.util.Date)2 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)2 PlaceholderReplacer (org.flywaydb.core.internal.util.PlaceholderReplacer)2 StopWatch (org.flywaydb.core.internal.util.StopWatch)2 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)2