Schema migrations with Liquibase (and ddl-auto validate)
Versioned, repeatable schema changes. Changelogs, changesets, raw-SQL includes, seed data, rollbacks, and why every project sets ddl-auto to validate.
The concept
Your schema is code, so it needs version control and repeatable deployment. A migration tool keeps an ordered list of changes and records which ones have been applied to each database, in the DATABASECHANGELOG table for Liquibase.
# db/changelog/db.changelog-master.yaml
databaseChangeLog:
- include: { file: db/changelog/db.changelog-001-initial-schema.yaml }
- include: { file: db/changelog/db.changelog-002-index.yaml }
# db.changelog-002-index.yaml
databaseChangeLog:
- changeSet:
id: 002-index
author: leroy
changes:
- sqlFile: { path: db/changelog/scripts/index.sql, splitStatements: true }
rollback:
- sql: DROP INDEX IF EXISTS idx_hostels_location;
On startup Spring Boot runs Liquibase before JPA initialises. Each changeset runs once, and its checksum is stored. Editing an applied changeset fails startup with a checksum error, so always add a new one instead.
ddl-auto: let Hibernate validate, never generate
spring.jpa.hibernate.ddl-auto: validate
| Value | Effect | Use |
|---|---|---|
create / create-drop |
Drop and recreate the schema | Throwaway demos only |
update |
Hibernate guesses ALTERs | Never in prod: it can’t rename, drops nothing, creates poor indexes |
validate |
Fails startup if entities and tables disagree | Yours, and correct |
none |
Does nothing | IR3D (relies on Liquibase alone) |
In your projects
resources/db/changelog/db.changelog-master.yaml · scripts/*.sqlChangesets 001–022 including raw SQL (initial-schema.sql, export-jobs-schema.sql, gtec-accounts-schema.sql). Seed data is managed as migrations too: default-data.sql, add-admin.sql and db.changelog-006-seed-employees.yaml.
db/changelog/db.changelog-001..099-*.yaml99 changesets, a real production history, e.g. -045-finance-ledger, -081-webhook-events, -099-campaign-carryover. The liquibase-maven-plugin is configured for running diffs and status from the CLI.
db/changelog/scripts/001..018-*.sqlThe YAML changelogs point at numbered SQL files (001-event-publication.sql creates Spring Modulith’s outbox table). Writing raw SQL lets SperoTek use Postgres features Liquibase’s YAML DSL can’t express: triggers, deferrable constraints, partial indexes, PL/pgSQL functions.
db-init/00-init-extensions.sql · changelog 001–006Extensions (postgis, uuid-ossp) are created by a Docker init script before Liquibase runs. Later changesets show the schema evolving: rename-room-price, unique-phone.
Alternatives
| Tool | Notes |
|---|---|
| Liquibase (yours) | Many formats, rollback, preconditions, diff tooling |
| Flyway | Simpler: V1__init.sql, V2__add_index.sql. Very popular; rollback (undo) is a paid feature |
Hibernate ddl-auto=update |
Not a migration tool |
| Atlas, Sqitch, golang-migrate | Other ecosystems |
Related concepts you must know
- Forward-only in practice. Rollbacks rarely run in prod. Instead, write migrations that are backward compatible with the previous app version (expand/contract, see the bonus module).
- Long locks.
ALTER TABLE ... ADD COLUMN ... DEFAULT xis fast in modern PG, butCREATE INDEXlocks writes. UseCREATE INDEX CONCURRENTLY(withrunInTransaction: falsein Liquibase). - Contexts and labels. Run seed changesets only in
dev(context: dev). - Testing migrations. Your Testcontainers tests run every migration on a real Postgres, which is the best possible check (Module 7).