Flyway Migrations in Spring Boot: Numbered SQL Files, Seed Data and the History Table
Relay's tables and its 50 seed tickets come from numbered SQL files. Watch Flyway apply V1, V2 and a repeatable file on first start, run only V3 on the next, and refuse to start when someone edits a file that already ran.
What you will learn
- Name versioned (V) and repeatable (R) migration files so Flyway runs them in order
- Seed data with a migration and read flyway_schema_history
- Add a schema change as a new file and see only that file run
- Explain the checksum mismatch and why ddl-auto validate pairs with Flyway
Before you start
- Connect Spring Boot to PostgreSQL
- JPA Entity Mapping: Ids, Enums and Dates
- Basic SQL
Where do Relay's tables come from? Not from Hibernate guessing at startup. They come from numbered SQL files in db/migration, and the database remembers which ones already ran. That tool is Flyway.
Even the 14 tickets you see on Olivia Bennett's page were put there by a migration file the first time Relay started. Nobody typed them in.
Set up: one starter, one rule for Hibernate
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-flyway</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
</dependency>
spring:
jpa:
open-in-view: false # no database work while the page is being drawn
hibernate:
ddl-auto: validate # Flyway owns the tables; Hibernate only checks that they match the entities
flyway:
locations: classpath:db/migration
ddl-auto: validate is the important line. Hibernate builds nothing. It only checks that the tables match the entities, and stops the app if they don't.
The file name is the rule
A versioned migration is named: capital V, a number, two underscores, a description, .sql. The number sets the order. Relay starts with three files:
V1__schema.sql: the tablesV2__seed.sql: the dataR__demo_data.sql: a repeatable file (no version; runs again whenever its text changes)
Version 1: the tables
It's plain SQL, the same SQL you'd type in psql. Here is the ticket table from it:
create table ticket (
id bigint generated by default as identity primary key,
subject varchar(200) not null,
description text not null,
status varchar(20) not null,
priority varchar(20) not null,
customer_id bigint not null references customer (id),
assignee_id bigint references agent (id), -- null = nobody has taken it yet
created_at timestamptz not null,
updated_at timestamptz not null
);
Version 2: data is a migration too
-- Relay seed data: 5 customers, 3 agents, 50 tickets, 80 comments, 4 tags.
-- Names and tickets are made up for the course.
insert into customer (id, name, email) values
(1, 'Olivia Bennett', '[email protected]'),
(2, 'Noah Carter', '[email protected]'),
...
Ordinary inserts, no Java. The ids are set right here so the tickets can point at them.
The repeatable file
-- Demo data. Flyway runs this file again whenever it changes,
-- so it must be safe to run twice.
insert into tag (name) values ('demo') on conflict (name) do nothing;
First start: Flyway goes first, then Hibernate checks
INFO Database: jdbc:postgresql://localhost:5432/relay (PostgreSQL 18.6)
INFO Creating Schema History table "public"."flyway_schema_history" ...
INFO Current version of schema "public": << Empty Schema >>
INFO Migrating schema "public" to version "1 - schema"
INFO Migrating schema "public" to version "2 - seed"
INFO Migrating schema "public" with repeatable migration "demo data"
INFO Successfully applied 3 migrations to schema "public", now at version v2
INFO Started RelayApplication in 5.548 seconds (process running for 6.098)
What Flyway remembers
select installed_rank as rank, version, description,
script, checksum, success
from flyway_schema_history order by installed_rank;
rank | version | description | script | checksum | success
------+---------+-------------+------------------+-------------+---------
1 | 1 | schema | V1__schema.sql | -1788030169 | t
2 | 2 | seed | V2__seed.sql | 1793164679 | t
3 | | demo data | R__demo_data.sql | -1589761259 | t
One row per file: its version, its name, a checksum (a number worked out from the file's text) and whether it worked. The repeatable file has no version, so that cell is empty.
Add a file, only that file runs
Agents sort their queue by priority, so we want an index. That's a new file, V3__ticket_priority_index.sql:
-- Agents sort their queue by priority.
create index idx_ticket_priority on ticket (priority);
INFO Current version of schema "public": 2
INFO Migrating schema "public" to version "3 - ticket priority index"
INFO Successfully applied 1 migration to schema "public", now at version v3
Flyway saw version 2 in the history, found 3 in the folder, and ran just that one. Start once more and it says Schema "public" is up to date. No migration necessary.
Change the repeatable file (say values ('demo'), ('vip')) and only it runs again; V1 to V3 stay untouched. That's why it uses on conflict do nothing: it has to be safe to run twice.
Break it: edit a file that has already run
In V1__schema.sql, change the customer name varchar(100) to varchar(120). One small change. The app doesn't start:
Validate failed: Migrations have failed validation
Migration checksum mismatch for migration version 1
-> Applied to database : -1788030169
-> Resolved locally : 1834096396
Either revert the changes to the migration, or run repair to update the schema history.
The file's checksum no longer matches the one recorded when it ran. Flyway can't know whether your database has the old column or the new one, so it stops. Put 100 back. The rule: never edit a file that has run. Put the change in a new file.
Who owns the tables?
| Setting | Who builds | Risk |
|---|---|---|
ddl-auto: update | Hibernate, by guessing | rename a field and you get a new column; the old one keeps the data |
ddl-auto: validate | nobody, it only checks | stops the app if tables and entities differ |
| Flyway | numbered SQL files | same result on every machine, one new file per change |
Relay uses Flyway together with validate.
Common mistakes
- One underscore.
V3_index.sqlisn't picked up. It's always two. - Editing V1 "just a little". It works on your new laptop and fails on every database where V1 already ran.
- A repeatable file that isn't safe to repeat. A plain insert in
R__fails on a unique column the second time. - Keeping
ddl-auto: updatenext to Flyway. Two owners for the same tables. Usevalidate.
Try it yourself
Tickets can have a due date. Write V4__ticket_due_date.sql with alter table ticket add column due_date date; and run. You should see Successfully applied 1 migration to schema "public", now at version v4. Then query the history table: how many rows does it have now?
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Spring Boot Repository and Service Layer: Save and Read Data Without Writing SQL
A one-line JpaRepository interface and a small @Service class save and read blog posts with no SQL. Follow one save and one read through every layer, then prove it with a test.
Entities and H2 in Spring Boot: a Post Class Becomes a Table
The blog's cards are typed into HTML. A Post class with @Entity and @Id becomes the POST table in an in-memory H2 database: three pom lines, a few YAML settings, and Hibernate writes the create table SQL at startup.
Seed Data and Posts on the Homepage: CommandLineRunner, the Model and th:each
SeedData, a CommandLineRunner, saves three posts when the POST table is empty. HomeController puts postService.getAll() into the Model, and one postCard inside th:each becomes three cards. Three inserts at startup, one select per visit.
Pagination and Sorting on a Spring Boot Homepage: PageRequest In, Page Out
Eight posts, three to a page, newest first. HomeController turns ?page=1&sort=oldest into a PageRequest, PostService passes it to findAll(Pageable), H2 runs one select with an offset and one count, and the Page that comes back knows it is page 2 of 3.
