The 30 second answer
I keep schema changes as versioned files in the repo and let Flyway or Liquibase apply the ones that have not yet run, on startup or in CI, against a history table. Each script has a unique version, I never edit a migration that has already been applied in any environment, and I write backwards-compatible changes so a rolling deploy does not break old app instances. Spring Boot auto-configures both when the dependency and locations are present.
A migration tool treats schema as code. Flyway and Liquibase each keep a history table (flyway_schema_history, databasechangelog) and, on a run, apply only the scripts that are not yet recorded.
Flyway. Versioned migrations are named V<version>__<description>.sql (two underscores). Spring Boot, when org.flywaydb:flyway-core is on the classpath, runs them at startup from classpath:db/migration by default. Repeatable migrations (R__) re-run when their checksum changes, useful for views. I do not change a V file after it has been applied; Flyway will fail on a checksum mismatch. I then add a new version.
Liquibase. Changes live in a changelog (XML, YAML, JSON or SQL) of change sets with an id and author. It has the same "already applied" model and a richer set of database-agnostic change types. Spring Boot also auto-configures Liquibase when its dependency is present.
For a rolling deploy I split incompatible changes: add a nullable column and ship the new code, then backfill, then add constraints in a later migration. I run migrations from CI against a throwaway database in tests, and I fail the app start if migrations cannot be applied.
-- db/migration/V3__add_order_notes.sql
ALTER TABLE orders ADD COLUMN notes TEXT;# application.yml (Spring Boot)
spring:
flyway:
enabled: true
locations: classpath:db/migration
Sources (5)
- Flyway: How Flyway works (versioned migrations and the history table)documentation.red-gate.com
- Flyway: Naming versioned migrationsdocumentation.red-gate.com
- Spring Boot reference: Flywaydocs.spring.io
- Spring Boot reference: Liquibasedocs.spring.io
- Liquibase: Changesetdocs.liquibase.com