Search in sources :

Example 11 with FlywayException

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

the class FileSystemResource method loadAsString.

/**
     * Loads this resource as a string.
     *
     * @param encoding The encoding to use.
     * @return The string contents of the resource.
     */
public String loadAsString(String encoding) {
    try {
        InputStream inputStream = new FileInputStream(location);
        Reader reader = new InputStreamReader(inputStream, Charset.forName(encoding));
        return FileCopyUtils.copyToString(reader);
    } catch (IOException e) {
        throw new FlywayException("Unable to load filesystem resource: " + location.getPath() + " (encoding: " + encoding + ")", e);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 12 with FlywayException

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

the class TransactionTemplate method execute.

/**
     * Executes this callback within a transaction.
     *
     * @param transactionCallback The callback to execute.
     * @return The result of the transaction code.
     */
public <T> T execute(Callable<T> transactionCallback) {
    boolean oldAutocommit = true;
    try {
        oldAutocommit = connection.getAutoCommit();
        connection.setAutoCommit(false);
        T result = transactionCallback.call();
        connection.commit();
        return result;
    } catch (SQLException e) {
        throw new FlywaySqlException("Unable to commit transaction", e);
    } catch (Exception e) {
        Savepoint savepoint = null;
        RuntimeException rethrow;
        if (e instanceof RollbackWithSavepointException) {
            savepoint = ((RollbackWithSavepointException) e).getSavepoint();
            rethrow = (RuntimeException) e.getCause();
        } else {
            if (e instanceof RuntimeException) {
                rethrow = (RuntimeException) e;
            } else {
                rethrow = new FlywayException(e);
            }
        }
        if (rollbackOnException) {
            try {
                LOG.debug("Rolling back transaction...");
                if (savepoint == null) {
                    connection.rollback();
                } else {
                    connection.rollback(savepoint);
                }
                LOG.debug("Transaction rolled back");
            } catch (SQLException se) {
                LOG.error("Unable to rollback transaction", se);
            }
        } else {
            try {
                connection.commit();
            } catch (SQLException se) {
                LOG.error("Unable to commit transaction", se);
            }
        }
        throw rethrow;
    } finally {
        try {
            connection.setAutoCommit(oldAutocommit);
        } catch (SQLException e) {
            LOG.error("Unable to restore autocommit to original value for connection", e);
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) SQLException(java.sql.SQLException) Savepoint(java.sql.Savepoint) SQLException(java.sql.SQLException) FlywayException(org.flywaydb.core.api.FlywayException) FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException)

Example 13 with FlywayException

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

the class FlywayMediumTest method repairDescription.

@Test
public void repairDescription() throws Exception {
    DriverDataSource dataSource = new DriverDataSource(Thread.currentThread().getContextClassLoader(), null, "jdbc:h2:mem:flyway_db_repair_description;DB_CLOSE_DELAY=-1", "sa", "", null, "SET AUTOCOMMIT OFF");
    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setLocations("migration/quote");
    assertEquals(1, flyway.migrate());
    // Switch out V1 for a different migration with a new description and checksum
    flyway.setLocations("migration/placeholder");
    try {
        flyway.validate();
        fail();
    } catch (FlywayException e) {
    //Should happen
    }
    flyway.repair();
    assertEquals(0, flyway.migrate());
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DriverDataSource(org.flywaydb.core.internal.util.jdbc.DriverDataSource) Test(org.junit.Test)

Example 14 with FlywayException

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

the class FlywayMediumTest method failed.

@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);
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) StringLogCreator(org.flywaydb.core.internal.util.logging.StringLogCreator) Test(org.junit.Test)

Example 15 with FlywayException

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

the class H2TriggerExceptionSmallTest method triggerException.

@Test
public void triggerException() {
    Flyway flyway = new Flyway();
    flyway.setDataSource("jdbc:h2:mem:flyway_db_trigger;DB_CLOSE_DELAY=-1", "sa", "");
    flyway.setLocations("org.flywaydb.core.internal.dbsupport.h2");
    try {
        flyway.migrate();
        fail();
    } catch (FlywayException e) {
        assertEquals("Expected", ExceptionUtils.getRootCause(e).getMessage());
    }
}
Also used : Flyway(org.flywaydb.core.Flyway) FlywayException(org.flywaydb.core.api.FlywayException) Test(org.junit.Test)

Aggregations

FlywayException (org.flywaydb.core.api.FlywayException)42 IOException (java.io.IOException)13 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)6 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 Test (org.junit.Test)6 File (java.io.File)5 InputStreamReader (java.io.InputStreamReader)5 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)5 FileInputStream (java.io.FileInputStream)4 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)4 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 URL (java.net.URL)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 HashMap (java.util.HashMap)3 Properties (java.util.Properties)3 FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)3 DriverDataSource (org.flywaydb.core.internal.util.jdbc.DriverDataSource)3 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)3