Flyway repair with Spring Boot
there are several ways to perform a repair on the database. I personally prefer the simple SQL statement.
SQL Statement:
Just delete the row with the failed migration. After that you can run the migration again.
Run flyway directly
You can install Flyway local and run flyway repair
in the console
Use the Flyway Maven Plugin
Add the Flyway Maven Plugin to your pom and run mvn flyway:repair
. I don't think this contradict with the Spring Boot concept.
Extend Spring Boot
Spring Boot will call
Flyway.migrate()
to perform the database migration. If you would like more control, provide a@Bean
that implementsFlywayMigrationStrategy
.
In the FlywayMigrationStrategy
you can call the migrate or repair method from flyway. More Information is available in the Spring Boot Reference Guide.
I don't think the FlywayMigrationStrategy
in the application is the right place to repair the database. A failed migration is a exception and should be handle outside the application.
Flyway Maven Plugin
Just to add this info to @Daniel's answer
1.
...
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>4.1.0</version>
<configuration>
<url>jdbc:mysql://localhost:3306</url>
<user>root</user>
<password>root</password>
<schemas>
<schema>[your_schema]</schema>
</schemas>
</configuration>
</plugin>
...
2.
mvn flyway:clean
3.
mvn flyway:repair
PS.: if the step 2 and 3 don't work change the order.
More info on maven goals: https://flywaydb.org/documentation/maven/
You can do it through code by declaring the following bean.
@Bean
public FlywayMigrationStrategy cleanMigrateStrategy() {
return flyway -> {
flyway.repair();
flyway.migrate();
};
}
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.