Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Schema Migrations

Direct answer

Schema migrations apply versioned, repeatable changes to database structure (Flyway, Liquibase, Alembic, framework migrations).

When to use this

Use it to change schemas safely across environments.

First 30 seconds

1. Migration tool status (e.g. flyway info).

2. Test on a copy with production volume.

3. Lock impact (ALTER on large tables).

4. Backward compatible with running app version?

5. Rollback plan.

Operational verification

migration script → applied in order → recorded in history table

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it to change schemas safely across environments.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 How to verify Verify boundary

Confirm the version table matches the expected revision in each environment. Check application errors after deploying a migration.

Step 2 Common failures Narrow the fault

Editing an already-applied migration makes environments differ. Dropping a column before the old code is gone breaks running instances. Long locks during peak hours cause outages.

Key points

Keep migrations in version control, test on production-size data, and make them backward-compatible for rolling deploys (expand → migrate → contract). Know which DDL locks tables.

Key checks
  • flyway info — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand

How it works

Migration tools apply scripts in order and record success. Some changes (adding a nullable column) are fast; others (changing a column type, adding a NOT NULL default on old engines) rewrite the whole table.

ReferenceImportant terms & boundaries3 sections · depth on demand

Migration practice

  • Versioned, reviewed scripts in Git (Flyway, Liquibase, Alembic, Rails/EF migrations).
  • Apply automatically in CI/CD to each environment in order.
  • Expand–contract for zero downtime: add new column → write both → backfill → switch reads → drop old.
  • Large tables: avoid locking DDL (CREATE INDEX CONCURRENTLY in PostgreSQL, online DDL / gh-ost / pt-online-schema-change in MySQL).
  • Always have a rollback plan or forward fix.

In depth

Expand → migrate → contract

A schema change that must not break running code is split into releases:

1. Expand: add the new column/table (nullable or with a default), keep the old one.

2. Deploy code that writes both and reads the new one with fallback.

3. Backfill existing rows in batches.

4. Deploy code that uses only the new column.

5. Contract: drop the old column in a later release.

Renaming a column in one step breaks every running instance of the old code during a rolling deploy.

Lock impact of common changes (PostgreSQL)

ChangeCost
ADD COLUMN nullable, or with a constant default (PG 11+)Metadata only, brief exclusive lock
ADD COLUMN … DEFAULT now() (volatile)Rewrites the table
ALTER COLUMN TYPE (most changes)Rewrites the table and indexes, ACCESS EXCLUSIVE
SET NOT NULLFull scan under ACCESS EXCLUSIVE; cheap if a validated CHECK (col IS NOT NULL) exists
CREATE INDEXBlocks writes — use CREATE INDEX CONCURRENTLY
ADD FOREIGN KEYUse NOT VALID + VALIDATE CONSTRAINT

The lock queue problem

Even a "fast" ALTER TABLE needs a brief exclusive lock. If a long query holds a lock on the table, the ALTER waits — and every new query queues behind the ALTER. Always set a lock timeout and retry:

SET lock_timeout = '3s';
ALTER TABLE orders ADD COLUMN channel text;

MySQL 8 supports ALGORITHM=INSTANT for adding columns; specify ALGORITHM=INPLACE, LOCK=NONE explicitly so the statement fails instead of silently copying the table when online DDL is not possible.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Migrations without rollback or tested on production-size data.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
schema migrationsdatabase schema migrationexpand contract migration
Confirmation

Confirm action

Continue?