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.
You can do it through code by declaring the following bean.
@Beanpublic FlywayMigrationStrategy cleanMigrateStrategy() { return flyway -> { flyway.repair(); flyway.migrate(); };}
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/