Stop 13 · Databases, JDBC & JPATopics · 12/12

Database Migrations with Flyway

Part of the Java Roadmap

In short

"It works on my machine" often means "my database has a column yours doesn't". Flyway keeps every laptop, test server and production database on exactly the same schema, one numbered script at a time.

Flyway applies numbered SQL scripts like V1__create_users.sql in order and records which ones ran, so every developer and server has the same schema. Spring Boot runs Flyway automatically at startup.

The jaadu in a yellow hard hat ticks a line in a thick register at a site office while a half-built building rises floor by floor behind it.

Think of it like this

Building floor by floor

A builder in Noida adds floors in order: ground, first, second. A register at the site office notes which floors are complete. A new worker checks the register and starts at the next floor, never redoing the first. Flyway's scripts are the floors, and its flyway_schema_history table is the register.

Watch it happen

Migrations applied in order

push(V1__create_students.sql)
V1__create_students.sql

Top of the stack is coloured

1/4First app start on an empty database. Flyway runs V1 and records it in flyway_schema_history.

The big ideas

What you need to know

Versioned names

Files are named V<version>__<description>.sql, with two underscores, in src/main/resources/db/migration.

History table

flyway_schema_history records each script with a checksum, so Flyway knows what ran and spots edits.

Never edit an applied script

Changing V2 after it ran makes the checksum fail validation. Add a new version instead.

Runs at startup

Add flyway-core (plus flyway-database-postgresql for PostgreSQL) and Spring Boot migrates before the app serves requests.

See it in code

Walk through the code

sql
1-- src/main/resources/db/migration/V1__create_students.sql2CREATE TABLE students (3    id         BIGSERIAL PRIMARY KEY,4    name       VARCHAR(100) NOT NULL,5    city       VARCHAR(60),6    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP7);8 9-- src/main/resources/db/migration/V2__add_email_to_students.sql10ALTER TABLE students ADD COLUMN email VARCHAR(150);11CREATE UNIQUE INDEX ux_students_email ON students (email);12 13-- src/main/resources/application.properties14-- spring.jpa.hibernate.ddl-auto=validate15-- spring.flyway.enabled=true   (the default when Flyway is on the classpath)

1/3V1 creates the table. It runs once on every new database.

Two PostgreSQL migration files. With ddl-auto=validate, Hibernate checks that your entities match what Flyway built.

Save yourself the pain

Common mistakes

Oops

Editing V1 after it has run on a shared database.

Do this instead

Flyway fails with a checksum mismatch. Write V3 with the change instead.

Oops

Using one underscore: V2_add_email.sql.

Do this instead

The separator is two underscores, V2__add_email.sql. Otherwise Flyway won't treat it as a migration.

Oops

Letting both Hibernate ddl-auto=update and Flyway change the schema.

Do this instead

Pick one owner. Use Flyway for changes and ddl-auto=validate.

Your mission

Three migrations

~25 min

Quick check

You need to rename a column that V3 created last month. What should you do?

Go deeper

  1. 1Add Flyway to a project and write V1 and V2 migration scripts.
  2. 2Never edit a migration that has already run; add a new one instead.
  3. 3Set spring.jpa.hibernate.ddl-auto to validate so Hibernate checks, not changes, the schema.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge