BBackend CourseLeroy Labs
0/0
Search the course

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.

● beginnerSpring-Boot-StoreHostel-Web-AppReport-Manager-2IR3D (Kontentt)SperoTek (in dev)

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

Report-Manager-2resources/db/changelog/db.changelog-master.yaml · scripts/*.sql

Changesets 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.

IR3D (Kontentt)db/changelog/db.changelog-001..099-*.yaml

99 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.

SperoTek (in dev)db/changelog/scripts/001..018-*.sql

The 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.

Hostel-Web-Appdb-init/00-init-extensions.sql · changelog 001–006

Extensions (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
  • 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 x is fast in modern PG, but CREATE INDEX locks writes. Use CREATE INDEX CONCURRENTLY (with runInTransaction: false in 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).

Check yourself

1. Why is ddl-auto=update dangerous in production?
2. What does Liquibase store to know a changeset ran?