Click to see details of `Migrations have failed validation`
mvn clean spring-boot:run
can fail errors such asValidate failed: Migrations have failed validation
if the previous run ofmvn clean spring-boot:run
had resulted in failed migrations, asflyway
would have made an entry in theflyway_schema_history;
db table.And then we see the following error when we run the
mvn clean spring-boot:run
command next time.Click here for errors
ERROR org.springframework.boot.SpringApplication.reportFailure - Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Validate failed: Migrations have failed validation Detected failed migration to version 001 (create baseline). Please remove any half-completed changes then run repair to fix the schema history. Caused by: org.flywaydb.core.api.exception.FlywayValidateException: Validate failed: Migrations have failed validation Detected failed migration to version 001 (create baseline). Please remove any half-completed changes then run repair to fix the schema history.
flyway
clearly telling us to repair the failed migrations in the logs. There are few ways to handle this issue, including both the manual and automated solutions. But let's focus on the automated solution for now usingflyway callbacks
as we don't want manual intervention.We could consider an approach to automatically clean the failed entries from the
flyway_schema_history
after a failed migration. For this purpose, we can use theafterMigrateError
Flyway callback.Let's first create the SQL callback file
db/callback/afterMigrateError__repair.sql
:DELETE FROM flyway_schema_history WHERE success=false;This will automatically remove any failed entry from the Flyway state history, whenever a migration error occurs.
But to make
flyway
find and execute this script, we have to add it to the path like below:spring.flyway.locations=classpath:db/migration,classpath:db/callback
Although it can be added to some existing properties file, but, we'll add it to a new
application-flyway-callback.properties
and activate this newflyway-callback
spring profile.
flyway-callback
spring profile should be activated only when theflyway
spring profile is also activated.