Retry a flyway failed migration

This is answered in the FAQ: http://flywaydb.org/documentation/faq.html#repair

The upcoming Flyway 2.0 will include the repair command. This code is already checked into SCM.

Note: This only deals with Flyway's metadata table. You are still responsible for cleaning up any other effects of a failed migration.

Update: Flyway 2.0 has now been released. You can grab it at http://flywaydb.org


Full example, this will always try to repair before running a migration, the rest of configuration in in a config file.

@Configuration
public class PersistanceConfiguration {
    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @Bean
    public FlywayMigrationStrategy cleanMigrateStrategy() {
        FlywayMigrationStrategy strategy = new FlywayMigrationStrategy() {
            @Override
            public void migrate(Flyway flyway) {
                flyway.repair();
                flyway.migrate();
            }
        };
        return strategy;
    }

}

I don't know whether this a good idea or not, but you could try doing a repair() if migrate() fails:

final Flyway flyway = new Flyway();
flyway.setBaselineOnMigrate(true);
flyway.setValidateOnMigrate(false);
flyway.setDataSource(dataSource());
try {
    flyway.migrate();
} catch (final Exception e) {
    logger.error("Flyway migration failed, doing a repair and retrying ...");
    flyway.repair();
    flyway.migrate();
}

Tags:

Flyway