| name | database-migrations |
| description | Database migration patterns for PostgreSQL + Alembic + reconcile |
Database Migration Strategy
Two-Layer System
- Alembic — Version-controlled, reversible migrations for production
- Reconcile — Auto-runs on startup, safe-only (ADD/DROP IF EXISTS)
When to Use Alembic
- Breaking changes (DROP column, rename, type change)
- Data migrations (transform existing data)
- Production deployments requiring rollback
When to Use Reconcile
- Additive changes (new table, new column)
- New indexes
- New CHECK constraints or foreign keys
Alembic Commands
.venv\Scripts\python.exe -m alembic upgrade head
.venv\Scripts\python.exe -m alembic downgrade -1
.venv\Scripts\python.exe -m alembic revision -m "desc"
.venv\Scripts\python.exe -m alembic current
.venv\Scripts\python.exe -m alembic history
Safety Rules
- Always create BOTH upgrade() and downgrade()
- Test against dev DB before production
- Never DROP data without backup
- Use IF EXISTS / IF NOT EXISTS for safety
- Notify user before destructive migrations
PostgreSQL-Specific
- Use
CREATE INDEX CONCURRENTLY for large tables (no lock)
- Use
SKIP LOCKED for batch updates on live data
- Expand-contract pattern for column type changes
- Always use
TIMESTAMPTZ not TIMESTAMP
- Use
NUMERIC not REAL for financial data