Guenther
Guenther

Reputation: 2045

How to execute a preFlywayUpgrade script before schema_version upgrade

we are trying to migrate from flyway 2.0.3 to 4.0.3. This fails, because flyway tries to drop the primary key from the schema_version table, but it didn't have one in 2.0.3. Reading previous posts about flyway upgrade issues, it seems we have two options. Try to migrate in several steps, first to 2.3, (the upgrade to 3 can be skipped?), and then to 4.0.3 or manually updating the schema_version table. For various reasons, we'd like to do the latter.

What is the best way of doing this with flyway? Our customers don't have db client tools available that would allow them to execute SQL directly on the database. Is there a way to execute a sql script before the upgrade to the schema_version table takes place, e.g.

preFlywayUpgrade.sql

? I tried preRepair.sql but this is only executed after flyway has successfully updated the schema_version table.

Edit: 2016-08-18: I ended up downloading flyway src from version 2.3.1, 3.2 and 4.0.3, combining the required sql statements to migrate from 2.0.3 to 4.0.3 and adding the resulting sql to flyway-core-4.0.3, replacing the existing 4.0.3 migration script. I can understand that you don't want to keep the old migration code around forever, but the projects I've worked with keep their database migration scripts around for years and some customers don't upgrade that often. Given that flyway is a tool used to upgrade incrementally from any given version, it would be nice, if it supported upgrades of itself a little more comfortable. (Don't get me wrong, I'm really grateful for all the work you have done on it and I think it's a great tool!) One idea could be to have flyway write a record of it's own version into the schema_version table. That way it would be easier to identify which changes need to be done to the schema_version table itself.

Upvotes: 0

Views: 285

Answers (2)

trf
trf

Reputation: 1394

Would a solution like this help?

https://stackoverflow.com/a/50390573/2681568

We used this to transparently upgrade from Flyway 3 to 5, but something similar might help here too.

Upvotes: 1

Axel Fontaine
Axel Fontaine

Reputation: 35169

If you want Flyway to do the work for you, you have to follow the built-in upgrade path and migrate to the latest version of each major version before moving to the next one. Otherwise you're on your own.

Alternatively you could also drop the schema_version table and baseline the DB again with the new version of Flyway.

Upvotes: 0

Related Questions