Search in sources :

Example 51 with FlywayException

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

the class FlywayMediumTest method repairFirst.

@Test
public void repairFirst() throws Exception {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_repair;DB_CLOSE_DELAY=-1", "sa", "", null, "SET AUTOCOMMIT OFF");
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    Map<String, String> placeholders = new HashMap<String, String>();
    placeholders.put("tableName", "aaa");
    flyway.setPlaceholders(placeholders);
    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());
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 52 with FlywayException

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

the class FlywayMediumTest method repeatableFailed.

@Test
public void repeatableFailed() {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_repeatable_failed;DB_CLOSE_DELAY=-1", "sa", "", null);
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setLocations("migration/repeatable_failed");
    try {
        flyway.migrate();
        fail();
    } catch (FlywayException e) {
        assertEquals(e.getMessage(), MigrationState.FAILED, flyway.info().current().getState());
    }
    try {
        flyway.validate();
        fail();
    } catch (FlywayException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("failed repeatable migration"));
    }
    flyway.setLocations("migration/repeatable");
    try {
        flyway.migrate();
        fail();
    } catch (FlywayException e) {
        assertTrue(e.getMessage(), e.getMessage().contains("failed repeatable migration"));
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 53 with FlywayException

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

the class FlywayMediumTest method cleanDisabled.

@Test(expected = FlywayException.class)
public void cleanDisabled() throws Exception {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_clean_disabled;DB_CLOSE_DELAY=-1", "sa", "", null);
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    try {
        flyway.clean();
    } catch (FlywayException e) {
        fail("clean should succeed when cleanDisabled is false");
    }
    flyway.setCleanDisabled(true);
    flyway.clean();
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 54 with FlywayException

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

the class DbSupportFactory method getDatabaseProductName.

/**
     * Retrieves the name of the database product.
     *
     * @param connection The connection to use to query the database.
     * @return The name of the database product. Ex.: Oracle, MySQL, ...
     */
private static String getDatabaseProductName(Connection connection) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData == null) {
            throw new FlywayException("Unable to read database metadata while it is null!");
        }
        String databaseProductName = databaseMetaData.getDatabaseProductName();
        if (databaseProductName == null) {
            throw new FlywayException("Unable to determine database. Product name is null.");
        }
        int databaseMajorVersion = databaseMetaData.getDatabaseMajorVersion();
        int databaseMinorVersion = databaseMetaData.getDatabaseMinorVersion();
        return databaseProductName + " " + databaseMajorVersion + "." + databaseMinorVersion;
    } catch (SQLException e) {
        throw new FlywaySqlException("Error while determining database product name", e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 55 with FlywayException

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

the class DbSupportFactory method getDriverName.

/**
     * Retrieves the name of the JDBC driver
     *
     * @param connection The connection to use to query the database.
     * @return The name of the driver. Ex: RedshiftJDBC
     */
private static String getDriverName(Connection connection) {
    try {
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        if (databaseMetaData == null) {
            throw new FlywayException("Unable to read database metadata while it is null!");
        }
        String driverName = databaseMetaData.getDriverName();
        if (driverName == null) {
            throw new FlywayException("Unable to determine JDBC  driver name. JDBC driver name is null.");
        }
        return driverName;
    } catch (SQLException e) {
        throw new FlywaySqlException("Error while determining JDBC driver name", e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

FlywayException (org.flywaydb.core.api.FlywayException)82 SQLException (java.sql.SQLException)22 IOException (java.io.IOException)17 ArrayList (java.util.ArrayList)14 PreparedStatement (java.sql.PreparedStatement)8 HashMap (java.util.HashMap)8 URL (java.net.URL)7 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)7 File (java.io.File)6 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)6 Test (org.junit.Test)6 InputStreamReader (java.io.InputStreamReader)5 Method (java.lang.reflect.Method)5 BufferedReader (java.io.BufferedReader)4 FileInputStream (java.io.FileInputStream)4 Reader (java.io.Reader)4 StringReader (java.io.StringReader)4 URLClassLoader (java.net.URLClassLoader)4