Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get flyway to re-run migration?

Tags:

flyway

OUR SYSTEM

We are trying to put migrations as .sql files under version control. Developers would write a VN__*.sql file, commit to version control and a job that runs every 5 minutes would automatically migrate to a Dev and Test database. Once the change proved to not cause problems, someone else would run a manual job to run the migration on Production.

MY PROBLEM:

I had a demo migration that created a few tables. I checked V4__DemoTables.sql into version control on my PC.

On our Linux box a job that runs every 5 minutes extracted the new file from version control, then ran the flyway.sh file. It detected the file and executed it.

But the .sql file had a typo. And we are using Neteeza which has problems with flyway automatically wrapping a migration in a BEGIN TRAN ... END TRAN. So the migration created 2 tables, then aborted before the third.

No problem I thought. I dropped the 2 tables that the .sql file did create. Checked V4__ out of version control, fixed the typo and re-submitted it.

Five minutes later the update was extracted but flyway complains that the checksum does not match. So it will NOT run the updated V4__DemoTables.sql file.

How do I get flyway to accept the updated file and update the checksum in the SCHEMA_VERSION file in case of a typo?

Reading the docs it seems like the developers suggest I should have created a new V4_1_DemoTables.sql file with the fix's. But this would have collided with the commands in the V4__ file so this seemed wrong.

So here is what the docs imply I need to do:

  • Leave V4__ as a 'successful' migration according to the SCHEMA_VERSION table.

    Create V4_1_ to delete the tables that were created before the typo line in V4__.

    Create V4_2_ which has the typo fix from the original file to do all the real work.

Is this correct?

like image 730
Bob Mac Avatar asked Jul 24 '12 18:07

Bob Mac


People also ask

What is repeatable migration?

Repeatable migrations have a description and a checksum, but no version. Instead of being run just once, they are (re-)applied every time their checksum changes. This is very useful for managing database objects whose definition can then simply be maintained in a single file in version control.

How do I undo a migration on Flyway?

Undoing the last migrationBy default, undo undoes the last applied versioned migration. Our audit trail now clearly shows that version 2 was first applied, then undone and is now pending again.

How does Flyway rollback work?

For rollbacks to work, an undo script needs to be maintained for each forward versioned migration between the currently deployed version and the version to roll back to. Undo scripts need to reverse the change made by the corresponding versioned migration and ensure that any data manipulation is reversed.


1 Answers

If the migration completes successfully, but some of the db objects are not quite right yet (typo in column name, ...), do as you said and push a follow-up script that fixes it (rename column, ...).

If the migration fails, and it didn't run on a DB with DDL transaction, the DB must be cleaned manually. This means:

  • Reverting the effects of the migration on the DB
  • Removing the version from the SCHEMA_VERSION table and marking the previous one as current

This second step will be automated in the future with the introduction of the flyway.repair() command.

like image 178
Axel Fontaine Avatar answered Dec 03 '22 07:12

Axel Fontaine