When database migration fails, the migration is marked as failed in the schema history table (i.e flyway_schema_history)
indicating manual database cleanup may be required. But if database supports DDL transactions, the migration is rolled back automatically and nothing is recorded in the schema history table. PostgreSQL
, Amazon Redshift
, MS SQL
are few of the databases which support DDL transactions whereas Oracle Database
, MySQL
, MariaDB
, Amazon Aurora
does not support DDL transactions.
In case of failed migration entries, there are several options to repair it (only applicable for databases that do NOT support DDL transactions) as described by @daniel-käfer. I want to add another (may be easier way) to deal with failed migrations.
There are several callbacks supported by flyway, afterMigrateError
is one of them. If we add a sql file with name afterMigrateError.sql
then, it will be executed after each failed migrate runs. Therefore, we can simply create a file afterMigrateError.sql
on default location of database migration folder (resources/db/migration
) with sql command to remove failed migrations from flyway_schema_history
table.
The sql command afterMigrateError.sql
can be as mentioned below:
DELETE IGNORE FROM flyway_schema_history WHERE success=0;
This command looks for the table flyway_schema_history
if it exists otherwise it will do no changes. Then it simply looks for the rows which has success
column with 0
entry (actually this happen if migration fails , all successful migration will have value 1
in success column), then delete such entries. Now, we can simply change our latest migration file and correct it and run again.
flyway repair
as changing previous sql files should be very exceptional. But it looks that checksums doesn't match between sql files in classpath and local... – Thomas Duchatellemvn flyway:clean flyway:migrate
. You can also use flyway command line. – Thomas Duchatelle