@baselane/packs 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/aliases.js +1 -0
  2. package/package.json +2 -2
  3. package/packs/database-review/pack.json +75 -9
  4. package/packs/database-review/rendered/.claude/agents/database-reviewer.md +75 -2
  5. package/packs/database-review/rendered/.claude/commands/migration-review.md +6 -0
  6. package/packs/database-review/rendered/.claude/settings.json +11 -0
  7. package/packs/database-review/rendered/.claude/skills/database-migrations/SKILL.md +431 -0
  8. package/packs/database-review/rendered/.claude/skills/mysql-patterns/SKILL.md +414 -0
  9. package/packs/database-review/rendered/.claude/skills/postgres-patterns/SKILL.md +149 -0
  10. package/packs/database-review/rendered/.github/copilot-instructions.md +1096 -14
  11. package/packs/database-review/rendered/AGENTS.md +45 -16
  12. package/packs/database-review/rendered/CLAUDE.md +4 -0
  13. package/packs/database-review/rendered/GEMINI.md +987 -0
  14. package/packs/go-rules/pack.json +65 -9
  15. package/packs/go-rules/rendered/.claude/agents/go-build-resolver.md +90 -0
  16. package/packs/go-rules/rendered/.claude/agents/go-reviewer.md +72 -2
  17. package/packs/go-rules/rendered/.claude/commands/go-build.md +6 -0
  18. package/packs/go-rules/rendered/.claude/commands/go-review.md +1 -1
  19. package/packs/go-rules/rendered/.claude/commands/go-test.md +6 -0
  20. package/packs/go-rules/rendered/.claude/settings.json +9 -0
  21. package/packs/go-rules/rendered/.claude/skills/golang-patterns/SKILL.md +673 -0
  22. package/packs/go-rules/rendered/.claude/skills/golang-testing/SKILL.md +719 -0
  23. package/packs/go-rules/rendered/.github/copilot-instructions.md +1783 -7
  24. package/packs/go-rules/rendered/AGENTS.md +244 -8
  25. package/packs/go-rules/rendered/CLAUDE.md +5 -0
  26. package/packs/go-rules/rendered/GEMINI.md +1394 -0
  27. package/packs/python-rules/pack.json +87 -10
  28. package/packs/python-rules/rendered/.claude/agents/django-reviewer.md +159 -0
  29. package/packs/python-rules/rendered/.claude/agents/fastapi-reviewer.md +70 -0
  30. package/packs/python-rules/rendered/.claude/agents/python-reviewer.md +92 -2
  31. package/packs/python-rules/rendered/.claude/commands/django-review.md +6 -0
  32. package/packs/python-rules/rendered/.claude/commands/fastapi-review.md +6 -0
  33. package/packs/python-rules/rendered/.claude/commands/python-review.md +1 -1
  34. package/packs/python-rules/rendered/.claude/settings.json +1 -1
  35. package/packs/python-rules/rendered/.claude/skills/django-security/SKILL.md +642 -0
  36. package/packs/python-rules/rendered/.claude/skills/django-tdd/SKILL.md +728 -0
  37. package/packs/python-rules/rendered/.claude/skills/fastapi-patterns/SKILL.md +512 -0
  38. package/packs/python-rules/rendered/.claude/skills/python-patterns/SKILL.md +749 -0
  39. package/packs/python-rules/rendered/.claude/skills/python-testing/SKILL.md +815 -0
  40. package/packs/python-rules/rendered/.github/copilot-instructions.md +3815 -12
  41. package/packs/python-rules/rendered/AGENTS.md +73 -13
  42. package/packs/python-rules/rendered/CLAUDE.md +9 -0
  43. package/packs/python-rules/rendered/GEMINI.md +3448 -0
  44. package/packs/security-review/pack.json +51 -7
  45. package/packs/security-review/rendered/.claude/agents/security-reviewer.md +29 -2
  46. package/packs/security-review/rendered/.claude/settings.json +11 -0
  47. package/packs/security-review/rendered/.claude/skills/security-review/SKILL.md +302 -0
  48. package/packs/security-review/rendered/.claude/skills/security-review/cloud-infrastructure-security.md +171 -0
  49. package/packs/security-review/rendered/.claude/skills/security-scan-config/SKILL.md +56 -0
  50. package/packs/security-review/rendered/.github/copilot-instructions.md +658 -15
  51. package/packs/security-review/rendered/AGENTS.md +283 -17
  52. package/packs/security-review/rendered/CLAUDE.md +2 -0
  53. package/packs/security-review/rendered/GEMINI.md +352 -0
  54. package/packs/software-engineer-harness/pack.json +249 -13
  55. package/packs/software-engineer-harness/rendered/.claude/agents/build-error-resolver.md +15 -0
  56. package/packs/software-engineer-harness/rendered/.claude/agents/code-explorer.md +67 -0
  57. package/packs/software-engineer-harness/rendered/.claude/agents/performance-optimizer.md +444 -0
  58. package/packs/software-engineer-harness/rendered/.claude/agents/pr-test-analyzer.md +43 -0
  59. package/packs/software-engineer-harness/rendered/.claude/agents/refactor-cleaner.md +12 -0
  60. package/packs/software-engineer-harness/rendered/.claude/agents/silent-failure-hunter.md +16 -0
  61. package/packs/software-engineer-harness/rendered/.claude/agents/tdd-guide.md +16 -0
  62. package/packs/software-engineer-harness/rendered/.claude/commands/feature-dev.md +6 -0
  63. package/packs/software-engineer-harness/rendered/.claude/commands/fix-build.md +6 -0
  64. package/packs/software-engineer-harness/rendered/.claude/commands/hunt-silent-failures.md +6 -0
  65. package/packs/software-engineer-harness/rendered/.claude/commands/tdd.md +6 -0
  66. package/packs/software-engineer-harness/rendered/.claude/settings.json +29 -0
  67. package/packs/software-engineer-harness/rendered/.claude/skills/api-design/SKILL.md +522 -0
  68. package/packs/software-engineer-harness/rendered/.claude/skills/architecture-decision-records/SKILL.md +178 -0
  69. package/packs/software-engineer-harness/rendered/.claude/skills/backend-patterns/SKILL.md +560 -0
  70. package/packs/software-engineer-harness/rendered/.claude/skills/codebase-onboarding/SKILL.md +232 -0
  71. package/packs/software-engineer-harness/rendered/.claude/skills/deployment-patterns/SKILL.md +426 -0
  72. package/packs/software-engineer-harness/rendered/.claude/skills/docker-patterns/SKILL.md +363 -0
  73. package/packs/software-engineer-harness/rendered/.claude/skills/e2e-testing/SKILL.md +325 -0
  74. package/packs/software-engineer-harness/rendered/.claude/skills/tdd-red-green-refactor/SKILL.md +20 -0
  75. package/packs/software-engineer-harness/rendered/.claude/skills/tdd-workflow/SKILL.md +580 -0
  76. package/packs/software-engineer-harness/rendered/.github/copilot-instructions.md +3948 -2
  77. package/packs/software-engineer-harness/rendered/AGENTS.md +194 -4
  78. package/packs/software-engineer-harness/rendered/CLAUDE.md +20 -0
  79. package/packs/software-engineer-harness/rendered/GEMINI.md +3208 -0
  80. package/packs/typescript-rules/pack.json +68 -5
  81. package/packs/typescript-rules/rendered/.claude/agents/react-reviewer.md +165 -0
  82. package/packs/typescript-rules/rendered/.claude/agents/type-design-analyzer.md +48 -0
  83. package/packs/typescript-rules/rendered/.claude/commands/react-review.md +6 -0
  84. package/packs/typescript-rules/rendered/.claude/settings.json +9 -0
  85. package/packs/typescript-rules/rendered/.claude/skills/react-patterns/SKILL.md +340 -0
  86. package/packs/typescript-rules/rendered/.claude/skills/react-performance/SKILL.md +573 -0
  87. package/packs/typescript-rules/rendered/.claude/skills/react-testing/SKILL.md +422 -0
  88. package/packs/typescript-rules/rendered/.github/copilot-instructions.md +2459 -53
  89. package/packs/typescript-rules/rendered/AGENTS.md +893 -17
  90. package/packs/typescript-rules/rendered/CLAUDE.md +6 -0
  91. package/packs/typescript-rules/rendered/GEMINI.md +1335 -0
@@ -2,47 +2,1129 @@
2
2
 
3
3
  ## Database review discipline
4
4
 
5
- `sqlfluff` catches SQL style and obvious mistakes — this pack is the judgment it can't encode, applied in review of any query, schema, or migration before it reaches production.
5
+ `sqlfluff` catches SQL style and obvious mistakes — this pack is the judgment it can't encode, applied in review of any query, schema, or migration before it reaches production. It draws on PostgreSQL, MySQL/MariaDB, and cross-ORM migration patterns (Prisma, Drizzle, Kysely, Django, golang-migrate) — see the `postgres-patterns`, `mysql-patterns`, and `database-migrations` skills bundled with this pack for full detail and copy-paste examples.
6
6
 
7
7
  ### Query performance
8
8
 
9
- - Every column in a `WHERE` or `JOIN` should be indexed — confirm with `EXPLAIN ANALYZE`, don't assume.
9
+ - Every column in a `WHERE` or `JOIN` should be indexed — confirm with `EXPLAIN`/`EXPLAIN ANALYZE`, don't assume.
10
10
  - Watch for N+1: a query inside a loop over rows from a previous query. Batch or join it.
11
- - Composite indexes put equality columns before range columns.
11
+ - Composite indexes put equality columns before range columns: `(status, created_at)` serves `WHERE status = 'pending' AND created_at > ...`.
12
+ - Use partial indexes for soft-delete filters (`WHERE deleted_at IS NULL`) and covering indexes (`INCLUDE (col)` / trailing columns) to avoid table lookups.
13
+ - Prefer cursor/keyset pagination (`WHERE id > $last` or `WHERE (created_at, id) < (?, ?)`) over `OFFSET` on large or frequently-written tables — OFFSET is O(n) and gets slower as the page number grows.
14
+ - For queue-style claims, `FOR UPDATE SKIP LOCKED` gives real throughput — but only for workloads where skipping a locked row is acceptable, never for general accounting/integrity-sensitive reads.
12
15
 
13
16
  ### Schema design
14
17
 
15
- - Use precise types: timestamp-with-timezone for times, decimal/numeric for money, `text` over an arbitrary varchar cap.
16
- - Every foreign key gets an index — no exceptions.
18
+ - Use precise types: `bigint` for IDs (not `int`, which overflows on tables that grow past 2B rows), `timestamptz`/`DATETIME`-with-UTC-discipline for times, `numeric`/`DECIMAL` for money (never `float`/`double`), `text` over an arbitrary varchar cap, `boolean` for flags.
19
+ - Every foreign key gets an index — no exceptions. An unindexed FK makes joins slow and deletes/updates on the parent lock-heavy.
17
20
  - Put constraints (`NOT NULL`, `CHECK`, `ON DELETE`) in the schema, not only in application code.
21
+ - Avoid random UUID primary keys on hot tables (poor locality); prefer UUIDv7/ordered IDs or surrogate integer keys with a separate lookup UUID if external-facing IDs are needed.
22
+ - Use `lowercase_snake_case` identifiers consistently — quoted mixed-case identifiers are a recurring source of driver/tooling bugs.
18
23
 
19
24
  ### Migration safety
20
25
 
21
- - Adding a `NOT NULL` column to a large table needs a backfill, not just a default check in-flight writes during the migration.
22
- - Don't hold locks across an external call or a slow backfill; batch the backfill.
23
- - Every migration has a rollback path.
26
+ - Every change is a migration never alter production databases manually, and never edit a migration that has already run in production (create a new forward migration instead).
27
+ - Adding a `NOT NULL` column to a large table needs a nullable-add + backfill + constrain sequence, not a single `ADD COLUMN ... NOT NULL` — that requires a full table rewrite and lock. A column with a constant default is instant in modern Postgres (11+); a computed/volatile default is not.
28
+ - Build indexes with `CREATE INDEX CONCURRENTLY` (Postgres) or equivalent non-blocking DDL on existing large tables — a plain `CREATE INDEX` blocks writes for the duration of the build. Note `CONCURRENTLY` cannot run inside a transaction block; most migration tools need special handling for it.
29
+ - Never mix schema (DDL) and data (DML) changes in one migration — a large backfill inside a schema migration turns a fast operation into a long lock.
30
+ - Batch large data migrations (e.g. `LIMIT batch_size ... FOR UPDATE SKIP LOCKED`, commit per batch) instead of updating every row in one transaction.
31
+ - Renames and drops go through expand-contract: add the new column, backfill, dual-write/dual-read from the application, then drop the old column in a later, separate migration — never rename directly in production.
32
+ - Every migration has a rollback path (an explicit DOWN, or an documented forward-only remediation migration if truly irreversible). Test against production-sized data — a migration that's instant on 100 rows can lock for minutes on 10M.
24
33
 
25
34
  ### Data-layer security
26
35
 
27
36
  - Queries are parameterized, never string-concatenated with user input.
28
- - Multi-tenant tables enforce row-level access at the database, not only in application code.
29
- - Application database users get least-privilege grants, never blanket admin.
37
+ - Multi-tenant tables enforce row-level access at the database (Postgres RLS with policies wrapped as `(SELECT auth.uid())` — an unwrapped per-row function call in a policy is a hidden N+1), not only in application code.
38
+ - Application database users get least-privilege grants (`SELECT, INSERT, UPDATE, DELETE` on the app schema), never `GRANT ALL`/admin. Separate migration/admin users from runtime application users. Require TLS for connections that cross hosts/networks. Revoke default `public` schema privileges.
39
+ - Drop anonymous/empty-username database accounts (a default MySQL install footgun) and audit public network exposure/bind addresses before tuning performance.
40
+
41
+ ### Concurrency and connections
42
+
43
+ - Keep transactions short — never hold a lock across an external API call or a slow backfill.
44
+ - Lock rows in a consistent order across all code paths touching the same tables (e.g. `ORDER BY id FOR UPDATE`) to prevent deadlocks; on deadlock, roll back and retry the whole transaction with a bounded budget.
45
+ - Size connection pools below the server's connection/idle timeout (e.g. `pool_recycle` under MySQL's `wait_timeout`), enable pre-ping/keepalive, and set statement/idle-in-transaction timeouts on the server side.
46
+ - Read replicas lag — never route read-your-own-write paths, checkout flows, permission checks, or idempotency-key reads to a replica immediately after a write.
30
47
 
31
48
  ### Flag in review
32
49
 
33
50
  - `SELECT *` in application code shipped to production.
34
- - Offset pagination on large, frequently-written tables — prefer cursor pagination.
35
- - `INSERT`s in a loop instead of a batch/multi-row insert.
51
+ - Offset pagination on large, frequently-written tables — prefer cursor/keyset pagination.
52
+ - `INSERT`s in a loop instead of a batch/multi-row insert or `COPY`.
36
53
  - Inconsistent lock ordering across transactions touching the same tables (deadlock risk).
54
+ - `ADD COLUMN ... NOT NULL` with no default on an existing large table, or a plain (non-concurrent) `CREATE INDEX` on one.
55
+ - `GRANT ALL` / admin privileges granted to an application's runtime database user.
56
+ - A migration that mixes schema and data changes, or has no rollback/remediation path.
37
57
 
38
58
  ## Workflow pack: Database review
39
59
 
40
- Query, schema, and migration review judgment a linter can't encode — index every foreign key, kill N+1s, keep queries parameterized, ship migrations that don't lock production with a reviewer and a sqlfluff guard.
60
+ Query, schema, and migration review judgment a linter can't encode — full PostgreSQL/MySQL/migration pattern libraries plus a reviewer, migration-review command, and safety hooks adapted from ECC's database-reviewer, postgres-patterns, mysql-patterns, and database-migrations.
61
+
62
+ When acting as the database-reviewer role: You are an expert database reviewer covering PostgreSQL and MySQL/MariaDB, focused on query optimization, schema design, security, and migration safety. Your mission is to ensure database code follows best practices, prevents performance issues, and maintains data integrity.
63
+
64
+ Before reviewing, consult ARCHITECTURE.md's structure and conventions (the baselane-managed system map) so your review matches this repo's real layout and hidden rules; if ARCHITECTURE.md does not exist yet, suggest running /map regenerate (or baselane map . from the repo root) to generate it, and proceed with the review while noting that gap.
65
+
66
+ ## Core responsibilities
67
+
68
+ 1. Query performance — optimize queries, add proper indexes, prevent table/sequential scans.
69
+ 2. Schema design — precise data types, constraints, and indexing.
70
+ 3. Security & access control — Row Level Security (Postgres) or equivalent tenancy isolation, least-privilege database users.
71
+ 4. Connection management — pooling, timeouts, limits appropriate to the server's own timeout settings.
72
+ 5. Concurrency — prevent deadlocks, keep transactions short, use consistent lock ordering.
73
+ 6. Migration safety — no full-table locks on large tables, backfills batched and separated from schema changes, every migration has a rollback path.
74
+
75
+ ## Diagnostic commands
76
+
77
+ ```bash
78
+ # Postgres
79
+ psql \$DATABASE_URL
80
+ psql -c "SELECT query, mean_exec_time, calls FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 10;"
81
+ psql -c "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_stat_user_tables ORDER BY pg_total_relation_size(relid) DESC;"
82
+ psql -c "SELECT indexrelname, idx_scan, idx_tup_read FROM pg_stat_user_indexes ORDER BY idx_scan DESC;"
83
+
84
+ # MySQL/MariaDB
85
+ SHOW FULL PROCESSLIST;
86
+ SHOW ENGINE INNODB STATUS\G;
87
+ EXPLAIN <query>;
88
+ ```
89
+
90
+ ## Review workflow
91
+
92
+ 1. **Query performance (CRITICAL)** — Are WHERE/JOIN columns indexed? Run EXPLAIN/EXPLAIN ANALYZE on complex queries and check for sequential/full scans on large tables. Watch for N+1 patterns. Verify composite index column order (equality first, then range).
93
+ 2. **Schema design (HIGH)** — Proper types (bigint IDs, text strings, timestamptz/DATETIME-UTC timestamps, numeric/DECIMAL money, boolean flags). PK/FK constraints with ON DELETE, NOT NULL, CHECK. Consistent lowercase_snake_case identifiers.
94
+ 3. **Security (CRITICAL)** — RLS (or equivalent) enabled on multi-tenant tables, policy columns indexed, policy functions wrapped to avoid per-row re-evaluation. Least-privilege grants — no blanket admin to application users. Public schema/anonymous-user exposure locked down.
95
+ 4. **Migration safety (CRITICAL)** — No NOT NULL column added without a default on an existing large table. Indexes built concurrently/non-blocking on existing large tables. Schema and data changes in separate migrations. Backfills batched. Rollback path documented or migration explicitly marked irreversible with a remediation plan.
96
+
97
+ ## Key principles
98
+
99
+ - Index foreign keys — always, no exceptions.
100
+ - Use partial indexes for soft deletes (`WHERE deleted_at IS NULL`) and covering indexes to avoid table lookups.
101
+ - `SKIP LOCKED` for queue-style claims only — not for integrity-sensitive reads.
102
+ - Cursor/keyset pagination instead of OFFSET on large tables.
103
+ - Batch inserts (multi-row INSERT or COPY), never individual inserts in a loop.
104
+ - Short transactions — never hold locks during an external API call.
105
+ - Consistent lock ordering (`ORDER BY id FOR UPDATE`) to prevent deadlocks.
106
+
107
+ ## Anti-patterns to flag
108
+
109
+ - `SELECT *` in production code.
110
+ - `int`/`INT` for IDs on tables that can grow large (use bigint), `varchar(255)` without reason (use text), `timestamp` without timezone.
111
+ - Random UUIDs as primary keys on hot tables.
112
+ - OFFSET pagination on large tables.
113
+ - Unparameterized queries (SQL injection risk).
114
+ - `GRANT ALL` / admin privileges to application runtime users.
115
+ - Access-control policies calling functions per-row instead of once per query.
116
+ - `ADD COLUMN ... NOT NULL` with no default, or a blocking (non-concurrent) index build, on an existing large table.
117
+ - Migrations mixing DDL and DML, or missing a rollback/remediation path.
118
+
119
+ ## Review checklist
120
+
121
+ - [ ] All WHERE/JOIN columns indexed
122
+ - [ ] Composite indexes in correct column order
123
+ - [ ] Proper data types (bigint, text, timestamptz/DATETIME-UTC, numeric/DECIMAL)
124
+ - [ ] Tenant isolation (RLS or equivalent) enabled where needed, policy columns indexed
125
+ - [ ] Foreign keys have indexes
126
+ - [ ] No N+1 query patterns
127
+ - [ ] EXPLAIN/EXPLAIN ANALYZE run on complex queries
128
+ - [ ] Transactions kept short, lock ordering consistent
129
+ - [ ] Migrations: reversible or explicitly irreversible with a plan, no blocking DDL on large tables, backfills batched and separated from schema changes
41
130
 
42
- When acting as the database-reviewer role: You are a database reviewer. Before reviewing, consult ARCHITECTURE.md's structure and conventions (the baselane-managed system map) so your review matches this repo's real layout and hidden rules; if ARCHITECTURE.md does not exist yet, suggest running /map regenerate (or baselane map . from the repo root) to generate it, and proceed with the review while noting that gap. Then read the changed SQL, schema, or migration files and check them against the database-review discipline in context: indexing on WHERE/JOIN/foreign-key columns, N+1 patterns, precise types and constraints, migration backfill/rollback safety, parameterized queries, and least-privilege access. Cite exact file:line for each finding, name the concrete failure mode (e.g. "this JOIN on orders.user_id has no index — table scan at N rows"), and give severity. If the change is sound, say so.
131
+ Cite exact file:line for each finding, name the concrete failure mode (e.g. "this JOIN on orders.user_id has no index — table scan at N rows"), and give severity. If the change is sound, say so.
132
+
133
+ For detailed index cheat sheets, MySQL/MariaDB-specific syntax, connection-pool configs, and per-ORM migration workflows (Prisma, Drizzle, Kysely, Django, golang-migrate), see the `postgres-patterns`, `mysql-patterns`, and `database-migrations` skills bundled with this pack.
134
+
135
+ *Patterns adapted from ECC's database-reviewer agent and postgres-patterns/mysql-patterns/database-migrations skills, which credit Supabase's postgres-best-practices (MIT license).*
43
136
 
44
137
  Workflow steps:
45
138
 
46
139
  - db-review: Invoke the database-reviewer subagent over the current diff (or $ARGUMENTS if given). Report findings grouped by area (indexing, schema, migration safety, security) with file:line and fix for each. End with a clear verdict.
140
+ - migration-review: Invoke the database-reviewer subagent over the migration at $ARGUMENTS (or the most recently added/changed migration file if not given). Check specifically: (1) does any DDL statement hold a full-table lock on a table that can be large in production — e.g. a plain CREATE INDEX instead of CONCURRENTLY, or ADD COLUMN ... NOT NULL with no default; (2) is there a rollback path (a DOWN migration, or an explicit note that it's forward-only with a remediation plan); (3) is any data backfill batched (bounded LIMIT + commit per batch) rather than a single unbounded UPDATE, and kept in a separate migration from the schema change. Report each of the three areas with file:line, verdict (pass/fail), and the concrete fix if it fails.
47
141
 
48
142
  - After an edit, lint SQL and migrations with sqlfluff when it is installed.
143
+ - Reminder from database-review: schema/migration file edits need a documented rollback path (or explicit forward-only + remediation plan) and must not hold long locks on production-sized tables — batch backfills, build indexes concurrently, and avoid NOT NULL columns without a default on existing large tables.
144
+
145
+ ## Skills
146
+
147
+ ### postgres-patterns
148
+
149
+ PostgreSQL indexing, schema, RLS, pagination, and configuration patterns. Use when writing SQL queries or migrations, designing schemas, troubleshooting slow queries, implementing Row Level Security, or setting up connection pooling.
150
+
151
+ # PostgreSQL Patterns
152
+
153
+ Quick reference for PostgreSQL best practices. For detailed guidance, use the `database-reviewer` agent.
154
+
155
+ ## When to Activate
156
+
157
+ - Writing SQL queries or migrations
158
+ - Designing database schemas
159
+ - Troubleshooting slow queries
160
+ - Implementing Row Level Security
161
+ - Setting up connection pooling
162
+
163
+ ## Quick Reference
164
+
165
+ ### Index Cheat Sheet
166
+
167
+ | Query Pattern | Index Type | Example |
168
+ |--------------|------------|---------|
169
+ | `WHERE col = value` | B-tree (default) | `CREATE INDEX idx ON t (col)` |
170
+ | `WHERE col > value` | B-tree | `CREATE INDEX idx ON t (col)` |
171
+ | `WHERE a = x AND b > y` | Composite | `CREATE INDEX idx ON t (a, b)` |
172
+ | `WHERE jsonb @> '{}'` | GIN | `CREATE INDEX idx ON t USING gin (col)` |
173
+ | `WHERE tsv @@ query` | GIN | `CREATE INDEX idx ON t USING gin (col)` |
174
+ | Time-series ranges | BRIN | `CREATE INDEX idx ON t USING brin (col)` |
175
+
176
+ ### Data Type Quick Reference
177
+
178
+ | Use Case | Correct Type | Avoid |
179
+ |----------|-------------|-------|
180
+ | IDs | `bigint` | `int`, random UUID |
181
+ | Strings | `text` | `varchar(255)` |
182
+ | Timestamps | `timestamptz` | `timestamp` |
183
+ | Money | `numeric(10,2)` | `float` |
184
+ | Flags | `boolean` | `varchar`, `int` |
185
+
186
+ ### Common Patterns
187
+
188
+ **Composite Index Order:**
189
+ ```sql
190
+ -- Equality columns first, then range columns
191
+ CREATE INDEX idx ON orders (status, created_at);
192
+ -- Works for: WHERE status = 'pending' AND created_at > '2024-01-01'
193
+ ```
194
+
195
+ **Covering Index:**
196
+ ```sql
197
+ CREATE INDEX idx ON users (email) INCLUDE (name, created_at);
198
+ -- Avoids table lookup for SELECT email, name, created_at
199
+ ```
200
+
201
+ **Partial Index:**
202
+ ```sql
203
+ CREATE INDEX idx ON users (email) WHERE deleted_at IS NULL;
204
+ -- Smaller index, only includes active users
205
+ ```
206
+
207
+ **RLS Policy (Optimized):**
208
+ ```sql
209
+ CREATE POLICY policy ON orders
210
+ USING ((SELECT auth.uid()) = user_id); -- Wrap in SELECT!
211
+ ```
212
+
213
+ **UPSERT:**
214
+ ```sql
215
+ INSERT INTO settings (user_id, key, value)
216
+ VALUES (123, 'theme', 'dark')
217
+ ON CONFLICT (user_id, key)
218
+ DO UPDATE SET value = EXCLUDED.value;
219
+ ```
220
+
221
+ **Cursor Pagination:**
222
+ ```sql
223
+ SELECT * FROM products WHERE id > $last_id ORDER BY id LIMIT 20;
224
+ -- O(1) vs OFFSET which is O(n)
225
+ ```
226
+
227
+ **Queue Processing:**
228
+ ```sql
229
+ UPDATE jobs SET status = 'processing'
230
+ WHERE id = (
231
+ SELECT id FROM jobs WHERE status = 'pending'
232
+ ORDER BY created_at LIMIT 1
233
+ FOR UPDATE SKIP LOCKED
234
+ ) RETURNING *;
235
+ ```
236
+
237
+ ### Anti-Pattern Detection
238
+
239
+ ```sql
240
+ -- Find unindexed foreign keys
241
+ SELECT conrelid::regclass, a.attname
242
+ FROM pg_constraint c
243
+ JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY(c.conkey)
244
+ WHERE c.contype = 'f'
245
+ AND NOT EXISTS (
246
+ SELECT 1 FROM pg_index i
247
+ WHERE i.indrelid = c.conrelid AND a.attnum = ANY(i.indkey)
248
+ );
249
+
250
+ -- Find slow queries
251
+ SELECT query, mean_exec_time, calls
252
+ FROM pg_stat_statements
253
+ WHERE mean_exec_time > 100
254
+ ORDER BY mean_exec_time DESC;
255
+
256
+ -- Check table bloat
257
+ SELECT relname, n_dead_tup, last_vacuum
258
+ FROM pg_stat_user_tables
259
+ WHERE n_dead_tup > 1000
260
+ ORDER BY n_dead_tup DESC;
261
+ ```
262
+
263
+ ### Configuration Template
264
+
265
+ ```sql
266
+ -- Connection limits (adjust for RAM)
267
+ ALTER SYSTEM SET max_connections = 100;
268
+ ALTER SYSTEM SET work_mem = '8MB';
269
+
270
+ -- Timeouts
271
+ ALTER SYSTEM SET idle_in_transaction_session_timeout = '30s';
272
+ ALTER SYSTEM SET statement_timeout = '30s';
273
+
274
+ -- Monitoring
275
+ CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
276
+
277
+ -- Security defaults
278
+ REVOKE ALL ON SCHEMA public FROM public;
279
+
280
+ SELECT pg_reload_conf();
281
+ ```
282
+
283
+ ## Related
284
+
285
+ - Agent: `database-reviewer` - Full database review workflow
286
+ - Skill: `clickhouse-io` - ClickHouse analytics patterns
287
+ - Skill: `backend-patterns` - API and backend patterns
288
+
289
+ ---
290
+
291
+ *Based on Supabase Agent Skills (credit: Supabase team) (MIT License)*
292
+
293
+ ### mysql-patterns
294
+
295
+ MySQL and MariaDB schema, query, indexing, transaction, replication, and connection-pool patterns for production backends. Use when designing MySQL/MariaDB tables and indexes, reviewing migrations before they run on large tables, debugging slow queries/lock waits/deadlocks/connection exhaustion, or adding keyset pagination, upserts, full-text search, JSON columns, or queues.
296
+
297
+ # MySQL Patterns
298
+
299
+ Use this skill when working on MySQL or MariaDB schema design, migrations,
300
+ slow-query investigation, queue-style transactions, connection pools, or
301
+ production database configuration. Prefer exact version checks before applying a
302
+ feature-specific pattern because MySQL and MariaDB have diverged in several SQL
303
+ details.
304
+
305
+ ## Activation
306
+
307
+ - Designing MySQL or MariaDB tables, indexes, and constraints
308
+ - Reviewing migrations before they run on large production tables
309
+ - Debugging slow queries, lock waits, deadlocks, or connection exhaustion
310
+ - Adding keyset pagination, upserts, full-text search, JSON columns, or queues
311
+ - Configuring application connection pools, read replicas, TLS, or slow logs
312
+
313
+ ## Version Check
314
+
315
+ Start by identifying the engine and version:
316
+
317
+ ```sql
318
+ SELECT VERSION();
319
+ SHOW VARIABLES LIKE 'version_comment';
320
+ ```
321
+
322
+ Keep MySQL and MariaDB guidance separate when syntax differs:
323
+
324
+ - MySQL documents row aliases as the replacement for `VALUES(col)` in
325
+ `ON DUPLICATE KEY UPDATE`; `VALUES(col)` is deprecated there.
326
+ - MariaDB documents `VALUES(col)` as the supported way to reference inserted
327
+ values in `ON DUPLICATE KEY UPDATE`; use it for cross-engine compatibility.
328
+ - `SKIP LOCKED` is appropriate for queue-like work only. It skips locked rows
329
+ and can return an inconsistent view, so do not use it for general accounting
330
+ or integrity-sensitive reads.
331
+
332
+ ## Schema Defaults
333
+
334
+ ```sql
335
+ CREATE TABLE orders (
336
+ id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
337
+ account_id BIGINT UNSIGNED NOT NULL,
338
+ status VARCHAR(32) NOT NULL,
339
+ total DECIMAL(15, 2) NOT NULL,
340
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
341
+ updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
342
+ deleted_at DATETIME NULL,
343
+ PRIMARY KEY (id),
344
+ KEY idx_orders_account_status_created (account_id, status, created_at),
345
+ KEY idx_orders_active (account_id, deleted_at)
346
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
347
+ ```
348
+
349
+ Default choices:
350
+
351
+ | Use Case | Prefer | Avoid |
352
+ | --- | --- | --- |
353
+ | Surrogate primary keys | `BIGINT UNSIGNED AUTO_INCREMENT` | `INT` for tables that can grow beyond 2B rows |
354
+ | UUID lookup keys | `BINARY(16)` with conversion helpers | `VARCHAR(36)` primary keys on hot tables |
355
+ | Money and exact quantities | `DECIMAL(p, s)` | `FLOAT` or `DOUBLE` |
356
+ | User-facing text | `utf8mb4` tables and indexes | MySQL `utf8` / `utf8mb3` defaults |
357
+ | Application timestamps | `DATETIME` with UTC managed by the app | Assuming `DATETIME` stores time zone metadata |
358
+ | Soft deletes | `deleted_at DATETIME NULL` plus scoped indexes | Filtering soft-deleted rows without an index |
359
+ | Extensible status values | lookup table or constrained `VARCHAR` | `ENUM` when values change often |
360
+
361
+ ## Indexing
362
+
363
+ Composite index order usually follows equality predicates first, then range or
364
+ sort columns:
365
+
366
+ ```sql
367
+ CREATE INDEX idx_orders_account_status_created
368
+ ON orders (account_id, status, created_at);
369
+
370
+ SELECT id, total
371
+ FROM orders
372
+ WHERE account_id = ?
373
+ AND status = 'pending'
374
+ AND created_at >= ?
375
+ ORDER BY created_at DESC
376
+ LIMIT 50;
377
+ ```
378
+
379
+ Use `EXPLAIN` before adding or changing an index:
380
+
381
+ ```sql
382
+ EXPLAIN
383
+ SELECT id, total
384
+ FROM orders
385
+ WHERE account_id = 123 AND status = 'pending'
386
+ ORDER BY created_at DESC
387
+ LIMIT 50;
388
+ ```
389
+
390
+ Signals to investigate:
391
+
392
+ | Field | Risk Signal |
393
+ | --- | --- |
394
+ | `type` | `ALL` on a large table |
395
+ | `key` | `NULL` when a selective predicate exists |
396
+ | `rows` | Very high row estimate for an interactive path |
397
+ | `Extra` | `Using temporary`, `Using filesort`, or broad `Using where` |
398
+
399
+ Avoid adding indexes blindly. Each index increases write cost, migration time,
400
+ backup size, and buffer-pool pressure.
401
+
402
+ ## Query Patterns
403
+
404
+ ### Upsert
405
+
406
+ Cross-engine-compatible form:
407
+
408
+ ```sql
409
+ INSERT INTO user_settings (user_id, setting_key, setting_value)
410
+ VALUES (?, ?, ?)
411
+ ON DUPLICATE KEY UPDATE
412
+ setting_value = VALUES(setting_value),
413
+ updated_at = CURRENT_TIMESTAMP;
414
+ ```
415
+
416
+ MySQL row-alias form:
417
+
418
+ ```sql
419
+ INSERT INTO user_settings (user_id, setting_key, setting_value)
420
+ VALUES (?, ?, ?) AS new
421
+ ON DUPLICATE KEY UPDATE
422
+ setting_value = new.setting_value,
423
+ updated_at = CURRENT_TIMESTAMP;
424
+ ```
425
+
426
+ Use the row-alias form only after confirming the target is MySQL. Use
427
+ `VALUES(col)` for MariaDB or mixed MySQL/MariaDB fleets.
428
+
429
+ ### Keyset Pagination
430
+
431
+ ```sql
432
+ SELECT id, name, created_at
433
+ FROM products
434
+ WHERE (created_at, id) < (?, ?)
435
+ ORDER BY created_at DESC, id DESC
436
+ LIMIT 50;
437
+ ```
438
+
439
+ Back it with an index that matches the cursor:
440
+
441
+ ```sql
442
+ CREATE INDEX idx_products_created_id ON products (created_at, id);
443
+ ```
444
+
445
+ Do not use deep `OFFSET` pagination on large tables; it makes the server scan
446
+ and discard rows before returning the page.
447
+
448
+ ### JSON Fields
449
+
450
+ Use JSON columns for extension data, not for fields that need heavy relational
451
+ filtering or constraints.
452
+
453
+ ```sql
454
+ CREATE TABLE events (
455
+ id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
456
+ payload JSON NOT NULL,
457
+ event_type VARCHAR(64)
458
+ GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(payload, '$.type'))) STORED,
459
+ KEY idx_events_type (event_type)
460
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
461
+ ```
462
+
463
+ For frequently queried JSON paths, expose a generated column and index that
464
+ column. Keep foreign keys, ownership, tenancy, and lifecycle fields relational.
465
+
466
+ ### Full-Text Search
467
+
468
+ ```sql
469
+ ALTER TABLE articles ADD FULLTEXT KEY ft_articles_title_body (title, body);
470
+
471
+ SELECT id, title, MATCH(title, body) AGAINST (? IN NATURAL LANGUAGE MODE) AS score
472
+ FROM articles
473
+ WHERE MATCH(title, body) AGAINST (? IN NATURAL LANGUAGE MODE)
474
+ ORDER BY score DESC
475
+ LIMIT 20;
476
+ ```
477
+
478
+ Use external search when you need typo tolerance, complex ranking, cross-table
479
+ facets, or language-specific analysis beyond built-in full-text behavior.
480
+
481
+ ## Transactions
482
+
483
+ Keep transactions short and lock rows in a consistent order:
484
+
485
+ ```sql
486
+ START TRANSACTION;
487
+
488
+ SELECT id, balance
489
+ FROM accounts
490
+ WHERE id IN (?, ?)
491
+ ORDER BY id
492
+ FOR UPDATE;
493
+
494
+ UPDATE accounts SET balance = balance - ? WHERE id = ?;
495
+ UPDATE accounts SET balance = balance + ? WHERE id = ?;
496
+
497
+ COMMIT;
498
+ ```
499
+
500
+ Deadlock and lock-wait checklist:
501
+
502
+ - Lock rows in a deterministic order across code paths.
503
+ - Do external API calls before opening the transaction, not inside it.
504
+ - Add indexes for predicates used in `UPDATE`, `DELETE`, and locking reads.
505
+ - On deadlock, roll back and retry the whole transaction with a bounded retry
506
+ budget.
507
+ - Capture `SHOW ENGINE INNODB STATUS\G` soon after a deadlock; it is overwritten
508
+ by later events.
509
+
510
+ Queue-style worker claim:
511
+
512
+ ```sql
513
+ START TRANSACTION;
514
+
515
+ SELECT id
516
+ FROM jobs
517
+ WHERE status = 'pending'
518
+ ORDER BY created_at
519
+ LIMIT 1
520
+ FOR UPDATE SKIP LOCKED;
521
+
522
+ UPDATE jobs
523
+ SET status = 'processing', started_at = CURRENT_TIMESTAMP
524
+ WHERE id = ?;
525
+
526
+ COMMIT;
527
+ ```
528
+
529
+ Use `SKIP LOCKED` only for queue-like workloads where skipping a locked row is
530
+ acceptable. It is not a replacement for normal transactional consistency.
531
+
532
+ ## Connection Pools
533
+
534
+ SQLAlchemy example:
535
+
536
+ ```python
537
+ from sqlalchemy import create_engine
538
+
539
+ engine = create_engine(
540
+ "mysql+mysqlconnector://app:secret@db.internal/app",
541
+ pool_size=10,
542
+ max_overflow=5,
543
+ pool_timeout=30,
544
+ pool_recycle=240,
545
+ pool_pre_ping=True,
546
+ connect_args={"connect_timeout": 5},
547
+ )
548
+ ```
549
+
550
+ Node.js `mysql2` example:
551
+
552
+ ```javascript
553
+ import mysql from 'mysql2/promise';
554
+
555
+ const pool = mysql.createPool({
556
+ host: process.env.DB_HOST,
557
+ user: process.env.DB_USER,
558
+ password: process.env.DB_PASSWORD,
559
+ database: process.env.DB_NAME,
560
+ waitForConnections: true,
561
+ connectionLimit: 10,
562
+ queueLimit: 0,
563
+ enableKeepAlive: true,
564
+ keepAliveInitialDelay: 30000,
565
+ });
566
+
567
+ const [rows] = await pool.execute(
568
+ 'SELECT id, total FROM orders WHERE account_id = ? LIMIT 50',
569
+ [accountId],
570
+ );
571
+ ```
572
+
573
+ Keep application pool recycling below the server `wait_timeout`. If the server
574
+ uses `wait_timeout = 300`, a `pool_recycle` around 240 seconds is coherent;
575
+ `pool_pre_ping` still helps recover from network and failover events.
576
+
577
+ ## Diagnostics
578
+
579
+ Useful first-pass commands:
580
+
581
+ ```sql
582
+ SHOW FULL PROCESSLIST;
583
+ SHOW ENGINE INNODB STATUS\G;
584
+ SHOW VARIABLES LIKE 'slow_query_log';
585
+ SHOW VARIABLES LIKE 'long_query_time';
586
+ ```
587
+
588
+ Enable the slow log in a controlled environment:
589
+
590
+ ```sql
591
+ SET GLOBAL slow_query_log = 'ON';
592
+ SET GLOBAL long_query_time = 1;
593
+ SET GLOBAL log_queries_not_using_indexes = 'ON';
594
+ ```
595
+
596
+ Use `EXPLAIN ANALYZE` only when it is safe to execute the query. It runs the
597
+ statement and can be expensive on production-sized data.
598
+
599
+ ## Replication
600
+
601
+ Read replicas can lag. Do not route read-your-own-write paths, checkout flows,
602
+ permission checks, or idempotency-key reads to a replica immediately after a
603
+ write.
604
+
605
+ ```sql
606
+ -- MySQL legacy terminology, still common in existing fleets
607
+ SHOW SLAVE STATUS\G;
608
+
609
+ -- Newer terminology where supported
610
+ SHOW REPLICA STATUS\G;
611
+ ```
612
+
613
+ Check the engine/version before standardizing on one command. Monitor replica
614
+ SQL thread health, IO thread health, and lag, not just whether the TCP
615
+ connection is alive.
616
+
617
+ ## Security
618
+
619
+ ```sql
620
+ CREATE USER 'app'@'%' IDENTIFIED BY 'use-a-secret-manager';
621
+ GRANT SELECT, INSERT, UPDATE, DELETE ON appdb.* TO 'app'@'%';
622
+
623
+ ALTER USER 'app'@'%' REQUIRE SSL;
624
+
625
+ SELECT user, host
626
+ FROM mysql.user
627
+ WHERE user = '';
628
+
629
+ DROP USER IF EXISTS ''@'localhost';
630
+ DROP USER IF EXISTS ''@'%';
631
+ ```
632
+
633
+ Security review points:
634
+
635
+ - Do not grant `ALL PRIVILEGES` or `*.*` to application users.
636
+ - Require TLS for application users when traffic crosses hosts or networks.
637
+ - Store credentials in the platform secret manager, not in examples, scripts, or
638
+ repository files.
639
+ - Separate migration/admin users from runtime application users.
640
+ - Audit public network exposure and bind addresses before tuning performance.
641
+
642
+ ## Configuration
643
+
644
+ Example starting point for a dedicated database host:
645
+
646
+ ```ini
647
+ [mysqld]
648
+ innodb_buffer_pool_size = 4G
649
+ innodb_flush_log_at_trx_commit = 1
650
+ sync_binlog = 1
651
+
652
+ max_connections = 300
653
+ thread_cache_size = 50
654
+
655
+ wait_timeout = 300
656
+ interactive_timeout = 300
657
+ innodb_lock_wait_timeout = 10
658
+
659
+ slow_query_log = ON
660
+ long_query_time = 1
661
+ log_queries_not_using_indexes = ON
662
+
663
+ log_bin = mysql-bin
664
+ binlog_format = ROW
665
+ binlog_expire_logs_seconds = 604800
666
+ ```
667
+
668
+ Treat configuration values as a prompt for review, not a universal preset. Size
669
+ memory, connections, log retention, and durability settings from workload,
670
+ hardware, backup policy, and recovery objectives.
671
+
672
+ ## Anti-Patterns
673
+
674
+ | Anti-Pattern | Risk | Better Pattern |
675
+ | --- | --- | --- |
676
+ | `SELECT *` in hot paths | Over-fetching and brittle clients | Select explicit columns |
677
+ | Deep `OFFSET` pagination | Linear scans and slow pages | Keyset pagination |
678
+ | No index on foreign-key joins | Slow joins and lock-heavy deletes | Index FK columns intentionally |
679
+ | Long transactions | Lock waits and large undo history | Commit small units of work |
680
+ | Direct DML against `mysql.user` | Grant-table corruption risk | Use `CREATE USER`, `ALTER USER`, `DROP USER` |
681
+ | Application user with admin grants | High blast radius | Least-privilege runtime user |
682
+ | Pool recycle above `wait_timeout` | Stale pooled connections | Recycle below timeout and pre-ping |
683
+ | Replica reads after writes | Stale user-facing state | Pin read-after-write flows to primary |
684
+
685
+ ## Output Expectations
686
+
687
+ When this skill is used for review, return:
688
+
689
+ 1. Engine/version assumptions.
690
+ 2. Highest-risk correctness, lock, security, and migration issues.
691
+ 3. Exact SQL or code changes for the safe path.
692
+ 4. Validation plan: `EXPLAIN`, migration dry run, lock/deadlock check, and
693
+ rollback criteria.
694
+ 5. Any MySQL/MariaDB syntax differences that affect the recommendation.
695
+
696
+ ## Related
697
+
698
+ - Skill: `postgres-patterns` - PostgreSQL-specific schema and query patterns
699
+ - Skill: `database-migrations` - migration planning and rollout safety
700
+ - Skill: `backend-patterns` - API and service-layer patterns
701
+ - Skill: `security-review` - secret handling, auth, and least privilege
702
+ - Agent: `database-reviewer` - broader database review workflow
703
+
704
+ ### database-migrations
705
+
706
+ Safe, reversible database schema-change patterns across PostgreSQL, MySQL, and common ORMs (Prisma, Drizzle, Kysely, Django, golang-migrate). Use when creating or altering tables, adding/removing columns or indexes, running data migrations, or planning zero-downtime schema changes.
707
+
708
+ # Database Migration Patterns
709
+
710
+ Safe, reversible database schema changes for production systems.
711
+
712
+ ## When to Activate
713
+
714
+ - Creating or altering database tables
715
+ - Adding/removing columns or indexes
716
+ - Running data migrations (backfill, transform)
717
+ - Planning zero-downtime schema changes
718
+ - Setting up migration tooling for a new project
719
+
720
+ ## Core Principles
721
+
722
+ 1. **Every change is a migration** — never alter production databases manually
723
+ 2. **Migrations are forward-only in production** — rollbacks use new forward migrations
724
+ 3. **Schema and data migrations are separate** — never mix DDL and DML in one migration
725
+ 4. **Test migrations against production-sized data** — a migration that works on 100 rows may lock on 10M
726
+ 5. **Migrations are immutable once deployed** — never edit a migration that has run in production
727
+
728
+ ## Migration Safety Checklist
729
+
730
+ Before applying any migration:
731
+
732
+ - [ ] Migration has both UP and DOWN (or is explicitly marked irreversible)
733
+ - [ ] No full table locks on large tables (use concurrent operations)
734
+ - [ ] New columns have defaults or are nullable (never add NOT NULL without default)
735
+ - [ ] Indexes created concurrently (not inline with CREATE TABLE for existing tables)
736
+ - [ ] Data backfill is a separate migration from schema change
737
+ - [ ] Tested against a copy of production data
738
+ - [ ] Rollback plan documented
739
+
740
+ ## PostgreSQL Patterns
741
+
742
+ ### Adding a Column Safely
743
+
744
+ ```sql
745
+ -- GOOD: Nullable column, no lock
746
+ ALTER TABLE users ADD COLUMN avatar_url TEXT;
747
+
748
+ -- GOOD: Column with default (Postgres 11+ is instant, no rewrite)
749
+ ALTER TABLE users ADD COLUMN is_active BOOLEAN NOT NULL DEFAULT true;
750
+
751
+ -- BAD: NOT NULL without default on existing table (requires full rewrite)
752
+ ALTER TABLE users ADD COLUMN role TEXT NOT NULL;
753
+ -- This locks the table and rewrites every row
754
+ ```
755
+
756
+ ### Adding an Index Without Downtime
757
+
758
+ ```sql
759
+ -- BAD: Blocks writes on large tables
760
+ CREATE INDEX idx_users_email ON users (email);
761
+
762
+ -- GOOD: Non-blocking, allows concurrent writes
763
+ CREATE INDEX CONCURRENTLY idx_users_email ON users (email);
764
+
765
+ -- Note: CONCURRENTLY cannot run inside a transaction block
766
+ -- Most migration tools need special handling for this
767
+ ```
768
+
769
+ ### Renaming a Column (Zero-Downtime)
770
+
771
+ Never rename directly in production. Use the expand-contract pattern:
772
+
773
+ ```sql
774
+ -- Step 1: Add new column (migration 001)
775
+ ALTER TABLE users ADD COLUMN display_name TEXT;
776
+
777
+ -- Step 2: Backfill data (migration 002, data migration)
778
+ UPDATE users SET display_name = username WHERE display_name IS NULL;
779
+
780
+ -- Step 3: Update application code to read/write both columns
781
+ -- Deploy application changes
782
+
783
+ -- Step 4: Stop writing to old column, drop it (migration 003)
784
+ ALTER TABLE users DROP COLUMN username;
785
+ ```
786
+
787
+ ### Removing a Column Safely
788
+
789
+ ```sql
790
+ -- Step 1: Remove all application references to the column
791
+ -- Step 2: Deploy application without the column reference
792
+ -- Step 3: Drop column in next migration
793
+ ALTER TABLE orders DROP COLUMN legacy_status;
794
+
795
+ -- For Django: use SeparateDatabaseAndState to remove from model
796
+ -- without generating DROP COLUMN (then drop in next migration)
797
+ ```
798
+
799
+ ### Large Data Migrations
800
+
801
+ ```sql
802
+ -- BAD: Updates all rows in one transaction (locks table)
803
+ UPDATE users SET normalized_email = LOWER(email);
804
+
805
+ -- GOOD: Batch update with progress
806
+ DO $$
807
+ DECLARE
808
+ batch_size INT := 10000;
809
+ rows_updated INT;
810
+ BEGIN
811
+ LOOP
812
+ UPDATE users
813
+ SET normalized_email = LOWER(email)
814
+ WHERE id IN (
815
+ SELECT id FROM users
816
+ WHERE normalized_email IS NULL
817
+ LIMIT batch_size
818
+ FOR UPDATE SKIP LOCKED
819
+ );
820
+ GET DIAGNOSTICS rows_updated = ROW_COUNT;
821
+ RAISE NOTICE 'Updated % rows', rows_updated;
822
+ EXIT WHEN rows_updated = 0;
823
+ COMMIT;
824
+ END LOOP;
825
+ END $$;
826
+ ```
827
+
828
+ ## Prisma (TypeScript/Node.js)
829
+
830
+ ### Workflow
831
+
832
+ ```bash
833
+ # Create migration from schema changes
834
+ npx prisma migrate dev --name add_user_avatar
835
+
836
+ # Apply pending migrations in production
837
+ npx prisma migrate deploy
838
+
839
+ # Reset database (dev only)
840
+ npx prisma migrate reset
841
+
842
+ # Generate client after schema changes
843
+ npx prisma generate
844
+ ```
845
+
846
+ ### Schema Example
847
+
848
+ ```prisma
849
+ model User {
850
+ id String @id @default(cuid())
851
+ email String @unique
852
+ name String?
853
+ avatarUrl String? @map("avatar_url")
854
+ createdAt DateTime @default(now()) @map("created_at")
855
+ updatedAt DateTime @updatedAt @map("updated_at")
856
+ orders Order[]
857
+
858
+ @@map("users")
859
+ @@index([email])
860
+ }
861
+ ```
862
+
863
+ ### Custom SQL Migration
864
+
865
+ For operations Prisma cannot express (concurrent indexes, data backfills):
866
+
867
+ ```bash
868
+ # Create empty migration, then edit the SQL manually
869
+ npx prisma migrate dev --create-only --name add_email_index
870
+ ```
871
+
872
+ ```sql
873
+ -- migrations/20240115_add_email_index/migration.sql
874
+ -- Prisma cannot generate CONCURRENTLY, so we write it manually
875
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email ON users (email);
876
+ ```
877
+
878
+ ## Drizzle (TypeScript/Node.js)
879
+
880
+ ### Workflow
881
+
882
+ ```bash
883
+ # Generate migration from schema changes
884
+ npx drizzle-kit generate
885
+
886
+ # Apply migrations
887
+ npx drizzle-kit migrate
888
+
889
+ # Push schema directly (dev only, no migration file)
890
+ npx drizzle-kit push
891
+ ```
892
+
893
+ ### Schema Example
894
+
895
+ ```typescript
896
+ import { pgTable, text, timestamp, uuid, boolean } from "drizzle-orm/pg-core";
897
+
898
+ export const users = pgTable("users", {
899
+ id: uuid("id").primaryKey().defaultRandom(),
900
+ email: text("email").notNull().unique(),
901
+ name: text("name"),
902
+ isActive: boolean("is_active").notNull().default(true),
903
+ createdAt: timestamp("created_at").notNull().defaultNow(),
904
+ updatedAt: timestamp("updated_at").notNull().defaultNow(),
905
+ });
906
+ ```
907
+
908
+ ## Kysely (TypeScript/Node.js)
909
+
910
+ ### Workflow (kysely-ctl)
911
+
912
+ ```bash
913
+ # Initialize config file (kysely.config.ts)
914
+ kysely init
915
+
916
+ # Create a new migration file
917
+ kysely migrate make add_user_avatar
918
+
919
+ # Apply all pending migrations
920
+ kysely migrate latest
921
+
922
+ # Rollback last migration
923
+ kysely migrate down
924
+
925
+ # Show migration status
926
+ kysely migrate list
927
+ ```
928
+
929
+ ### Migration File
930
+
931
+ ```typescript
932
+ // migrations/2024_01_15_001_create_user_profile.ts
933
+ import { type Kysely, sql } from 'kysely'
934
+
935
+ // IMPORTANT: Always use Kysely<any>, not your typed DB interface.
936
+ // Migrations are frozen in time and must not depend on current schema types.
937
+ export async function up(db: Kysely<any>): Promise<void> {
938
+ await db.schema
939
+ .createTable('user_profile')
940
+ .addColumn('id', 'serial', (col) => col.primaryKey())
941
+ .addColumn('email', 'varchar(255)', (col) => col.notNull().unique())
942
+ .addColumn('avatar_url', 'text')
943
+ .addColumn('created_at', 'timestamp', (col) =>
944
+ col.defaultTo(sql`now()`).notNull()
945
+ )
946
+ .execute()
947
+
948
+ await db.schema
949
+ .createIndex('idx_user_profile_avatar')
950
+ .on('user_profile')
951
+ .column('avatar_url')
952
+ .execute()
953
+ }
954
+
955
+ export async function down(db: Kysely<any>): Promise<void> {
956
+ await db.schema.dropTable('user_profile').execute()
957
+ }
958
+ ```
959
+
960
+ ### Programmatic Migrator
961
+
962
+ ```typescript
963
+ import { Migrator, FileMigrationProvider } from 'kysely'
964
+ import { promises as fs } from 'fs'
965
+ import * as path from 'path'
966
+ // ESM only — CJS can use __dirname directly
967
+ import { fileURLToPath } from 'url'
968
+ const migrationFolder = path.join(
969
+ path.dirname(fileURLToPath(import.meta.url)),
970
+ './migrations',
971
+ )
972
+
973
+ // `db` is your Kysely<any> database instance
974
+ const migrator = new Migrator({
975
+ db,
976
+ provider: new FileMigrationProvider({
977
+ fs,
978
+ path,
979
+ migrationFolder,
980
+ }),
981
+ // WARNING: Only enable in development. Disables timestamp-ordering
982
+ // validation, which can cause schema drift between environments.
983
+ // allowUnorderedMigrations: true,
984
+ })
985
+
986
+ const { error, results } = await migrator.migrateToLatest()
987
+
988
+ results?.forEach((it) => {
989
+ if (it.status === 'Success') {
990
+ console.log(`migration "${it.migrationName}" executed successfully`)
991
+ } else if (it.status === 'Error') {
992
+ console.error(`failed to execute migration "${it.migrationName}"`)
993
+ }
994
+ })
995
+
996
+ if (error) {
997
+ console.error('migration failed', error)
998
+ process.exit(1)
999
+ }
1000
+ ```
1001
+
1002
+ ## Django (Python)
1003
+
1004
+ ### Workflow
1005
+
1006
+ ```bash
1007
+ # Generate migration from model changes
1008
+ python manage.py makemigrations
1009
+
1010
+ # Apply migrations
1011
+ python manage.py migrate
1012
+
1013
+ # Show migration status
1014
+ python manage.py showmigrations
1015
+
1016
+ # Generate empty migration for custom SQL
1017
+ python manage.py makemigrations --empty app_name -n description
1018
+ ```
1019
+
1020
+ ### Data Migration
1021
+
1022
+ ```python
1023
+ from django.db import migrations
1024
+
1025
+ def backfill_display_names(apps, schema_editor):
1026
+ User = apps.get_model("accounts", "User")
1027
+ batch_size = 5000
1028
+ users = User.objects.filter(display_name="")
1029
+ while users.exists():
1030
+ batch = list(users[:batch_size])
1031
+ for user in batch:
1032
+ user.display_name = user.username
1033
+ User.objects.bulk_update(batch, ["display_name"], batch_size=batch_size)
1034
+
1035
+ def reverse_backfill(apps, schema_editor):
1036
+ pass # Data migration, no reverse needed
1037
+
1038
+ class Migration(migrations.Migration):
1039
+ dependencies = [("accounts", "0015_add_display_name")]
1040
+
1041
+ operations = [
1042
+ migrations.RunPython(backfill_display_names, reverse_backfill),
1043
+ ]
1044
+ ```
1045
+
1046
+ ### SeparateDatabaseAndState
1047
+
1048
+ Remove a column from the Django model without dropping it from the database immediately:
1049
+
1050
+ ```python
1051
+ class Migration(migrations.Migration):
1052
+ operations = [
1053
+ migrations.SeparateDatabaseAndState(
1054
+ state_operations=[
1055
+ migrations.RemoveField(model_name="user", name="legacy_field"),
1056
+ ],
1057
+ database_operations=[], # Don't touch the DB yet
1058
+ ),
1059
+ ]
1060
+ ```
1061
+
1062
+ ## golang-migrate (Go)
1063
+
1064
+ ### Workflow
1065
+
1066
+ ```bash
1067
+ # Create migration pair
1068
+ migrate create -ext sql -dir migrations -seq add_user_avatar
1069
+
1070
+ # Apply all pending migrations
1071
+ migrate -path migrations -database "$DATABASE_URL" up
1072
+
1073
+ # Rollback last migration
1074
+ migrate -path migrations -database "$DATABASE_URL" down 1
1075
+
1076
+ # Force version (fix dirty state)
1077
+ migrate -path migrations -database "$DATABASE_URL" force VERSION
1078
+ ```
1079
+
1080
+ ### Migration Files
1081
+
1082
+ ```sql
1083
+ -- migrations/000003_add_user_avatar.up.sql
1084
+ ALTER TABLE users ADD COLUMN avatar_url TEXT;
1085
+ CREATE INDEX CONCURRENTLY idx_users_avatar ON users (avatar_url) WHERE avatar_url IS NOT NULL;
1086
+
1087
+ -- migrations/000003_add_user_avatar.down.sql
1088
+ DROP INDEX IF EXISTS idx_users_avatar;
1089
+ ALTER TABLE users DROP COLUMN IF EXISTS avatar_url;
1090
+ ```
1091
+
1092
+ ## Zero-Downtime Migration Strategy
1093
+
1094
+ For critical production changes, follow the expand-contract pattern:
1095
+
1096
+ ```
1097
+ Phase 1: EXPAND
1098
+ - Add new column/table (nullable or with default)
1099
+ - Deploy: app writes to BOTH old and new
1100
+ - Backfill existing data
1101
+
1102
+ Phase 2: MIGRATE
1103
+ - Deploy: app reads from NEW, writes to BOTH
1104
+ - Verify data consistency
1105
+
1106
+ Phase 3: CONTRACT
1107
+ - Deploy: app only uses NEW
1108
+ - Drop old column/table in separate migration
1109
+ ```
1110
+
1111
+ ### Timeline Example
1112
+
1113
+ ```
1114
+ Day 1: Migration adds new_status column (nullable)
1115
+ Day 1: Deploy app v2 — writes to both status and new_status
1116
+ Day 2: Run backfill migration for existing rows
1117
+ Day 3: Deploy app v3 — reads from new_status only
1118
+ Day 7: Migration drops old status column
1119
+ ```
1120
+
1121
+ ## Anti-Patterns
1122
+
1123
+ | Anti-Pattern | Why It Fails | Better Approach |
1124
+ |-------------|-------------|-----------------|
1125
+ | Manual SQL in production | No audit trail, unrepeatable | Always use migration files |
1126
+ | Editing deployed migrations | Causes drift between environments | Create new migration instead |
1127
+ | NOT NULL without default | Locks table, rewrites all rows | Add nullable, backfill, then add constraint |
1128
+ | Inline index on large table | Blocks writes during build | CREATE INDEX CONCURRENTLY |
1129
+ | Schema + data in one migration | Hard to rollback, long transactions | Separate migrations |
1130
+ | Dropping column before removing code | Application errors on missing column | Remove code first, drop column next deploy |