knightrider
knightrider

Reputation: 237

How to change flyway migration file that were applied a while ago?

Problem: We have 2 migration files that created the tables using the functions in postgres DB. These tables were created a year ago and after that we have many new migrations. Based on our new standards, we are prohibiting a word that is in the table name.

Example: We are not allowing going forward tables to be named '_audit'. The tables created were are named 'abc_audit' and 'def_audit'.

Prohibiting '_audit' is embedded in the latest function that creates the tables. We can't just alter the table name since docker spins up the entire new db and runs all the migration files and considers only the latest function which prevents '_audit' table name and it fails the original migration.

How do we resolve this? We don't want to re-baseline and lose the history.

Upvotes: 1

Views: 3361

Answers (1)

Mikiel
Mikiel

Reputation: 448

It sounds like you need to bring this naming-compliance function into version control.

  • Delete the function from the database
  • Create a versioned migration which creates the function
  • Give the new migration a version so it will run in the correct order
    • If you had V1__abc_audit.sql and V2__def_audit.sql, followed by V3__something_else.sql, name your new migration V2_1__naming_function.sql

Now this function will only exist part way through the deployment, after the non-compliant tables are deployed.

For existing Flyway deployments, I suggest using the of the 'out of order' config option. You could do the following:

  • Delete the function from the database
  • Set flyway.outOfOrder to true
  • Run migrate

The 'out of order' option means Flyway will run the new migration in the correct order, despite it being a lower version than the currently deployed version.

Upvotes: 1

Related Questions