Skip to content
Ameen Abozaid
JNR-033

Schema migrations

  • 6 Lessons
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Flyway or Liquibase
  • versioned migrations
  • seed data

Learn first

2 h of practice (estimate)

Learning outcomes

  1. Version the schema with Flyway migrations that run on startup.
  2. Write forward-only migrations and explain why applied migrations are never edited.
  3. Separate reference/seed data from test data.
  4. Validate entity mappings against the migrated schema (ddl-auto=validate).

Lessons

  1. 01Why the schema needs version control
  2. 02Flyway basics
  3. 03Writing safe migrations
  4. 04Seed data
  5. 05Liquibase in brief
  6. 06Demo: baselining Shop with Flyway and adding V2

Lab and exercises

Lab

JNR-033-LAB1 · Migrate Shop

Outcomes: 1–4 · Time: ~2 h

  1. Add Flyway and convert the manual schema to V1__baseline.sql.
  2. Set ddl-auto=validate; fix any mapping/schema mismatch it reports.
  3. Add V2__add_sku.sql: add sku, backfill existing rows, then set not null and a unique index.
  4. Put reference data (order statuses) in V3; keep demo data in test-only scripts.
  5. Reach the same schema on a fresh database and on an existing one (baseline).

Acceptance: fresh database and existing database both reach the same state.

Exercises (assessment: write a migration for a new requirement)

IDOutcomeTask
JNR-033-EX011Name three migration files correctly and explain how Flyway orders them.
JNR-033-EX022An applied V4 has a typo. Explain what happens if you edit it and what to do instead.
JNR-033-EX032Write the safe three-step migration to add a not null column to a table with rows.
JNR-033-EX043Classify as reference or test data: order statuses, 50 fake customers, country list, a demo admin password.
JNR-033-EX054Show what ddl-auto=validate reports when an entity has a field that the table lacks.
JNR-033-EX061, 2Write a migration for a new requirement: "products have an optional weight_grams that must be positive when present".
Show solutions
  • EX01 V1__baseline.sql, V2__add_sku.sql, V3__seed_status.sql; ordered by version number, not alphabetically as text.
  • EX02 The checksum differs and Flyway refuses to start; add V5__fix_typo.sql instead.
  • EX03 alter table ... add column x text; then update ... set x = ...; then alter table ... alter column x set not null;.
  • EX04 Reference: statuses, countries. Test: fake customers, demo admin password (never in production).
  • EX05 Startup fails with a schema-validation error naming the missing column.
  • EX06 alter table product add column weight_grams integer; alter table product add constraint ck_product_weight check (weight_grams is null or weight_grams > 0);

Quiz

Outcomes: O1 Flyway on startup · O2 forward-only migrations · O3 seed vs test data · O4 schema validation.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Flyway records applied migrations in:O1
  2. 2A correct migration file name is:O1
  3. 3Migrations run:O1
  4. 4An already applied migration has an error. You should:O2
  5. 5Editing an applied migration causes:O2
  6. 6Safe way to add a required column to a populated table:O2
  7. 7Order status codes needed everywhere are:O3
  8. 8Fake demo customers belong in:O3
  9. 9ddl-auto=validate does:O4
  10. 10ddl-auto=update in production is:O4

Practical part

Six exercises JNR-033-EX01 to EX06 plus the lab; the assessment is writing a migration for a new requirement (EX06). Pass: EX06 correct and safe, at least five of six exercises correct, and fresh and existing databases reaching the same state.

Common mistakes

Editing an applied migration; ddl-auto=update in production.

Assessment

Write a migration for a new requirement.

Career relevance

Zero-downtime migrations (MID-020).

Type to search levels, phases, modules and lessons.