Working on a product that is deployed by many clients in many production environments. It includes at least one Spring Boot app.
We've used flyway for db schema migrations. Upgrading from Spring Boot 1.5.x to 2.0.x has bumped our flyway version from 3.x to 5.x.
The Spring Boot migration guide simply says to upgrade to flyway 4 before the boot upgrade. However, this would require all of our customers to do an intermediate upgrade before being able to upgrade to the latest.
So, the question is: How would you upgrade from flyway 3 directly to flyway 5?
I tried to skip v4 too but didn't work. Running the repair from 3 to 5 will make the checksums correct, but won't change the
schema_version
format. That has changed as well.It seems you need to go to v4 first. Even if temporarily just to run
mvn flyway:validate
, which will repairschema_version
.I've done this on this repo: https://github.com/fabiofalci/flyway-from-3-to-5/commits/5.0.7
The first commit is v3, the second commit is v4 (where I ran validate) and then the third commit on v5 the schema is correct.
Step 0.
Upgrade to spring boot v2.1 (and therby implicitly to flyway 5).
Step 1.
Since
schema_version
was used in flyway 3.x let new flyway versions know that they should keep using this table.:Step 2.
Create file
src/main/ressources/db/migration/flyway_upgradeMetaDataTable_V3_to_V4.sql
for upgrading the meta table based on the dialect you use.See https://github.com/flyway/flyway/commit/cea8526d7d0a9b0ec35bffa5cb43ae08ea5849e4#diff-b9cb194749ffef15acc9969b90488d98 for the update scripts of several dialects.
Here is the one for postgres and assuming the flyway table name is
schema_version
:Step 3.
Create Java file
your.package/FlywayUpdate3To4Callback.java
Please note that this does the following:
Flyway.repair()
Step 4.
Run spring boot.
The log should show info messages similar to these:
Credits
This answer is based on Eduardo Rodrigues answer by changing:
Event.BEFORE_VALIDATE
to trigger a flyway callback that upgrade flyway 3 to 4.In case I'm not the last person on the planet to still be upgrading from 3 to 5.
Problem:
I wanted the upgrade to be transparent to other developers on the project as well as not requiring any special deployment instructions when upgrading on the live applications, so I did the following.
I had a look at how version 4 handled the upgrade:
This is easy enough to do manually but to make it transparent. The app is a spring app, but not a spring boot app, so at the time I had flyway running migrations automatically on application startup by having LocalContainerEntityManager bean construction dependent on the flyway bean, which would call migrate as its init method (explained here Flyway Spring JPA2 integration - possible to keep schema validation?), so the order of bootstrapping would be:
Solution:
I changed the order of bootstrapping to:
where Flyway3To4Migrator would perform the schema_table changes if needed, run the repair if the upgrade happened, and then always run flyway.migrate to continue the migrations.
A few things to note:
The code above is not compatible with version 5. It uses deprecated classes. Here is an updated version.
If you're using Spring Boot, you can register a callback that does the upgrade on beforeMigrate(). The code is similar to @trf and looks like this:
Notice that you don't need to manually call flyway.migrate() here.