Search in sources :

Example 6 with FlywaySqlException

use of org.flywaydb.core.internal.exception.FlywaySqlException in project flyway by flyway.

the class JdbcTableSchemaHistory method refreshCache.

private void refreshCache() {
    int maxCachedInstalledRank = cache.isEmpty() ? -1 : cache.getLast().getInstalledRank();
    String query = database.getSelectStatement(table);
    try {
        cache.addAll(jdbcTemplate.query(query, new RowMapper<AppliedMigration>() {

            public AppliedMigration mapRow(final ResultSet rs) throws SQLException {
                // Construct a map of lower-cased column names to ordinals. This is useful for databases that
                // upper-case them - eg Snowflake with QUOTED-IDENTIFIERS-IGNORE-CASE turned on
                HashMap<String, Integer> columnOrdinalMap = constructColumnOrdinalMap(rs);
                Integer checksum = rs.getInt(columnOrdinalMap.get("checksum"));
                if (rs.wasNull()) {
                    checksum = null;
                }
                return new AppliedMigration(rs.getInt(columnOrdinalMap.get("installed_rank")), rs.getString(columnOrdinalMap.get("version")) != null ? MigrationVersion.fromVersion(rs.getString(columnOrdinalMap.get("version"))) : null, rs.getString(columnOrdinalMap.get("description")), MigrationType.fromString(rs.getString(columnOrdinalMap.get("type"))), rs.getString(columnOrdinalMap.get("script")), checksum, rs.getTimestamp(columnOrdinalMap.get("installed_on")), rs.getString(columnOrdinalMap.get("installed_by")), rs.getInt(columnOrdinalMap.get("execution_time")), rs.getBoolean(columnOrdinalMap.get("success")));
            }
        }, maxCachedInstalledRank));
    } catch (SQLException e) {
        throw new FlywaySqlException("Error while retrieving the list of applied migrations from Schema History table " + table, e);
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) RowMapper(org.flywaydb.core.internal.jdbc.RowMapper)

Example 7 with FlywaySqlException

use of org.flywaydb.core.internal.exception.FlywaySqlException in project CzechIdMng by bcvsolutions.

the class IdmFlywayMigrationStrategy method resolveDbName.

/**
 * Resolve dbName, which is used by {@Flyway} datasource.
 *
 * @param dataSource sql data source
 * @return e.g. {@link #POSTGRESQL_DBNAME}, {@link #MSSQL_DBNAME}
 * @since 11.1.0
 */
public String resolveDbName(DataSource dataSource) {
    Connection connection = JdbcUtils.openConnection(dataSource, 1);
    // 
    try {
        DatabaseMetaData databaseMetaData = JdbcUtils.getDatabaseMetaData(connection);
        // 
        String databaseProductName = databaseMetaData.getDatabaseProductName().toLowerCase().replace(" ", "");
        if (databaseProductName.contains(MSSQL_DBNAME)) {
            // product name for mssql was changed since flyway 6 => map product name to our folder name
            databaseProductName = MSSQL_DBNAME;
        }
        return databaseProductName;
    } catch (SQLException ex) {
        throw new FlywaySqlException("Error while determining database product name", ex);
    } finally {
        JdbcUtils.closeConnection(connection);
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 8 with FlywaySqlException

use of org.flywaydb.core.internal.exception.FlywaySqlException 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)

Example 9 with FlywaySqlException

use of org.flywaydb.core.internal.exception.FlywaySqlException in project flyway by flyway.

the class CockroachDBDatabase method rawDetermineVersion.

private MigrationVersion rawDetermineVersion() {
    String version;
    try {
        // Use rawMainJdbcConnection to avoid infinite recursion.
        JdbcTemplate template = new JdbcTemplate(rawMainJdbcConnection);
        version = template.queryForString("SELECT value FROM crdb_internal.node_build_info where field='Version'");
        if (version == null) {
            version = template.queryForString("SELECT value FROM crdb_internal.node_build_info where field='Tag'");
        }
    } catch (SQLException e) {
        throw new FlywaySqlException("Unable to determine CockroachDB version", e);
    }
    int firstDot = version.indexOf(".");
    int majorVersion = Integer.parseInt(version.substring(1, firstDot));
    String minorPatch = version.substring(firstDot + 1);
    int minorVersion = Integer.parseInt(minorPatch.substring(0, minorPatch.indexOf(".")));
    return MigrationVersion.fromVersion(majorVersion + "." + minorVersion);
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) SQLException(java.sql.SQLException) JdbcTemplate(org.flywaydb.core.internal.jdbc.JdbcTemplate)

Example 10 with FlywaySqlException

use of org.flywaydb.core.internal.exception.FlywaySqlException in project flyway by flyway.

the class DbClean method dropDatabaseObjectsPreSchemas.

/**
 * Drops database-level objects that need to be cleaned prior to schema-level objects.
 */
private void dropDatabaseObjectsPreSchemas() {
    LOG.debug("Dropping pre-schema database level objects...");
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    try {
        ExecutionTemplateFactory.createExecutionTemplate(connection.getJdbcConnection(), database).execute(() -> {
            database.cleanPreSchemas();
            return null;
        });
        stopWatch.stop();
        LOG.info(String.format("Successfully dropped pre-schema database level objects (execution time %s)", TimeFormat.format(stopWatch.getTotalTimeMillis())));
    } catch (FlywaySqlException e) {
        LOG.debug(e.getMessage());
        LOG.warn("Unable to drop pre-schema database level objects");
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) StopWatch(org.flywaydb.core.internal.util.StopWatch)

Aggregations

FlywaySqlException (org.flywaydb.core.internal.exception.FlywaySqlException)17 SQLException (java.sql.SQLException)13 ResultSet (java.sql.ResultSet)3 FlywayException (org.flywaydb.core.api.FlywayException)3 MigrationVersion (org.flywaydb.core.api.MigrationVersion)3 StopWatch (org.flywaydb.core.internal.util.StopWatch)3 MigrationType (org.flywaydb.core.api.MigrationType)2 JdbcTemplate (org.flywaydb.core.internal.jdbc.JdbcTemplate)2 RowMapper (org.flywaydb.core.internal.jdbc.RowMapper)2 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 CustomLog (lombok.CustomLog)1 MigrationPattern (org.flywaydb.core.api.MigrationPattern)1 CommandResultFactory (org.flywaydb.core.api.output.CommandResultFactory)1 RepairOutput (org.flywaydb.core.api.output.RepairOutput)1 RepairResult (org.flywaydb.core.api.output.RepairResult)1