Search in sources :

Example 56 with FlywayException

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

the class Flyway method doValidate.

/**
     * Performs the actual validation. All set up must have taken place beforehand.
     *
     * @param connectionMetaDataTable The database connection for the metadata table.
     * @param dbSupport               The database-specific support.
     * @param migrationResolver       The migration resolver;
     * @param metaDataTable           The metadata table.
     * @param schemas                 The schemas managed by Flyway.
     * @param pending                 Whether pending migrations are ok.
     */
private void doValidate(Connection connectionMetaDataTable, DbSupport dbSupport, MigrationResolver migrationResolver, MetaDataTable metaDataTable, Schema[] schemas, FlywayCallback[] flywayCallbacks, boolean pending) {
    String validationError = new DbValidate(connectionMetaDataTable, dbSupport, metaDataTable, schemas[0], migrationResolver, target, outOfOrder, pending, ignoreMissingMigrations, ignoreFutureMigrations, flywayCallbacks).validate();
    if (validationError != null) {
        if (cleanOnValidationError) {
            new DbClean(connectionMetaDataTable, dbSupport, metaDataTable, schemas, flywayCallbacks, cleanDisabled).clean();
            metaDataTable.clearCache();
        } else {
            throw new FlywayException("Validate failed: " + validationError);
        }
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) DbValidate(org.flywaydb.core.internal.command.DbValidate) DbClean(org.flywaydb.core.internal.command.DbClean)

Example 57 with FlywayException

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

the class AbstractFlywayMojo method getConfigFileProperties.

/**
     * Retrieve the properties from the config file (if specified).
     */
private Properties getConfigFileProperties() throws IOException {
    Properties properties = new Properties();
    String configFileProp = System.getProperty("flyway.configFile");
    if (configFileProp != null) {
        configFile = new File(configFileProp);
        if (!configFile.isAbsolute()) {
            configFile = new File(mavenProject.getBasedir(), configFileProp);
        }
    }
    if (configFile == null) {
        File file = new File(mavenProject.getBasedir(), "flyway.properties");
        if (file.isFile() && file.canRead()) {
            configFile = file;
        } else {
            log.debug("flyway.properties not found. Skipping.");
            return properties;
        }
    } else if (!configFile.canRead() || !configFile.isFile()) {
        throw new FlywayException("Unable to read config file: " + configFile.getAbsolutePath());
    }
    properties.load(new InputStreamReader(new FileInputStream(configFile), encoding));
    return properties;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) InputStreamReader(java.io.InputStreamReader) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 58 with FlywayException

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

the class DriverDataSource method getConnectionFromDriver.

/**
 * Build properties for the Driver, including the given user and password (if any),
 * and obtain a corresponding Connection.
 *
 * @param username the name of the user
 * @param password the password to use
 * @return the obtained Connection
 * @throws SQLException in case of failure
 * @see java.sql.Driver#connect(String, java.util.Properties)
 */
protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
    Properties properties = new Properties(this.defaultProperties);
    if (username != null) {
        properties.setProperty("user", username);
    }
    if (password != null) {
        properties.setProperty("password", password);
    }
    properties.putAll(additionalProperties);
    Connection connection = driver.connect(url, properties);
    if (connection == null) {
        throw new FlywayException("Unable to connect to " + DatabaseTypeRegister.redactJdbcUrl(url));
    }
    connection.setAutoCommit(autoCommit);
    return connection;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) Connection(java.sql.Connection) Properties(java.util.Properties)

Example 59 with FlywayException

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

the class PostgreSQLCopyParsedStatement method execute.

@Override
public Results execute(JdbcTemplate jdbcTemplate) {
    // #2355: Use reflection to ensure this works in cases where the PostgreSQL driver classes were loaded in a
    // child URLClassLoader instead of the system classloader.
    Object baseConnection;
    Object copyManager;
    Method copyManagerCopyInMethod;
    try {
        Connection connection = jdbcTemplate.getConnection();
        ClassLoader classLoader = connection.getClass().getClassLoader();
        Class<?> baseConnectionClass = classLoader.loadClass("org.postgresql.core.BaseConnection");
        baseConnection = connection.unwrap(baseConnectionClass);
        Class<?> copyManagerClass = classLoader.loadClass("org.postgresql.copy.CopyManager");
        Constructor<?> copyManagerConstructor = copyManagerClass.getConstructor(baseConnectionClass);
        copyManagerCopyInMethod = copyManagerClass.getMethod("copyIn", String.class, Reader.class);
        copyManager = copyManagerConstructor.newInstance(baseConnection);
    } catch (Exception e) {
        throw new FlywayException("Unable to find PostgreSQL CopyManager class", e);
    }
    Results results = new Results();
    try {
        try {
            Long updateCount = (Long) copyManagerCopyInMethod.invoke(copyManager, getSql(), new StringReader(copyData));
            results.addResult(new Result(updateCount, null, null, getSql()));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new SQLException("Unable to execute COPY operation", e);
        }
    } catch (SQLException e) {
        jdbcTemplate.extractErrors(results, e);
    }
    return results;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Reader(java.io.Reader) StringReader(java.io.StringReader) Method(java.lang.reflect.Method) FlywayException(org.flywaydb.core.api.FlywayException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(org.flywaydb.core.internal.jdbc.Result) Results(org.flywaydb.core.internal.jdbc.Results) StringReader(java.io.StringReader)

Example 60 with FlywayException

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

the class TransactionalExecutionTemplate method execute.

/**
 * Executes this callback within a transaction.
 *
 * @param callback The callback to execute.
 * @return The result of the transaction code.
 */
@Override
public <T> T execute(Callable<T> callback) {
    boolean oldAutocommit = true;
    try {
        oldAutocommit = connection.getAutoCommit();
        connection.setAutoCommit(false);
        T result = callback.call();
        connection.commit();
        return result;
    } catch (Exception e) {
        RuntimeException rethrow;
        if (e instanceof SQLException) {
            rethrow = new FlywaySqlException("Unable to commit transaction", (SQLException) e);
        } else if (e instanceof RuntimeException) {
            rethrow = (RuntimeException) e;
        } else {
            rethrow = new FlywayException(e);
        }
        if (rollbackOnException) {
            try {
                LOG.debug("Rolling back transaction...");
                connection.rollback();
                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.exception.FlywaySqlException) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) FlywayException(org.flywaydb.core.api.FlywayException) FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException)

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