Sub topic 9
Checksum validation and what to do when it fails
This chapter assumes you can write versioned migrations and read flyway_schema_history, both covered in earlier chapters. It explains how Flyway detects edited migrations and how to recover when validation fails.
How validation works
When Flyway applies a migration, it computes a checksum of the file's contents and stores it in the checksum column of flyway_schema_history. On every later start, Flyway recomputes the checksum of each file on the classpath and compares it with the stored value. This is the validate-on-migrate setting, which is on by default. If any applied migration no longer matches, Flyway stops and the application fails to start.
SELECT installed_rank, version, description, checksum, success
FROM flyway_schema_history
ORDER BY installed_rank;A typical failure message looks like this:
Migration checksum mismatch for migration version 1
-> Applied to database : 1234567890
-> Resolved locally : -987654321Why checksums change
- Someone edited an applied migration, even by adding a comment or a space.
- Line endings changed, for example from Git's
autocrlfconverting LF to CRLF. - A merge or formatter rewrote the file.
Flyway treats all of these as modifications. It cannot tell a harmless whitespace change from a changed ALTER statement.
Repeatable migrations are the exception. Their checksum is expected to change, and Flyway re-applies them when it does.
Choosing a recovery path
- Revert the file. This is the best fix when the edit was accidental. Check the line endings if the diff looks empty.
- Add a new migration. If the schema needs to change, write
V4__...sql. Never editV1to make the change. - Repair. If the edit was cosmetic and you have confirmed the database matches,
repairrewrites the stored checksums to match the files.
Do not run repair to silence a mismatch you do not understand. It hides real drift between environments.
Running repair
Repair is a Flyway command, not a startup default. With the Spring Boot integration, you can call it from a FlywayMigrationStrategy bean:
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@Profile("repair")
class FlywayRepairConfig {
@Bean
FlywayMigrationStrategy repairThenMigrate() {
return flyway -> {
flyway.repair();
flyway.migrate();
};
}
}The @Profile("repair") annotation keeps this out of normal runs. Start the application once with --spring.profiles.active=repair, then remove the profile. You can also run flyway repair from the command line or the Maven or Gradle plugin.
Failed migrations
A migration can fail partway through. PostgreSQL runs DDL inside a transaction, so a failed script rolls back and leaves no trace. MySQL and some other databases do not roll back DDL. They leave a row with success = false, and Flyway refuses to continue.
To recover:
- Inspect the database and manually undo whatever the script did before it failed.
- Fix the script. Since it never completed, editing it is safe.
- Run
repair, which removes the failed row, then start the application again.
Related validation errors
- Detected applied migration not resolved locally. The database has a version whose file is missing. Restore the file. Do not delete the history row.
- Validate failed: migrations have failed validation. This is the general wrapper message. Read the lines below it.
Exercise
A teammate fixes a typo in a comment inside V2_adddisplay_name.sql, which production already applied. The next deployment fails with a checksum mismatch. What are two safe ways to resolve it, and which would you prefer?
Show answer
Revert the comment change, or run repair so the stored checksum matches the edited file. Reverting is preferable because it leaves the history untouched and needs no database change. Repair is acceptable only because the edit is cosmetic and you have confirmed the schema is unaffected.