Search in sources :

Example 1 with MigrationPattern

use of org.flywaydb.core.api.MigrationPattern 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)

Aggregations

ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 java.util (java.util)1 Callable (java.util.concurrent.Callable)1 CustomLog (lombok.CustomLog)1 FlywayException (org.flywaydb.core.api.FlywayException)1 MigrationPattern (org.flywaydb.core.api.MigrationPattern)1 MigrationType (org.flywaydb.core.api.MigrationType)1 MigrationVersion (org.flywaydb.core.api.MigrationVersion)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 Database (org.flywaydb.core.internal.database.base.Database)1 Table (org.flywaydb.core.internal.database.base.Table)1 FlywaySqlException (org.flywaydb.core.internal.exception.FlywaySqlException)1 ExecutionTemplateFactory (org.flywaydb.core.internal.jdbc.ExecutionTemplateFactory)1 JdbcNullTypes (org.flywaydb.core.internal.jdbc.JdbcNullTypes)1