Sub topic 3
Writing versioned SQL migrations
This chapter assumes you have a Spring Boot project with flyway-core and the database module for your database on the classpath, and that you can write basic SQL DDL and DML. Versioned migrations are the main tool Flyway gives you. Each one is a SQL file that Flyway applies exactly once, in version order.
The naming convention
Flyway finds migrations by file name. Put the files in src/main/resources/db/migration, which is the default location. A versioned migration name has four parts.
| Part | Example | Meaning |
|---|---|---|
| Prefix | `V` | Marks a versioned migration |
| Version | `2` or `2.1` | Decides the order of application |
| Separator | `__` | Two underscores |
| Description | `add_display_name` | Text for humans, with underscores shown as spaces |
The file V2__add_display_name.sql therefore has version 2 and the description "add display name". Flyway compares versions numerically, so V10 runs after V2. A single underscore, a lowercase v, or a missing description makes Flyway fail or ignore the file.
Your first migration
The first migration creates the app_user table.
-- src/main/resources/db/migration/V1__create_app_user.sql
CREATE TABLE app_user (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
username VARCHAR(100) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);On startup Flyway runs this file against an empty database, creates the table, and records the migration as applied. On the next startup it sees the record and skips the file.
Changing the schema with a new migration
To change the table, add a new file with a higher version. Do not edit V1.
-- src/main/resources/db/migration/V2__add_display_name.sql
ALTER TABLE app_user ADD COLUMN display_name VARCHAR(200);The next startup applies only V2, because V1 is already recorded.
Rules that keep migrations safe
- Never edit an applied migration. Flyway stores a checksum of each file. If the file changes after it was applied, validation fails at startup. A later chapter covers this failure.
- Keep each migration small. One logical change per file makes failures easier to diagnose.
- Use unique, increasing versions. Two files with the same version are an error. A new file must have a higher version than the latest applied one. Otherwise Flyway reports it as out of order.
- Write plain, deterministic SQL. The file runs once per database, so it should not depend on data that differs between environments.
Many databases, including PostgreSQL, run a migration inside a transaction. If a statement fails, the whole file is rolled back, and you can fix the file and retry. Some databases commit DDL statements immediately, so a failed file can leave the schema half changed. Keep migrations small for that reason.
Common errors
- The migration never runs. The file is outside
db/migration, or the name does not match the pattern. Check for a single underscore or a wrong prefix. - A "validate failed" error. An applied file was changed. Restore the original content and put the change in a new version.
- A "detected resolved migration not applied" error. A new file has a lower version than the latest applied one. Give it a higher version.
Exercise
You deployed V3_addphone.sql and then noticed the column should be VARCHAR(30) instead of VARCHAR(20). What should you do, and why not edit V3?
Show answer
Add a new migration, for example V4_widenphone.sql, that alters the column type. Editing V3 changes its checksum, so Flyway's validation fails on every database where V3 was already applied.