| name | migration-patterns |
| description | Database migration creation with mandatory RLS policies and ARCHitect approval workflow. Use when creating migrations, adding tables, or planning data migrations. Migrations are raw SQL files in `src/db/migrations/` with `XXX_description.sql` naming, registered in `index.ts`, auto-run on startup.
|
Migration Patterns Skill
TEMPLATE: This skill uses {{PLACEHOLDER}} tokens. Replace with your project values before use.
Purpose
Guide database migration creation with mandatory RLS policies, following security-first architecture and approval workflow.
Current Tech Stack
- Migrations: Raw SQL files in
src/db/migrations/
- Naming:
XXX_description.sql (e.g., 001_initial_schema.sql)
- Registration:
src/db/migrations/index.ts auto-discovers and runs .sql files
- Execution: Auto-run on startup in sorted order, tracked in
migrations table
- Database: PostgreSQL (prod) / SQLite (dev)
- No ORM migrations: Raw SQL only (no ORM migration tools)
When This Skill Applies
- Creating database migrations
- Adding new tables
- Altering existing schemas
- Adding indexes or constraints
- Schema impact analysis
- Data migration planning
Stop-the-Line Conditions
FORBIDDEN Patterns
CREATE TABLE user_data (...);
CREATE TABLE planning_features (...);
CORRECT Patterns
CREATE TABLE IF NOT EXISTS audit_log (
id SERIAL PRIMARY KEY,
organization_id TEXT NOT NULL,
action TEXT NOT NULL,
entity_type TEXT NOT NULL,
entity_id TEXT,
payload JSONB,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_audit_log_org_id ON audit_log(organization_id);
CREATE INDEX IF NOT EXISTS idx_audit_log_created_at ON audit_log(created_at);
Migration Workflow (MANDATORY)
Step 1: Get ARCHitect Approval
Before ANY schema change:
1. Document proposed changes
2. Get ARCHitect approval (create issue or discussion)
3. Only proceed after explicit approval
Step 2: Create Migration File
touch src/db/migrations/008_add_audit_log.sql
ls src/db/migrations/
Step 3: Write Migration SQL
Write the migration with:
Step 4: Verify Locally
npm run dev
psql $DATABASE_URL -f src/db/migrations/008_add_audit_log.sql
psql $DATABASE_URL -c "\dt"
Step 5: Update Documentation
After successful migration:
Migration Checklist
Before PR:
Production Migration Requirements
For production migrations:
Authoritative References
- Migration Runner:
src/db/migrations/index.ts (MANDATORY -- understand before creating)
- Existing Migrations:
src/db/migrations/*.sql (follow existing patterns)
- Data Dictionary:
docs/database/DATA_DICTIONARY.md (update after changes)
- Security First:
docs/guides/SECURITY_FIRST_ARCHITECTURE.md