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