Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retry a flyway failed migration

Tags:

flyway

I'm just in the process of configuring and fully understanding flyway and I came into this situation:

  • I successfully configured a new project to work with flyway.
  • I successfully migrated a test database from version 0 to 1.0.3.
  • Migration to version 1.0.4 failed to execute. (I was trying to add column that was already there, no problems so far, my bad.)

However, once that I made the necessary changes to the corresponding script to work, flyway kept showing this message:

 Current schema version: 1.0.4
 com.googlecode.flyway.core.migration.MigrationException: Migration to version 1.0.4 failed! Please restore backups and roll back database and code!

Since I didn't want to restore a complete dump and apply every migration again, just to make an alter table script to work, what I finally did were some changes to the 'schema_version' table:

  • 1st I erased the entry for version 1.0.4
  • 2nd I set the 'current_version' field to 1 for version 1.0.3
  • And then executed the flyway:migrate command again

After this, the migration finally was applied and a success message shown, however I´m not quite sure if this is the right approach to deal kind of this situations. I'm no sure if its right to modify the 'schema_version' table by myself since i think it should only be modified by flyway itself.

So, after explaining what happened to me, my question would be:

Is there a way to 'retry' to apply a failed migration in flyway, without modifying the 'schema_version' table by myself?

Any command I'm not aware of to fulfill this task?

like image 699
pablocmg Avatar asked Aug 12 '12 16:08

pablocmg


People also ask

What is Flyway error?

Cause: There was a SQL error while running a migration script. Flyway includes a detailed error message about what went wrong; the solution will vary depending on the SQL error. After you fix the migration, delete the failed migration record from flyway_schema_history and start the app again.

What is repeatable migration in Flyway?

Repeatable migrations are very useful for managing database objects whose definition can then simply be maintained in a single file in version control. Instead of being run just once, they are (re-)applied every time their checksum changes. They are typically used for. (Re-)creating views/procedures/functions/packages/ ...


3 Answers

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

like image 94
Axel Fontaine Avatar answered Oct 12 '22 13:10

Axel Fontaine


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();
}
like image 37
yglodt Avatar answered Oct 12 '22 14:10

yglodt


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;
    }

}
like image 25
Orhan Avatar answered Oct 12 '22 14:10

Orhan