Database Change Management
Migration-based and state-based changes, how Liquibase and Flyway work, and zero-downtime changes with expand and contract.
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-based | State-based | |
|---|---|---|
| What's in Git | Each change, as an ordered script: "add column", then "add index" | The desired final schema: what every table should look like |
| How it's applied | Run the scripts not yet applied, in order | A tool compares the desired schema with the database and generates the changes |
| Strengths | Full control over exactly what runs, including data changes; predictable | Easy to see the whole schema; no scripts to write for simple changes |
| Risks | Many scripts over time | Generated changes can be surprising, such as a rename becoming drop-and-create |
| Tools | Liquibase, Flyway | SQL 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
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
vbuckchanges, or changes for thetestcontext only, such as test data. - Preconditions
- Checks that must pass before a changeset runs, such as "this table doesn't exist yet".
| Command | What it does |
|---|---|
liquibase status | Lists changesets not yet applied to this database |
liquibase update-sql | Shows the exact SQL that update would run, without running it: Liquibase's dry run |
liquibase update | Applies the pending changesets and records them |
liquibase tag v1.4.0 | Marks the current state, as a point to roll back to |
liquibase rollback v1.3.0 | Rolls back every changeset applied after a tag |
liquibase update-testing-rollback | Applies 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:
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.