Search in sources :

Example 1 with FlywaySqlScriptException

use of org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException 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 2 with FlywaySqlScriptException

use of org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException 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 3 with FlywaySqlScriptException

use of org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException in project flyway by flyway.

the class MigrationTestCase method failedMigration.

@Test
public void failedMigration() throws Exception {
    String tableName = "before_the_error";
    flyway.setLocations(getMigrationDir() + "/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) {
        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(21, 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()) {
        assertNull(migration);
    } 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)

Aggregations

SQLException (java.sql.SQLException)3 HashMap (java.util.HashMap)3 MigrationInfo (org.flywaydb.core.api.MigrationInfo)3 MigrationVersion (org.flywaydb.core.api.MigrationVersion)3 FlywaySqlScriptException (org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException)3 Test (org.junit.Test)2