dendron
dendron

Reputation: 63

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?

Upvotes: 5

Views: 4214

Answers (2)

ynerdy
ynerdy

Reputation: 604

Problem:

[schema_version table with SPRING_JDBC][1] [1]: https://i.sstatic.net/GI9So.png

SOLUTION: I solve this issue by manually updating schema_version table using below query: update schema_version set type='SQL', checksum=662979041 where script='V001_026__initial_reconciliation_config_env_NonProd.sql';

However, to create above UPDATE statement, you will need checksum of actual script that you migrated previously using spring/java class which will not have checksum in it.

How to get that checksum? In your local, delete the schema_version table in local. Start your app successfully, you should now have new schema_version table created with correct checksum and type as SQL.

checksum is to check the integrity of file, that means do no modify the file once you get checksum.

Then execute above UPDATE statement manually in your non-prod environment.

Next step, remove java class, move that script in folder like db/non-prod and configure that folder in application.yaml like

spring: flyway: locations: "classpath:db/migration,classpath:db/env/migration/V001/NON-PROD"

Upvotes: 0

Axel Fontaine
Axel Fontaine

Reputation: 35169

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.

Upvotes: 4

Related Questions