Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway: How to replace deprecated SpringJdbcMigration without getting "FlywayException: Validate failed"?

Upgrading springboot 2.0.3 to 2.1.1 also brings a new flyway version: 5.2.3 instead of 5.0.7.

In 5.2.3 SpringJdbcMigration is deprecated and will be removed in flyway 6. I use sql scripts mostly, but I also have one java migration class in a project (there are 4 sql migrations and the last one, 4.2, is a java migration - it was just some quick and dirty hack to change old data and I don't need it anymore).

So I changed that class:

class V4_2__Update_foo implements SpringJdbcMigration {
    public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    ...
    }
}

to

class V4_2__Update_foo extends BaseJavaMigration {
    public void migrate(Context context) throws Exception {
    JdbcTemplate jdbcTemplate = 
      new JdbcTemplate(new SingleConnectionDataSource(context.getConnection(), true));
    ......
    }
}

This is the only change, everything else is the same. The result is

Application run failed
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]:
Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException:
Validate failed: Migration type mismatch for migration version 4.2
....
Caused by: org.flywaydb.core.api.FlywayException:
Validate failed: Migration type mismatch for migration version 4.2
-> Applied to database : SPRING_JDBC
-> Resolved locally    : JDBC
at org.flywaydb.core.Flyway.doValidate(Flyway.java:1479)

I don't want to disable validation permanently, but I also don't know how to solve this. I tried googling but found nothing concerning "type mismatch". On my developer machine I tried "flyway repair", but it only said

Repair of failed migration in Schema History table "PUBLIC"."schema_version" not necessary. No failed migration detected.
Successfully repaired schema history table "PUBLIC"."schema_version"

The type of migration 4.2 is still "SPRING_JDBC" after running repair. Later I deleted the java class completely which got me a warning that

Schema "PUBLIC" has a version (4.2) that is newer than the latest available migration (4) !

but at least the application was running again. I'm not really comfortable doing that in production, though. Any other ideas?

like image 356
dendron Avatar asked Dec 11 '18 10:12

dendron


1 Answers

That's definitely a bug. Unfortunately this won't be fixed in 5.2.x. Flyway 6.0 (due in Q1 2019) will automatically correct your schema history table and fix this.

Alternatively if you really don't want to wait, you can manually path up the schema history table to make this message go away.

like image 174
Axel Fontaine Avatar answered Oct 15 '22 15:10

Axel Fontaine