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);
}
}
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");
}
}
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);
}
}
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();
}
}
}
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);
}
}
Aggregations