Sub topic 1
Why Version Your Database Schema
Prerequisites
This chapter assumes you know basic Spring Boot (starters and application.yml), SQL DDL and DML, and how to add a Maven or Gradle dependency. You should also know a relational database such as PostgreSQL and a basic Git workflow. No Flyway knowledge is needed.
The problem with hand-applied changes
Application code is versioned in Git. Every change is reviewed, tagged and can be rolled back. The database schema that the code depends on often gets no such treatment.
A common pattern looks like this:
- A developer adds a column on their local database.
- They push code that reads the column.
- A teammate pulls the code and gets an error, because their database has no such column.
- Someone sends a SQL snippet in chat. Staging gets the change, and production gets it later, or never.
Each environment now has a slightly different schema. The code and the database can no longer be matched to one another, and nobody can say with certainty which changes a given database has received.
What a migration tool does
A schema migration is a small, ordered change to the database structure, stored as a file. A migration tool applies those files to a database in order. It also records which ones it has already applied, so no file runs twice.
Flyway is such a tool. Your migrations live in the project next to the code, and Flyway applies them automatically when the application starts. The schema then changes through the same review and deployment process as the code.
What you gain
| Without versioned migrations | With Flyway |
|---|---|
| Schema differs between environments | Every environment applies the same ordered files |
| New developers copy a dump or run guesswork scripts | An empty database is built from scratch by starting the app |
| Unknown whether production has a change | A history table lists every applied change |
| Changes are not reviewed | Migration files go through pull requests |
| Code and schema can be deployed out of step | Code and migrations ship in one artifact |
How the pieces fit together
The history table is the key idea. The database itself remembers its own version, so Flyway can bring any database, whether empty or partly up to date, to the current state.
A small example
Suppose the project holds two files:
-- V1__create_app_user.sql
CREATE TABLE app_user (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
username VARCHAR(100) NOT NULL UNIQUE
);-- V2__add_display_name.sql
ALTER TABLE app_user ADD COLUMN display_name VARCHAR(200);A new developer's empty database receives both files. A production database that already has V1 receives only V2. Both end up with the same schema. Later chapters explain the naming rules and the history table in detail.
One rule to remember
Once a migration has been applied somewhere shared, you do not edit it. You add a new migration instead. Flyway checks this and refuses to start if an applied file has changed. This rule keeps every environment on the same path, and it is why a migration file is a permanent record rather than a draft.
Common mistakes to avoid
- Letting Hibernate create or update the schema in production.
ddl-auto: updatecannot express renames, data fixes or ordering, and it leaves no record of what it did. - Applying a "quick fix" directly in production and never writing it down as a migration.
- Editing an old migration to fix a typo after teammates have already applied it.
Exercise
A teammate fixes a typo in a column name by editing V2_adddisplay_name.sql, which your staging database has already applied. What is the correct approach, and why?
Show answer
Leave V2 unchanged and add a new migration, such as V3, that renames the column. Staging has already recorded V2 in its history table, so changing the file would no longer match that record. Flyway would detect the mismatch and fail at startup. A new migration also gives every environment the same change history.
Exercise
Name two benefits of keeping migrations in the same repository as the application code.
Show answer
Schema changes go through code review and version control like any other change. Code and the schema it needs are deployed together, so they cannot drift apart. A new environment can also be built from scratch by starting the application.