Skip to main content
PreparationZone Logo
PrepZone

Schema Migrations with Flyway

Versioned SQL migrations, ddl-auto pitfalls and zero-downtime schema changes.

Pass checkpoint below

Why this matters

  • ddl-auto: update is unpredictable in production — Flyway gives you repeatable, reviewable schema changes.
  • Migration files are the database equivalent of git commits: versioned, auditable, and reversible in principle.
  • Zero-downtime deployments require careful migration ordering — Flyway's versioning model supports this.

Versioned SQL

L1 · Fresher

Files like V1__create_books.sql apply in order; Flyway records history in flyway_schema_history.

Boot integration

L2 · Intermediate

Add flyway-core and migrations under db/migration — Boot runs them on startup before serving traffic.

Backward-compatible changes

L3 · Advanced

Expand-contract pattern: add nullable columns first, backfill, then enforce NOT NULL in a later release.

Repair and out-of-order

L4 · Expert

Know flyway repair for failed migrations in lower environments; production changes always go through reviewed scripts.

V1__baseline.sql
V2__add_index.sql
V3__column.sql
Schema matches code
Scripts run in version order; history is stored in flyway_schema_history.

Add Flyway

Java
<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
</dependency>
<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-database-postgresql</artifactId>
</dependency>

Flyway auto-configures when on the classpath. Set Hibernate to validate only:

Java
spring:
  jpa:
    hibernate:
      ddl-auto: validate
  flyway:
    enabled: true
    locations: classpath:db/migration

Migration file naming

Files live in src/main/resources/db/migration/:

Java
V1__create_books_table.sql
V2__create_authors_table.sql
V3__add_author_id_to_books.sql
V4__create_orders_table.sql

Convention: V{version}__{description}.sql. Double underscore separates version from description. Versions must be unique and sequential.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 7 sections

  • V1: Initial schema
  • V2: Add authors
  • V3: Seed reference data
  • Repeatable migrations
  • Baseline for existing databases
  • Zero-downtime patterns
  • Flyway in CI

Test yourself

Answer these before moving on — recall is what makes it stick.