cfsa-antigravity 2.0.0 → 2.2.0

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 (116) hide show
  1. package/README.md +14 -0
  2. package/package.json +1 -1
  3. package/template/.agent/instructions/commands.md +8 -32
  4. package/template/.agent/instructions/example.md +21 -0
  5. package/template/.agent/instructions/patterns.md +3 -3
  6. package/template/.agent/instructions/tech-stack.md +71 -23
  7. package/template/.agent/instructions/workflow.md +12 -1
  8. package/template/.agent/rules/completion-checklist.md +6 -0
  9. package/template/.agent/rules/security-first.md +3 -3
  10. package/template/.agent/rules/vertical-slices.md +1 -1
  11. package/template/.agent/skill-library/MANIFEST.md +6 -0
  12. package/template/.agent/skill-library/stack/devops/git-advanced/SKILL.md +972 -0
  13. package/template/.agent/skill-library/stack/devops/git-workflow/SKILL.md +420 -0
  14. package/template/.agent/skills/api-versioning/SKILL.md +44 -298
  15. package/template/.agent/skills/api-versioning/references/typescript.md +157 -0
  16. package/template/.agent/skills/architecture-mapping/SKILL.md +13 -13
  17. package/template/.agent/skills/bootstrap-agents/SKILL.md +151 -152
  18. package/template/.agent/skills/clean-code/SKILL.md +64 -118
  19. package/template/.agent/skills/clean-code/references/typescript.md +126 -0
  20. package/template/.agent/skills/database-schema-design/SKILL.md +93 -317
  21. package/template/.agent/skills/database-schema-design/references/relational.md +228 -0
  22. package/template/.agent/skills/error-handling-patterns/SKILL.md +62 -557
  23. package/template/.agent/skills/error-handling-patterns/references/go.md +162 -0
  24. package/template/.agent/skills/error-handling-patterns/references/python.md +262 -0
  25. package/template/.agent/skills/error-handling-patterns/references/rust.md +112 -0
  26. package/template/.agent/skills/error-handling-patterns/references/typescript.md +178 -0
  27. package/template/.agent/skills/idea-extraction/SKILL.md +322 -224
  28. package/template/.agent/skills/logging-best-practices/SKILL.md +108 -767
  29. package/template/.agent/skills/logging-best-practices/references/go.md +49 -0
  30. package/template/.agent/skills/logging-best-practices/references/python.md +52 -0
  31. package/template/.agent/skills/logging-best-practices/references/typescript.md +215 -0
  32. package/template/.agent/skills/migration-management/SKILL.md +127 -311
  33. package/template/.agent/skills/migration-management/references/relational.md +214 -0
  34. package/template/.agent/skills/parallel-feature-development/SKILL.md +34 -43
  35. package/template/.agent/skills/pipeline-rubrics/references/be-rubric.md +1 -1
  36. package/template/.agent/skills/pipeline-rubrics/references/ia-rubric.md +2 -2
  37. package/template/.agent/skills/pipeline-rubrics/references/scoring.md +1 -1
  38. package/template/.agent/skills/pipeline-rubrics/references/vision-rubric.md +2 -1
  39. package/template/.agent/skills/prd-templates/SKILL.md +23 -6
  40. package/template/.agent/skills/prd-templates/references/be-spec-template.md +2 -2
  41. package/template/.agent/skills/prd-templates/references/decomposition-templates.md +2 -2
  42. package/template/.agent/skills/prd-templates/references/engineering-standards-template.md +2 -0
  43. package/template/.agent/skills/prd-templates/references/fe-spec-template.md +1 -1
  44. package/template/.agent/skills/prd-templates/references/fractal-cx-template.md +58 -0
  45. package/template/.agent/skills/prd-templates/references/fractal-feature-template.md +93 -0
  46. package/template/.agent/skills/prd-templates/references/fractal-node-index-template.md +55 -0
  47. package/template/.agent/skills/prd-templates/references/ideation-crosscut-template.md +26 -47
  48. package/template/.agent/skills/prd-templates/references/ideation-index-template.md +47 -31
  49. package/template/.agent/skills/prd-templates/references/operational-templates.md +1 -1
  50. package/template/.agent/skills/prd-templates/references/placeholder-workflow-mapping.md +50 -21
  51. package/template/.agent/skills/prd-templates/references/skill-loading-protocol.md +32 -0
  52. package/template/.agent/skills/prd-templates/references/slice-completion-gates.md +29 -0
  53. package/template/.agent/skills/prd-templates/references/spec-coverage-sweep.md +3 -3
  54. package/template/.agent/skills/prd-templates/references/tdd-testing-policy.md +39 -0
  55. package/template/.agent/skills/prd-templates/references/vision-template.md +8 -8
  56. package/template/.agent/skills/regex-patterns/SKILL.md +122 -540
  57. package/template/.agent/skills/regex-patterns/references/go.md +44 -0
  58. package/template/.agent/skills/regex-patterns/references/javascript.md +63 -0
  59. package/template/.agent/skills/regex-patterns/references/python.md +77 -0
  60. package/template/.agent/skills/regex-patterns/references/rust.md +43 -0
  61. package/template/.agent/skills/resolve-ambiguity/SKILL.md +1 -1
  62. package/template/.agent/skills/session-continuity/SKILL.md +11 -9
  63. package/template/.agent/skills/session-continuity/protocols/02-progress-generation.md +2 -2
  64. package/template/.agent/skills/session-continuity/protocols/04-pattern-extraction.md +1 -1
  65. package/template/.agent/skills/session-continuity/protocols/05-session-close.md +1 -1
  66. package/template/.agent/skills/session-continuity/protocols/09-parallel-claim.md +1 -1
  67. package/template/.agent/skills/session-continuity/protocols/10-placeholder-verification-gate.md +57 -78
  68. package/template/.agent/skills/session-continuity/protocols/11-parallel-synthesis.md +1 -1
  69. package/template/.agent/skills/spec-writing/SKILL.md +1 -1
  70. package/template/.agent/skills/tdd-workflow/SKILL.md +94 -317
  71. package/template/.agent/skills/tdd-workflow/references/typescript.md +231 -0
  72. package/template/.agent/skills/testing-strategist/SKILL.md +74 -687
  73. package/template/.agent/skills/testing-strategist/references/typescript.md +328 -0
  74. package/template/.agent/skills/workflow-automation/SKILL.md +62 -154
  75. package/template/.agent/skills/workflow-automation/references/inngest.md +88 -0
  76. package/template/.agent/skills/workflow-automation/references/temporal.md +64 -0
  77. package/template/.agent/workflows/bootstrap-agents-fill.md +85 -143
  78. package/template/.agent/workflows/bootstrap-agents-provision.md +90 -107
  79. package/template/.agent/workflows/create-prd-architecture.md +23 -16
  80. package/template/.agent/workflows/create-prd-compile.md +11 -12
  81. package/template/.agent/workflows/create-prd-design-system.md +1 -1
  82. package/template/.agent/workflows/create-prd-security.md +9 -11
  83. package/template/.agent/workflows/create-prd-stack.md +10 -4
  84. package/template/.agent/workflows/create-prd.md +9 -9
  85. package/template/.agent/workflows/decompose-architecture-structure.md +4 -6
  86. package/template/.agent/workflows/decompose-architecture-validate.md +18 -1
  87. package/template/.agent/workflows/decompose-architecture.md +18 -3
  88. package/template/.agent/workflows/evolve-contract.md +11 -11
  89. package/template/.agent/workflows/evolve-feature-classify.md +14 -6
  90. package/template/.agent/workflows/ideate-discover.md +72 -107
  91. package/template/.agent/workflows/ideate-extract.md +84 -63
  92. package/template/.agent/workflows/ideate-validate.md +26 -22
  93. package/template/.agent/workflows/ideate.md +9 -9
  94. package/template/.agent/workflows/implement-slice-setup.md +25 -23
  95. package/template/.agent/workflows/implement-slice-tdd.md +73 -89
  96. package/template/.agent/workflows/implement-slice.md +4 -4
  97. package/template/.agent/workflows/plan-phase-preflight.md +6 -2
  98. package/template/.agent/workflows/plan-phase-write.md +6 -8
  99. package/template/.agent/workflows/remediate-pipeline-assess.md +2 -1
  100. package/template/.agent/workflows/resolve-ambiguity.md +2 -2
  101. package/template/.agent/workflows/update-architecture-map.md +22 -5
  102. package/template/.agent/workflows/validate-phase-quality.md +155 -0
  103. package/template/.agent/workflows/validate-phase-readiness.md +167 -0
  104. package/template/.agent/workflows/validate-phase.md +19 -157
  105. package/template/.agent/workflows/verify-infrastructure.md +10 -10
  106. package/template/.agent/workflows/write-architecture-spec-design.md +23 -14
  107. package/template/.agent/workflows/write-be-spec-classify.md +25 -21
  108. package/template/.agent/workflows/write-be-spec.md +1 -1
  109. package/template/.agent/workflows/write-fe-spec-classify.md +6 -12
  110. package/template/.agent/workflows/write-fe-spec-write.md +1 -1
  111. package/template/AGENTS.md +6 -2
  112. package/template/GEMINI.md +5 -3
  113. package/template/docs/README.md +10 -10
  114. package/template/docs/kit-architecture.md +126 -33
  115. package/template/docs/plans/ideation/README.md +8 -3
  116. package/template/.agent/skills/prd-templates/references/ideation-domain-template.md +0 -55
@@ -1,384 +1,200 @@
1
1
  ---
2
2
  name: database-migration-management
3
- description: Manage database migrations and schema versioning. Use when planning migrations, version control, rollback strategies, or data transformations in PostgreSQL and MySQL.
3
+ description: Manage database migrations and schema versioning. Use when planning migrations, version control, rollback strategies, or data transformations for any database paradigm.
4
4
  ---
5
5
 
6
6
  # Database Migration Management
7
7
 
8
8
  ## Overview
9
9
 
10
- Implement robust database migration systems with version control, rollback capabilities, and data transformation strategies. Includes migration frameworks and production deployment patterns.
10
+ Implement robust database migration systems with version control, rollback capabilities, and data transformation strategies. This skill covers universal migration methodology. For paradigm-specific tooling, see references.
11
11
 
12
12
  ## When to Use
13
13
 
14
14
  - Schema versioning and evolution
15
15
  - Data transformations and cleanup
16
- - Adding/removing tables and columns
16
+ - Adding/removing tables, columns, collections, or fields
17
17
  - Index creation and optimization
18
18
  - Migration testing and validation
19
19
  - Rollback planning and execution
20
20
  - Multi-environment deployments
21
21
 
22
- ## Migration Framework Setup
23
-
24
- ### PostgreSQL - Schema Versioning
25
-
26
- ```sql
27
- -- Create migrations tracking table
28
- CREATE TABLE schema_migrations (
29
- version BIGINT PRIMARY KEY,
30
- name VARCHAR(255) NOT NULL,
31
- executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
32
- duration_ms INTEGER,
33
- checksum VARCHAR(64)
34
- );
35
-
36
- -- Create migration log table
37
- CREATE TABLE migration_logs (
38
- id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
39
- version BIGINT NOT NULL,
40
- status VARCHAR(20) NOT NULL,
41
- error_message TEXT,
42
- rolled_back_at TIMESTAMP,
43
- executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
44
- );
45
-
46
- -- Function to record migration
47
- CREATE OR REPLACE FUNCTION record_migration(
48
- p_version BIGINT,
49
- p_name VARCHAR,
50
- p_duration_ms INTEGER
51
- ) RETURNS void AS $$
52
- BEGIN
53
- INSERT INTO schema_migrations (version, name, duration_ms)
54
- VALUES (p_version, p_name, p_duration_ms)
55
- ON CONFLICT (version) DO UPDATE
56
- SET executed_at = CURRENT_TIMESTAMP;
57
- END;
58
- $$ LANGUAGE plpgsql;
59
- ```
60
-
61
- ### MySQL - Migration Tracking
62
-
63
- ```sql
64
- -- Create migrations table for MySQL
65
- CREATE TABLE schema_migrations (
66
- version BIGINT PRIMARY KEY,
67
- name VARCHAR(255) NOT NULL,
68
- executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
69
- duration_ms INT,
70
- checksum VARCHAR(64)
71
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
72
-
73
- -- Migration status table
74
- CREATE TABLE migration_status (
75
- id INT AUTO_INCREMENT PRIMARY KEY,
76
- version BIGINT NOT NULL,
77
- status ENUM('pending', 'completed', 'failed', 'rolled_back'),
78
- error_message TEXT,
79
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
80
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
81
- ```
22
+ ## Paradigm-Specific References
82
23
 
83
- ## Common Migration Patterns
84
-
85
- ### Adding Columns
86
-
87
- **PostgreSQL - Safe Column Addition:**
88
-
89
- ```sql
90
- -- Migration: 20240115_001_add_phone_to_users.sql
91
-
92
- -- Add column with default (non-blocking)
93
- ALTER TABLE users
94
- ADD COLUMN phone VARCHAR(20) DEFAULT '';
95
-
96
- -- Add constraint after population
97
- ALTER TABLE users
98
- ADD CONSTRAINT phone_format
99
- CHECK (phone = '' OR phone ~ '^\+?[0-9\-\(\)]{10,}$');
100
-
101
- -- Create index
102
- CREATE INDEX CONCURRENTLY idx_users_phone ON users(phone);
103
-
104
- -- Rollback:
105
- -- DROP INDEX CONCURRENTLY idx_users_phone;
106
- -- ALTER TABLE users DROP COLUMN phone;
107
- ```
108
-
109
- **MySQL - Column Addition:**
24
+ After reading the methodology below, read the reference matching your surface's Databases column in the surface stack map (`.agent/instructions/tech-stack.md`):
110
25
 
111
- ```sql
112
- -- Migration: 20240115_001_add_phone_to_users.sql
26
+ | Paradigm | Reference | Example Stores |
27
+ |----------|-----------|----------------|
28
+ | Relational | `references/relational.md` | PostgreSQL, MySQL, SQLite |
29
+ | Document | `references/document.md` | MongoDB, Firestore, CouchDB |
30
+ | Graph | `references/graph.md` | SurrealDB, Neo4j, ArangoDB |
113
31
 
114
- -- Add column with ALTER
115
- ALTER TABLE users
116
- ADD COLUMN phone VARCHAR(20) DEFAULT '',
117
- ADD INDEX idx_phone (phone);
118
-
119
- -- Rollback:
120
- -- ALTER TABLE users DROP COLUMN phone;
121
- ```
122
-
123
- ### Renaming Columns
124
-
125
- **PostgreSQL - Column Rename:**
126
-
127
- ```sql
128
- -- Migration: 20240115_002_rename_user_name_columns.sql
129
-
130
- -- Rename columns
131
- ALTER TABLE users RENAME COLUMN user_name TO full_name;
132
- ALTER TABLE users RENAME COLUMN user_email TO email_address;
133
-
134
- -- Update indexes
135
- REINDEX TABLE users;
32
+ ---
136
33
 
137
- -- Rollback:
138
- -- ALTER TABLE users RENAME COLUMN email_address TO user_email;
139
- -- ALTER TABLE users RENAME COLUMN full_name TO user_name;
140
- ```
34
+ ## Universal Migration Principles
141
35
 
142
- ### Creating Indexes Non-blocking
36
+ ### 1. Bidirectional Migrations
143
37
 
144
- **PostgreSQL - Concurrent Index Creation:**
38
+ Every migration must have both directions:
39
+ - **UP** — applies the change
40
+ - **DOWN** — reverses the change
145
41
 
146
- ```sql
147
- -- Migration: 20240115_003_add_performance_indexes.sql
42
+ If a migration cannot be reversed (e.g., data deletion), document this explicitly and require a backup before execution.
148
43
 
149
- -- Create indexes without blocking writes
150
- CREATE INDEX CONCURRENTLY idx_orders_user_created
151
- ON orders(user_id, created_at DESC);
44
+ ### 2. Migration Ordering
152
45
 
153
- CREATE INDEX CONCURRENTLY idx_products_category_active
154
- ON products(category_id)
155
- WHERE active = true;
46
+ Migrations are **sequentially ordered** by timestamp or version number:
47
+ - Timestamp format: `YYYYMMDD_NNN_description` (e.g., `20240115_001_add_phone_to_users`)
48
+ - Never reorder or renumber existing migrations
49
+ - Never modify an already-executed migration — create a new one
156
50
 
157
- -- Verify index creation
158
- SELECT schemaname, tablename, indexname, idx_scan
159
- FROM pg_stat_user_indexes
160
- WHERE indexname LIKE 'idx_%';
51
+ ### 3. Idempotency
161
52
 
162
- -- Rollback:
163
- -- DROP INDEX CONCURRENTLY idx_orders_user_created;
164
- -- DROP INDEX CONCURRENTLY idx_products_category_active;
165
- ```
53
+ Migrations should be idempotent where possible:
54
+ - Use "IF NOT EXISTS" / "IF EXISTS" guards
55
+ - Check for the presence of the change before applying it
56
+ - Run pre-flight checks before destructive operations
166
57
 
167
- **MySQL - Online Index Creation:**
58
+ ### 4. Atomic Migrations
168
59
 
169
- ```sql
170
- -- Migration: 20240115_003_add_performance_indexes.sql
60
+ Each migration should be a single logical change:
61
+ - One migration = one purpose (add column, create table, transform data)
62
+ - Don't combine unrelated changes
63
+ - Use transactions for multi-statement migrations where supported
171
64
 
172
- -- Create indexes with ALGORITHM=INPLACE and LOCK=NONE
173
- ALTER TABLE orders
174
- ADD INDEX idx_user_created (user_id, created_at),
175
- ALGORITHM=INPLACE, LOCK=NONE;
65
+ ---
176
66
 
177
- -- Monitor progress
178
- SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST
179
- WHERE INFO LIKE 'ALTER TABLE%';
180
- ```
67
+ ## Common Migration Patterns
181
68
 
182
- ### Data Transformations
69
+ ### Safe Schema Changes
183
70
 
184
- **PostgreSQL - Data Cleanup Migration:**
71
+ | Operation | Safe? | Strategy |
72
+ |-----------|-------|----------|
73
+ | Add nullable column | ✅ Always safe | Add with default or NULL |
74
+ | Add NOT NULL column | ⚠️ Requires backfill | Add nullable → backfill → add constraint |
75
+ | Rename column | ⚠️ Requires coordination | Add new → copy data → update app → drop old |
76
+ | Drop column | ⚠️ Requires coordination | Stop reading → deploy → drop column |
77
+ | Add index | ⚠️ Can lock table | Use concurrent/online index creation |
78
+ | Drop table | 🔴 Destructive | Backup first, confirm no references |
185
79
 
186
- ```sql
187
- -- Migration: 20240115_004_normalize_email_addresses.sql
80
+ ### Multi-Phase Migrations
188
81
 
189
- -- Normalize existing email addresses
190
- UPDATE users
191
- SET email = LOWER(TRIM(email))
192
- WHERE email != LOWER(TRIM(email));
82
+ For non-trivial schema changes (rename, type change, restructure):
193
83
 
194
- -- Remove duplicates by keeping latest
195
- DELETE FROM users
196
- WHERE id NOT IN (
197
- SELECT DISTINCT ON (LOWER(email)) id
198
- FROM users
199
- ORDER BY LOWER(email), created_at DESC
200
- );
84
+ 1. **Phase 1: Expand** Add new structure alongside old
85
+ 2. **Phase 2: Migrate** — Copy/transform data to new structure
86
+ 3. **Phase 3: Contract** — Remove old structure after verification
201
87
 
202
- -- Rollback: Restore from backup (no safe rollback for data changes)
203
- ```
88
+ This pattern prevents downtime and allows rollback at any phase.
204
89
 
205
- **MySQL - Bulk Data Update:**
90
+ ### Data Transformation Migrations
206
91
 
207
- ```sql
208
- -- Migration: 20240115_004_update_product_categories.sql
92
+ When transforming data (normalization, cleanup, format changes):
209
93
 
210
- -- Update multiple rows with JOIN
211
- UPDATE products p
212
- JOIN category_mapping cm ON p.old_category = cm.old_name
213
- SET p.category_id = cm.new_category_id
214
- WHERE p.old_category IS NOT NULL;
94
+ 1. **Verify source data** Count records, check constraints
95
+ 2. **Apply transformation** — Batch for large datasets
96
+ 3. **Validate results** Compare counts, spot-check values
97
+ 4. **Document irreversibility** — If rollback requires backup restore, say so
215
98
 
216
- -- Verify update
217
- SELECT COUNT(*) as updated_count
218
- FROM products
219
- WHERE category_id IS NOT NULL;
220
- ```
99
+ ---
221
100
 
222
- ### Table Structure Changes
101
+ ## Rollback Strategies
223
102
 
224
- **PostgreSQL - Alter Table Migration:**
103
+ ### Per-Migration Rollback
225
104
 
226
- ```sql
227
- -- Migration: 20240115_005_modify_order_columns.sql
105
+ Every migration has a DOWN direction. Execute in reverse order:
106
+ - Migration 5 DOWN
107
+ - Migration 4 DOWN
108
+ - Migration 3 DOWN
109
+ - ... until target version reached
228
110
 
229
- -- Add new column
230
- ALTER TABLE orders
231
- ADD COLUMN status_updated_at TIMESTAMP;
111
+ ### Point-in-Time Recovery
232
112
 
233
- -- Add constraint
234
- ALTER TABLE orders
235
- ADD CONSTRAINT valid_status
236
- CHECK (status IN ('pending', 'processing', 'completed', 'cancelled'));
113
+ For data-destructive migrations:
114
+ - Require backup before execution
115
+ - Document the backup location
116
+ - Test restore procedure before migration
237
117
 
238
- -- Set default for existing records
239
- UPDATE orders
240
- SET status_updated_at = updated_at
241
- WHERE status_updated_at IS NULL;
118
+ ### Blue-Green Schema Changes
242
119
 
243
- -- Make column NOT NULL
244
- ALTER TABLE orders
245
- ALTER COLUMN status_updated_at SET NOT NULL;
120
+ For zero-downtime migrations:
121
+ - Run new schema alongside old
122
+ - Application supports both schemas during transition
123
+ - Cut over when new schema is verified
124
+ - Drop old schema after observation period
246
125
 
247
- -- Rollback:
248
- -- ALTER TABLE orders DROP COLUMN status_updated_at;
249
- -- ALTER TABLE orders DROP CONSTRAINT valid_status;
250
- ```
126
+ ---
251
127
 
252
128
  ## Testing Migrations
253
129
 
254
- **PostgreSQL - Test in Transaction:**
255
-
256
- ```sql
257
- -- Test migration in transaction (will be rolled back)
258
- BEGIN;
259
-
260
- -- Run migration statements
261
- ALTER TABLE users ADD COLUMN test_column VARCHAR(255);
130
+ ### Local Testing
262
131
 
263
- -- Validate data
264
- SELECT COUNT(*) FROM users;
265
- SELECT COUNT(DISTINCT email) FROM users;
132
+ 1. **Fresh database** — Run all migrations from scratch (empty → current)
133
+ 2. **Incremental** Run only new migrations on a copy of production
134
+ 3. **Rollback** Test DOWN migration after UP
135
+ 4. **Data integrity** — Verify constraints hold after migration
266
136
 
267
- -- Rollback if issues found
268
- ROLLBACK;
137
+ ### Staging Validation
269
138
 
270
- -- Or commit if all good
271
- COMMIT;
272
- ```
139
+ 1. Restore production backup to staging
140
+ 2. Run migrations on staging
141
+ 3. Verify application works against migrated schema
142
+ 4. Run automated tests against staging
143
+ 5. Record migration timing for capacity planning
273
144
 
274
- **Validate Migration:**
145
+ ### Pre-Production Checklist
275
146
 
276
- ```sql
277
- -- Check migration was applied
278
- SELECT version, name, executed_at FROM schema_migrations
279
- WHERE version = 20240115005;
147
+ - [ ] Tested on local database
148
+ - [ ] Tested on staging with production data copy
149
+ - [ ] Rollback procedure tested
150
+ - [ ] Backup verified before migration
151
+ - [ ] Application code compatible with both old and new schema
152
+ - [ ] Migration timing measured (acceptable for maintenance window?)
153
+ - [ ] Long-running query impact assessed
280
154
 
281
- -- Verify table structure
282
- SELECT column_name, data_type, is_nullable
283
- FROM information_schema.columns
284
- WHERE table_name = 'users'
285
- ORDER BY ordinal_position;
286
- ```
287
-
288
- ## Rollback Strategies
289
-
290
- **PostgreSQL - Bidirectional Migrations:**
291
-
292
- ```sql
293
- -- Migration file: 20240115_006_add_user_status.sql
294
-
295
- -- ===== UP =====
296
- CREATE TYPE user_status AS ENUM ('active', 'suspended', 'deleted');
297
- ALTER TABLE users ADD COLUMN status user_status DEFAULT 'active';
298
-
299
- -- ===== DOWN =====
300
- -- ALTER TABLE users DROP COLUMN status;
301
- -- DROP TYPE user_status;
302
- ```
303
-
304
- **Rollback Execution:**
305
-
306
- ```sql
307
- -- Function to rollback to specific version
308
- CREATE OR REPLACE FUNCTION rollback_to_version(p_target_version BIGINT)
309
- RETURNS TABLE (version BIGINT, name VARCHAR, status VARCHAR) AS $$
310
- BEGIN
311
- -- Execute down migrations in reverse order
312
- RETURN QUERY
313
- SELECT m.version, m.name, 'rolled_back'::VARCHAR
314
- FROM schema_migrations m
315
- WHERE m.version > p_target_version
316
- ORDER BY m.version DESC;
317
- END;
318
- $$ LANGUAGE plpgsql;
319
- ```
155
+ ---
320
156
 
321
157
  ## Production Deployment
322
158
 
323
- **Safe Migration Checklist:**
159
+ ### Safe Migration Checklist
324
160
 
325
- - Test migration on production-like database
326
- - Verify backup exists before migration
327
- - Schedule during low-traffic window
328
- - Monitor table locks and long-running queries
329
- - Have rollback plan ready
330
- - Test rollback procedure
331
- - Document all changes
332
- - Run in transaction when possible
333
- - Verify data integrity after migration
334
- - Update application code coordinated with migration
161
+ - [ ] Test migration on production-like database
162
+ - [ ] Verify backup exists before migration
163
+ - [ ] Schedule during low-traffic window
164
+ - [ ] Monitor locks and long-running queries
165
+ - [ ] Have rollback plan ready and tested
166
+ - [ ] Document all changes
167
+ - [ ] Run in transaction when possible
168
+ - [ ] Verify data integrity after migration
169
+ - [ ] Coordinate application code deployment with migration
170
+ - [ ] Notify team of migration window
335
171
 
336
- **PostgreSQL - Long Transaction Safety:**
172
+ ### Post-Migration Verification
337
173
 
338
- ```sql
339
- -- Use statement timeout to prevent hanging migrations
340
- SET statement_timeout = '30min';
174
+ 1. **Schema check** — Verify new structures exist
175
+ 2. **Data check** Verify data integrity constraints
176
+ 3. **Application check** — Verify application reads/writes correctly
177
+ 4. **Performance check** — Verify query performance hasn't degraded
178
+ 5. **Monitoring** — Watch error rates for 24 hours post-migration
341
179
 
342
- -- Use lock timeout to prevent deadlocks
343
- SET lock_timeout = '5min';
344
-
345
- -- Run migration with timeouts
346
- ALTER TABLE large_table
347
- ADD COLUMN new_column VARCHAR(255),
348
- ALGORITHM='INPLACE';
349
- ```
350
-
351
- ## Migration Examples
352
-
353
- **Combined Migration - Multiple Changes:**
354
-
355
- ```sql
356
- -- Migration: 20240115_007_refactor_user_tables.sql
357
-
358
- BEGIN;
359
-
360
- -- 1. Create new column with data from old column
361
- ALTER TABLE users ADD COLUMN full_name VARCHAR(255);
362
- UPDATE users SET full_name = first_name || ' ' || last_name;
363
-
364
- -- 2. Add indexes
365
- CREATE INDEX idx_users_full_name ON users(full_name);
180
+ ---
366
181
 
367
- -- 3. Add new constraint
368
- ALTER TABLE users
369
- ADD CONSTRAINT email_unique UNIQUE(email);
182
+ ## Migration Tracking
370
183
 
371
- -- 4. Drop old columns (after verification)
372
- -- ALTER TABLE users DROP COLUMN first_name;
373
- -- ALTER TABLE users DROP COLUMN last_name;
184
+ Maintain a migration history table (or equivalent) that records:
185
+ - Migration version/ID
186
+ - Migration name/description
187
+ - Execution timestamp
188
+ - Duration
189
+ - Checksum (to detect tampering)
190
+ - Status (applied, rolled back, failed)
374
191
 
375
- COMMIT;
376
- ```
192
+ ---
377
193
 
378
194
  ## Resources
379
195
 
380
- - [Flyway - Java Migration Tool](https://flywaydb.org/)
381
- - [Liquibase - Database Changelog](https://www.liquibase.org/)
382
- - [Alembic - Python Migration](https://alembic.sqlalchemy.org/)
383
- - [PostgreSQL ALTER TABLE](https://www.postgresql.org/docs/current/sql-altertable.html)
384
- - [MySQL ALTER TABLE](https://dev.mysql.com/doc/refman/8.0/en/alter-table.html)
196
+ - [Flyway](https://flywaydb.org/) — Java/JVM migration tool
197
+ - [Liquibase](https://www.liquibase.org/) — Database changelog management
198
+ - [Alembic](https://alembic.sqlalchemy.org/) — Python/SQLAlchemy migrations
199
+ - [Knex.js](https://knexjs.org/) — Node.js query builder with migrations
200
+ - [golang-migrate](https://github.com/golang-migrate/migrate) — Go migration tool