Lesson 3 of 6 · Database CI/CD

Database Change Management

Migration-based and state-based changes, how Liquibase and Flyway work, and zero-downtime changes with expand and contract.

Beginner 6 sections 4 min read

Two ways to change a schema

Database CI/CD depends on managing schema changes as code. Most teams use migrations: small, numbered change scripts that are applied in order and recorded in the database, so every environment goes through exactly the same steps. Liquibase and Flyway are the most widely used tools for this.

Migration-basedState-based
What's in GitEach change, as an ordered script: "add column", then "add index"The desired final schema: what every table should look like
How it's appliedRun the scripts not yet applied, in orderA tool compares the desired schema with the database and generates the changes
StrengthsFull control over exactly what runs, including data changes; predictableEasy to see the whole schema; no scripts to write for simple changes
RisksMany scripts over timeGenerated changes can be surprising, such as a rename becoming drop-and-create
ToolsLiquibase, FlywaySQL Server Data Tools (dacpac), Redgate comparison tools, Atlas

Migration-based is the most common approach, especially for Oracle and PostgreSQL, because DBAs can see and control exactly what will run in production.

How migration tools work

CHANGELOG IN GIT 1 · create orders table2 · index on order date3 · customer_vbuck_points Liquibase or Flyway TESTPRODUCTION applied: 1, 2, 3nothing to do applied: 1, 2runs 3, then records it DATABASECHANGELOGDATABASECHANGELOG

Each database has a tracking table, DATABASECHANGELOG for Liquibase or flyway_schema_history for Flyway, recording which changes have run, when, and a checksum of each. When the tool runs, it applies only the changes that aren't recorded yet. That's how one changelog keeps development, test, and production in step, and why a change must never be edited after it's been applied: the checksum would no longer match.

Liquibase

Liquibase changes are called changesets, listed in a changelog. Changesets can be written in XML, YAML, JSON, or plain SQL. DBAs usually prefer formatted SQL: normal SQL with special comments.

--liquibase formatted sql

--changeset kofi:DBA-310-1 labels:vbuck
--comment: V-Buck points earned per order
CREATE TABLE customer_vbuck_points (
  order_id    NUMBER        NOT NULL,
  customer_id NUMBER        NOT NULL,
  points      NUMBER(10)    DEFAULT 0 NOT NULL,
  earned_at   TIMESTAMP     DEFAULT SYSTIMESTAMP NOT NULL,
  CONSTRAINT customer_vbuck_points_pk PRIMARY KEY (order_id),
  CONSTRAINT customer_vbuck_points_order_fk FOREIGN KEY (order_id) REFERENCES orders (order_id)
);
--rollback DROP TABLE customer_vbuck_points;

--changeset kofi:DBA-310-2 labels:vbuck
CREATE INDEX customer_vbuck_points_cust_ix ON customer_vbuck_points (customer_id) ONLINE;  -- ONLINE needs Enterprise Edition
--rollback DROP INDEX customer_vbuck_points_cust_ix;
--changeset author:id
Starts a changeset with a unique ID; including the Jira key links it to its work item.
--rollback
The SQL that undoes the change, used by Liquibase's rollback commands.
labels and contexts
Tags for choosing which changesets run, such as only the vbuck changes, or changes for the test context only, such as test data.
Preconditions
Checks that must pass before a changeset runs, such as "this table doesn't exist yet".
CommandWhat it does
liquibase statusLists changesets not yet applied to this database
liquibase update-sqlShows the exact SQL that update would run, without running it: Liquibase's dry run
liquibase updateApplies the pending changesets and records them
liquibase tag v1.4.0Marks the current state, as a point to roll back to
liquibase rollback v1.3.0Rolls back every changeset applied after a tag
liquibase update-testing-rollbackApplies changes, rolls them back, and applies them again: proves the rollbacks work

Flyway

Flyway, from Redgate, is simpler: each migration is a plain SQL file, named with its version:

db/migration/
├── V1__create_orders.sql
├── V2__index_order_date.sql
├── V3__create_customer_vbuck_points.sql
└── R__sales_summary_view.sql    # repeatable: rerun whenever it changes

flyway migrate applies pending versions in order, flyway info shows what's applied and pending, and flyway validate checks that applied files haven't been changed. Both tools work with Oracle, SQL Server, PostgreSQL, MySQL, and many more.

Zero-downtime changes: expand and contract

Some changes break the running application if made in one step. Renaming a column, for example: the moment it's renamed, the old application code fails. The expand and contract pattern (also called parallel change) splits a breaking change into safe, backward-compatible steps, across several releases:

1 EXPAND2 MIGRATE3 SWITCH 4 CONTRACT add the new columncust_emailbeside the old one copy the data; theapp writes to both the app reads andwrites only thenew column drop the old columnemailonce nothing uses it Every release works with both the old and the new application code.

Each step is small and safe on its own, and the risky part, dropping the old column, happens only when it's certain nothing still uses it. It's how teams change databases without maintenance windows, and a prerequisite for continuous deployment.

Key takeaways

  • Migration-based tools keep every change as an ordered script; state-based tools generate changes from a desired schema.
  • A tracking table in each database records applied changes, so one changelog keeps every environment in step.
  • Never edit an applied migration; add a new one.
  • Liquibase uses changesets, often in formatted SQL with rollbacks, and offers dry runs, tags, and rollback testing.
  • Flyway uses versioned SQL files, such as V3__create_customer_vbuck_points.sql.
  • Expand and contract splits breaking changes into backward-compatible steps, avoiding downtime.