Search in sources :

Example 1 with FlywaySqlException

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

the class Table method lock.

/**
 * Locks this table in this schema using a read/write pessimistic lock until the end of the current transaction.
 * Note that {@code unlock()} still needs to be called even if your database unlocks the table implicitly
 * (in which case {@code doUnlock()} may be a no-op) in order to maintain the lock count correctly.
 */
public void lock() {
    if (!exists()) {
        return;
    }
    try {
        doLock();
        lockDepth++;
    } catch (SQLException e) {
        throw new FlywaySqlException("Unable to lock table " + this, e);
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) SQLException(java.sql.SQLException)

Example 2 with FlywaySqlException

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

the class DbClean method dropDatabaseObjectsPostSchemas.

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

Example 3 with FlywaySqlException

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

the class JdbcTableSchemaHistory method update.

@Override
public void update(AppliedMigration appliedMigration, ResolvedMigration resolvedMigration) {
    connection.restoreOriginalState();
    clearCache();
    MigrationVersion version = appliedMigration.getVersion();
    String description = resolvedMigration.getDescription();
    Integer checksum = resolvedMigration.getChecksum();
    MigrationType type = appliedMigration.getType().isSynthetic() ? appliedMigration.getType() : resolvedMigration.getType();
    LOG.info("Repairing Schema History table for version " + version + " (Description: " + description + ", Type: " + type + ", Checksum: " + checksum + ")  ...");
    if (!database.supportsEmptyMigrationDescription() && "".equals(description)) {
        description = NO_DESCRIPTION_MARKER;
    }
    Object checksumObj = checksum == null ? JdbcNullTypes.IntegerNull : checksum;
    try {
        jdbcTemplate.update("UPDATE " + table + " SET " + database.quote("description") + "=? , " + database.quote("type") + "=? , " + database.quote("checksum") + "=?" + " WHERE " + database.quote("installed_rank") + "=?", description, type.name(), checksumObj, appliedMigration.getInstalledRank());
    } catch (SQLException e) {
        throw new FlywaySqlException("Unable to repair Schema History table " + table + " for version " + version, e);
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) MigrationVersion(org.flywaydb.core.api.MigrationVersion) SQLException(java.sql.SQLException) MigrationType(org.flywaydb.core.api.MigrationType)

Example 4 with FlywaySqlException

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

the class JdbcTableSchemaHistory method doAddAppliedMigration.

@Override
protected void doAddAppliedMigration(int installedRank, MigrationVersion version, String description, MigrationType type, String script, Integer checksum, int executionTime, boolean success) {
    boolean tableIsLocked = false;
    connection.restoreOriginalState();
    // in highly concurrent environments
    if (!database.supportsDdlTransactions()) {
        table.lock();
        tableIsLocked = true;
    }
    try {
        String versionStr = version == null ? null : version.toString();
        if (!database.supportsEmptyMigrationDescription() && "".equals(description)) {
            description = NO_DESCRIPTION_MARKER;
        }
        Object versionObj = versionStr == null ? JdbcNullTypes.StringNull : versionStr;
        Object checksumObj = checksum == null ? JdbcNullTypes.IntegerNull : checksum;
        jdbcTemplate.update(database.getInsertStatement(table), installedRank, versionObj, description, type.name(), script, checksumObj, database.getInstalledBy(), executionTime, success);
        LOG.debug("Schema History table " + table + " successfully updated to reflect changes");
    } catch (SQLException e) {
        throw new FlywaySqlException("Unable to insert row for version '" + version + "' in Schema History table " + table, e);
    } finally {
        if (tableIsLocked) {
            table.unlock();
        }
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) SQLException(java.sql.SQLException)

Example 5 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)

Aggregations

FlywaySqlException (org.flywaydb.core.internal.exception.FlywaySqlException)16 SQLException (java.sql.SQLException)12 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 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 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)1 Connection (org.flywaydb.core.internal.database.base.Connection)1