A minimal hands-on project to learn Flyway — a database migration tool that versions your schema using plain SQL files.
This project uses H2 (an embedded Java database stored in target/), so you
can run everything end-to-end without installing a database.
Flyway tracks the state of your database schema in a metadata table called
flyway_schema_history. When you run a migration:
- Flyway scans a folder for migration files.
- It compares them against
flyway_schema_history. - It runs any migrations that have not been applied yet, in version order.
This makes schema changes reproducible, versioned in git alongside code, and safe to apply across environments (dev, staging, prod).
flyway-getting-started/
├── pom.xml
├── src/main/java/com/example/App.java
└── src/main/resources/db/migration/
├── V1__Create_person_table.sql
├── V2__Insert_initial_people.sql
├── V3__Add_email_to_person.sql
└── R__Person_summary_view.sql
Flyway derives behavior from the filename:
| Prefix | Meaning | Example |
|---|---|---|
V |
Versioned — runs once, in version order | V1__Create_person_table.sql |
R |
Repeatable — re-runs whenever it changes | R__Person_summary_view.sql |
U |
Undo (Flyway Teams) | U2__Drop_email_from_person.sql |
The __ (two underscores) separates the version from the human-readable
description. Spaces become underscores.
mvn package
java -jar target/flyway-getting-started-1.0.0.jarThe app calls Flyway.migrate(), then prints the contents of the person
table, the person_summary view, and the flyway_schema_history table so you
can see exactly what Flyway did.
mvn flyway:info # show pending and applied migrations
mvn flyway:migrate # apply pending migrations
mvn flyway:validate # check that applied migrations match files on disk
mvn flyway:clean # drop everything (DANGER — disabled by default in prod)The Maven plugin reads its config from the
<configuration>block inpom.xml. In a real project you'd usually externalise this into aflyway.conffile or pass-Dflyway.url=...on the command line.
Once the project runs cleanly, experiment to build intuition:
-
Add a new versioned migration Create
V4__Add_age_to_person.sql:ALTER TABLE person ADD COLUMN age INT;
Re-run — only
V4applies.V1–V3are skipped because they're already inflyway_schema_history. -
Edit a repeatable migration Change
R__Person_summary_view.sqlto also exposeMAX(id). Re-run — Flyway detects the new checksum and re-applies it. (Try the same edit on aVfile and you'll get a checksum-mismatch error — that's the safety net.) -
Break the safety net intentionally Edit
V2__Insert_initial_people.sqland re-run. Flyway refuses to migrate: already-applied versioned files are immutable. Usemvn flyway:repairto reset the checksum after you've reverted the change. -
Inspect the history table The app prints it, but you can also run:
mvn flyway:info
| Concept | What it does |
|---|---|
flyway_schema_history |
Metadata table Flyway uses to track applied migrations |
Versioned (V) |
Applied once, in order, immutable after apply |
Repeatable (R) |
Re-applied when checksum changes; great for views & procs |
migrate |
Apply all pending migrations |
info |
Show what's applied, pending, missing |
validate |
Detect drift between applied migrations and current SQL files |
baseline |
Mark an existing DB as being at a starting version |
repair |
Fix flyway_schema_history after manual intervention |
clean |
Drop all objects in the schema (disable in prod!) |
- Real-world DB: swap H2 for PostgreSQL/MySQL by changing the JDBC URL +
driver dependency in
pom.xml. The migrations themselves usually need minor tweaks for vendor-specific SQL. - Java-based migrations: classes implementing
JavaMigrationfor logic that's awkward in SQL (data backfills with conditional branching, etc.). - Callbacks:
beforeMigrate,afterMigrate, etc. for hooks. - CI/CD: run
mvn flyway:migrateas a deploy step — same migrations, every environment.