Use it to change schemas safely across environments.
Schema Migrations
Schema migrations apply versioned, repeatable changes to database structure (Flyway, Liquibase, Alembic, framework migrations).
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.
migration script → applied in order → recorded in history table
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
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.
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.
flyway info— first check for this topicpsql -c "SELECT now(), version();"— PostgreSQL reachable and versionmysql -e "SELECT NOW(), VERSION();"— MariaDB/MySQL reachable and versionsqlcmd -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 CONCURRENTLYin 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)
| Change | Cost |
|---|---|
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 NULL | Full scan under ACCESS EXCLUSIVE; cheap if a validated CHECK (col IS NOT NULL) exists |
CREATE INDEX | Blocks writes — use CREATE INDEX CONCURRENTLY |
ADD FOREIGN KEY | Use 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.
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
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.