8000 GitHub - gsaini/flyway-getting-started: Hands-on getting-started project for Flyway database migrations (Maven + H2) · GitHub
[go: up one dir, main page]

Skip to content

About

Hands-on getting-started project for Flyway database migrations (Maven + H2)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Flyway Getting Started

Java Apache Maven Flyway H2 Database SQL

GitHub stars GitHub forks GitHub last commit

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.


What is Flyway?

Flyway tracks the state of your database schema in a metadata table called flyway_schema_history. When you run a migration:

  1. Flyway scans a folder for migration files.
  2. It compares them against flyway_schema_history.
  3. 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).


Project layout

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

Migration file naming

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.


How to run

Option 1 — via the Java app (recommended for first run)

mvn package
java -jar target/flyway-getting-started-1.0.0.jar

The 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.

Option 2 — via the Flyway Maven plugin (CLI-style)

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 in pom.xml. In a real project you'd usually externalise this into a flyway.conf file or pass -Dflyway.url=... on the command line.


Try it yourself

Once the project runs cleanly, experiment to build intuition:

  1. Add a new versioned migration Create V4__Add_age_to_person.sql:

    ALTER TABLE person ADD COLUMN age INT;

    Re-run — only V4 applies. V1–V3 are skipped because they're already in flyway_schema_history.

  2. Edit a repeatable migration Change R__Person_summary_view.sql to also expose MAX(id). Re-run — Flyway detects the new checksum and re-applies it. (Try the same edit on a V file and you'll get a checksum-mismatch error — that's the safety net.)

  3. Break the safety net intentionally Edit V2__Insert_initial_people.sql and re-run. Flyway refuses to migrate: already-applied versioned files are immutable. Use mvn flyway:repair to reset the checksum after you've reverted the change.

  4. Inspect the history table The app prints it, but you can also run:

    mvn flyway:info

Key concepts cheat sheet

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!)

Where to go next

  • 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 JavaMigration for logic that's awkward in SQL (data backfills with conditional branching, etc.).
  • Callbacks: beforeMigrate, afterMigrate, etc. for hooks.
  • CI/CD: run mvn flyway:migrate as a deploy step — same migrations, every environment.

About

Hands-on getting-started project for Flyway database migrations (Maven + H2)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0