Search in sources :

Example 16 with FlywaySqlException

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;
}
Also used : Database(org.flywaydb.core.internal.database.base.Database) java.util(java.util) FlywayException(org.flywaydb.core.api.FlywayException) RepairResult(org.flywaydb.core.api.output.RepairResult) RowMapper(org.flywaydb.core.internal.jdbc.RowMapper) CustomLog(lombok.CustomLog) Callable(java.util.concurrent.Callable) MigrationType(org.flywaydb.core.api.MigrationType) MigrationPattern(org.flywaydb.core.api.MigrationPattern) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) Connection(org.flywaydb.core.internal.database.base.Connection) ExecutionTemplateFactory(org.flywaydb.core.internal.jdbc.ExecutionTemplateFactory) MigrationVersion(org.flywaydb.core.api.MigrationVersion) CommandResultFactory(org.flywaydb.core.api.output.CommandResultFactory) JdbcNullTypes(org.flywaydb.core.internal.jdbc.JdbcNullTypes) SqlScriptExecutorFactory(org.flywaydb.core.internal.sqlscript.SqlScriptExecutorFactory) SQLException(java.sql.SQLException) JdbcTemplate(org.flywaydb.core.internal.jdbc.JdbcTemplate) SqlScriptFactory(org.flywaydb.core.internal.sqlscript.SqlScriptFactory) ResultSet(java.sql.ResultSet) FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) RepairOutput(org.flywaydb.core.api.output.RepairOutput) Table(org.flywaydb.core.internal.database.base.Table) ResultSetMetaData(java.sql.ResultSetMetaData) FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) SQLException(java.sql.SQLException)

Example 17 with FlywaySqlException

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);
            }
        }
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.exception.FlywaySqlException) BackoffStrategy(org.flywaydb.core.internal.strategy.BackoffStrategy)

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