Flyway is a database migration tool that versions your schema as ordered scripts and applies each one exactly once. In Spring Boot it runs automatically at startup: Boot detects Flyway on the classpath, finds the scripts in db/migration, applies the pending ones and records them in a history table. This keeps every environment, from a laptop to production, on the same schema as the code.
Treat the database schema as code that is reviewed, versioned and deployed with the application, instead of changed by hand.
Flyway keeps a table, flyway_schema_history by default, in your database. Each row stores a version, a description, a checksum of the script, who applied it and when. On every start Flyway compares the scripts it finds with that table:
This gives you a reliable history and protects against silent drift between environments.
Add the core library plus the module for your database. Since Flyway 10 the database support is split into separate artifacts, so PostgreSQL, MySQL and others each need their own dependency:
For MySQL use flyway-mysql instead. Spring Boot manages these versions through its dependency management.
Configure it in application.yml:
Setting ddl-auto: validate makes Hibernate check that entities match the schema Flyway built, and never change it itself.
Place scripts in src/main/resources/db/migration. The file name decides the order and must follow the pattern V<version>__<description>.sql, with two underscores:
Never edit a script after it has run anywhere beyond your own machine. Add a new version that changes the schema further.
Scripts named R__<description>.sql are repeatable. They run after all versioned scripts, and again whenever their content changes. They suit views, functions and reference data that you redefine in full:
For logic that is awkward in SQL, write a Java migration by extending BaseJavaMigration in the db.migration package, and Flyway runs it in the same ordered history.
| Situation | Recommendation |
|---|---|
| Existing database without Flyway history | Set a baseline version with baseline-on-migrate once, then add later migrations |
| Several app instances starting together | Flyway takes a lock, so one instance migrates while others wait |
| Large tables | Prefer additive, backward compatible changes (add a nullable column, backfill, then enforce) |
| Rollbacks | Undo migrations are a paid feature; in practice roll forward with a new migration |
| Test data | Put it in a separate location such as db/testdata enabled only for dev or test profiles |
For zero-downtime releases, split risky changes across two deployments so both the old and new application versions work with the intermediate schema. In larger organisations some teams run migrations as a separate pipeline step instead of at application start, which you can do by setting spring.flyway.enabled=false and running the Flyway CLI or container.
Both are supported by Spring Boot auto-configuration. Flyway favours plain SQL files and a simple naming convention. Liquibase uses changelogs in XML, YAML, JSON or SQL, with more declarative features. If your team is comfortable with SQL, Flyway has the lower learning curve.
In classpath:db/migration by default. Change it with spring.flyway.locations, and use {vendor} in the path for database-specific scripts.
An applied migration file was modified. Restore the original file and put the change in a new version. Use flyway repair only when you understand the cause.
Use validate or none. Letting update or create change the schema alongside Flyway causes drift and conflicts.
Set spring.flyway.enabled=false in the test profile, or keep it enabled against a Testcontainers database to test the real migrations.
Flyway applies versioned SQL scripts in order, records them in flyway_schema_history and validates checksums, so every environment ends up with the same schema. In Spring Boot you add flyway-core and the database module, put V__ scripts in db/migration, set Hibernate to validate, never edit applied scripts, and roll forward with new migrations.