use of org.flywaydb.core.internal.exception.FlywaySqlException in project flyway by flyway.
the class JdbcTableSchemaHistory method removeFailedMigrations.
@Override
public boolean removeFailedMigrations(RepairResult repairResult, MigrationPattern[] migrationPatternFilter) {
if (!exists()) {
LOG.info("Repair of failed migration in Schema History table " + table + " not necessary as table doesn't exist.");
return false;
}
List<AppliedMigration> appliedMigrations = filterMigrations(allAppliedMigrations(), migrationPatternFilter);
boolean failed = appliedMigrations.stream().anyMatch(am -> !am.isSuccess());
if (!failed) {
LOG.info("Repair of failed migration in Schema History table " + table + " not necessary. No failed migration detected.");
return false;
}
try {
appliedMigrations.stream().filter(am -> !am.isSuccess()).forEach(am -> repairResult.migrationsRemoved.add(CommandResultFactory.createRepairOutput(am)));
for (AppliedMigration appliedMigration : appliedMigrations) {
jdbcTemplate.execute("DELETE FROM " + table + " WHERE " + database.quote("success") + " = " + database.getBooleanFalse() + " AND " + (appliedMigration.getVersion() != null ? database.quote("version") + " = '" + appliedMigration.getVersion().getVersion() + "'" : database.quote("description") + " = '" + appliedMigration.getDescription() + "'"));
}
clearCache();
} catch (SQLException e) {
throw new FlywaySqlException("Unable to repair Schema History table " + table, e);
}
return true;
}
use of org.flywaydb.core.internal.exception.FlywaySqlException in project flyway by flyway.
the class JdbcUtils method openConnection.
/**
* Opens a new connection from this DataSource.
*
* @param dataSource The DataSource to obtain the connection from.
* @param connectRetries The maximum number of retries when attempting to connect to the database.
* @param connectRetriesInterval The maximum time between retries in seconds.
* @return The new connection.
* @throws FlywayException when the connection could not be opened.
*/
public static Connection openConnection(DataSource dataSource, int connectRetries, int connectRetriesInterval) throws FlywayException {
BackoffStrategy backoffStrategy = new BackoffStrategy(1, 2, connectRetriesInterval);
int retries = 0;
while (true) {
try {
return dataSource.getConnection();
} catch (SQLException e) {
if ("08S01".equals(e.getSQLState()) && e.getMessage().contains("This driver is not configured for integrated authentication")) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, true) + ": " + e.getMessage() + "\nTo setup integrated authentication see " + FlywayDbWebsiteLinks.WINDOWS_AUTH, e);
} else if (e.getSQLState() == null && e.getMessage().contains("MSAL4J")) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, false) + ": " + e.getMessage() + "\nYou need to install some extra drivers in order for interactive authentication to work." + "\nFor instructions, see " + FlywayDbWebsiteLinks.AZURE_ACTIVE_DIRECTORY, e);
}
if (++retries > connectRetries) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, false) + ": " + e.getMessage(), e);
}
Throwable rootCause = ExceptionUtils.getRootCause(e);
String message = "Connection error: " + e.getMessage();
if (rootCause != null && rootCause != e && rootCause.getMessage() != null) {
message += "\n(Caused by " + rootCause.getMessage() + ")";
}
LOG.warn(message + "\nRetrying in " + backoffStrategy.peek() + " sec...");
try {
Thread.sleep(backoffStrategy.next() * 1000L);
} catch (InterruptedException e1) {
throw new FlywaySqlException("Unable to obtain connection from database" + getDataSourceInfo(dataSource, false) + ": " + e.getMessage(), e);
}
}
}
}
Aggregations