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.
- package/README.md +14 -0
- package/package.json +1 -1
- package/template/.agent/instructions/commands.md +8 -32
- package/template/.agent/instructions/example.md +21 -0
- package/template/.agent/instructions/patterns.md +3 -3
- package/template/.agent/instructions/tech-stack.md +71 -23
- package/template/.agent/instructions/workflow.md +12 -1
- package/template/.agent/rules/completion-checklist.md +6 -0
- package/template/.agent/rules/security-first.md +3 -3
- package/template/.agent/rules/vertical-slices.md +1 -1
- package/template/.agent/skill-library/MANIFEST.md +6 -0
- package/template/.agent/skill-library/stack/devops/git-advanced/SKILL.md +972 -0
- package/template/.agent/skill-library/stack/devops/git-workflow/SKILL.md +420 -0
- package/template/.agent/skills/api-versioning/SKILL.md +44 -298
- package/template/.agent/skills/api-versioning/references/typescript.md +157 -0
- package/template/.agent/skills/architecture-mapping/SKILL.md +13 -13
- package/template/.agent/skills/bootstrap-agents/SKILL.md +151 -152
- package/template/.agent/skills/clean-code/SKILL.md +64 -118
- package/template/.agent/skills/clean-code/references/typescript.md +126 -0
- package/template/.agent/skills/database-schema-design/SKILL.md +93 -317
- package/template/.agent/skills/database-schema-design/references/relational.md +228 -0
- package/template/.agent/skills/error-handling-patterns/SKILL.md +62 -557
- package/template/.agent/skills/error-handling-patterns/references/go.md +162 -0
- package/template/.agent/skills/error-handling-patterns/references/python.md +262 -0
- package/template/.agent/skills/error-handling-patterns/references/rust.md +112 -0
- package/template/.agent/skills/error-handling-patterns/references/typescript.md +178 -0
- package/template/.agent/skills/idea-extraction/SKILL.md +322 -224
- package/template/.agent/skills/logging-best-practices/SKILL.md +108 -767
- package/template/.agent/skills/logging-best-practices/references/go.md +49 -0
- package/template/.agent/skills/logging-best-practices/references/python.md +52 -0
- package/template/.agent/skills/logging-best-practices/references/typescript.md +215 -0
- package/template/.agent/skills/migration-management/SKILL.md +127 -311
- package/template/.agent/skills/migration-management/references/relational.md +214 -0
- package/template/.agent/skills/parallel-feature-development/SKILL.md +34 -43
- package/template/.agent/skills/pipeline-rubrics/references/be-rubric.md +1 -1
- package/template/.agent/skills/pipeline-rubrics/references/ia-rubric.md +2 -2
- package/template/.agent/skills/pipeline-rubrics/references/scoring.md +1 -1
- package/template/.agent/skills/pipeline-rubrics/references/vision-rubric.md +2 -1
- package/template/.agent/skills/prd-templates/SKILL.md +23 -6
- package/template/.agent/skills/prd-templates/references/be-spec-template.md +2 -2
- package/template/.agent/skills/prd-templates/references/decomposition-templates.md +2 -2
- package/template/.agent/skills/prd-templates/references/engineering-standards-template.md +2 -0
- package/template/.agent/skills/prd-templates/references/fe-spec-template.md +1 -1
- package/template/.agent/skills/prd-templates/references/fractal-cx-template.md +58 -0
- package/template/.agent/skills/prd-templates/references/fractal-feature-template.md +93 -0
- package/template/.agent/skills/prd-templates/references/fractal-node-index-template.md +55 -0
- package/template/.agent/skills/prd-templates/references/ideation-crosscut-template.md +26 -47
- package/template/.agent/skills/prd-templates/references/ideation-index-template.md +47 -31
- package/template/.agent/skills/prd-templates/references/operational-templates.md +1 -1
- package/template/.agent/skills/prd-templates/references/placeholder-workflow-mapping.md +50 -21
- package/template/.agent/skills/prd-templates/references/skill-loading-protocol.md +32 -0
- package/template/.agent/skills/prd-templates/references/slice-completion-gates.md +29 -0
- package/template/.agent/skills/prd-templates/references/spec-coverage-sweep.md +3 -3
- package/template/.agent/skills/prd-templates/references/tdd-testing-policy.md +39 -0
- package/template/.agent/skills/prd-templates/references/vision-template.md +8 -8
- package/template/.agent/skills/regex-patterns/SKILL.md +122 -540
- package/template/.agent/skills/regex-patterns/references/go.md +44 -0
- package/template/.agent/skills/regex-patterns/references/javascript.md +63 -0
- package/template/.agent/skills/regex-patterns/references/python.md +77 -0
- package/template/.agent/skills/regex-patterns/references/rust.md +43 -0
- package/template/.agent/skills/resolve-ambiguity/SKILL.md +1 -1
- package/template/.agent/skills/session-continuity/SKILL.md +11 -9
- package/template/.agent/skills/session-continuity/protocols/02-progress-generation.md +2 -2
- package/template/.agent/skills/session-continuity/protocols/04-pattern-extraction.md +1 -1
- package/template/.agent/skills/session-continuity/protocols/05-session-close.md +1 -1
- package/template/.agent/skills/session-continuity/protocols/09-parallel-claim.md +1 -1
- package/template/.agent/skills/session-continuity/protocols/10-placeholder-verification-gate.md +57 -78
- package/template/.agent/skills/session-continuity/protocols/11-parallel-synthesis.md +1 -1
- package/template/.agent/skills/spec-writing/SKILL.md +1 -1
- package/template/.agent/skills/tdd-workflow/SKILL.md +94 -317
- package/template/.agent/skills/tdd-workflow/references/typescript.md +231 -0
- package/template/.agent/skills/testing-strategist/SKILL.md +74 -687
- package/template/.agent/skills/testing-strategist/references/typescript.md +328 -0
- package/template/.agent/skills/workflow-automation/SKILL.md +62 -154
- package/template/.agent/skills/workflow-automation/references/inngest.md +88 -0
- package/template/.agent/skills/workflow-automation/references/temporal.md +64 -0
- package/template/.agent/workflows/bootstrap-agents-fill.md +85 -143
- package/template/.agent/workflows/bootstrap-agents-provision.md +90 -107
- package/template/.agent/workflows/create-prd-architecture.md +23 -16
- package/template/.agent/workflows/create-prd-compile.md +11 -12
- package/template/.agent/workflows/create-prd-design-system.md +1 -1
- package/template/.agent/workflows/create-prd-security.md +9 -11
- package/template/.agent/workflows/create-prd-stack.md +10 -4
- package/template/.agent/workflows/create-prd.md +9 -9
- package/template/.agent/workflows/decompose-architecture-structure.md +4 -6
- package/template/.agent/workflows/decompose-architecture-validate.md +18 -1
- package/template/.agent/workflows/decompose-architecture.md +18 -3
- package/template/.agent/workflows/evolve-contract.md +11 -11
- package/template/.agent/workflows/evolve-feature-classify.md +14 -6
- package/template/.agent/workflows/ideate-discover.md +72 -107
- package/template/.agent/workflows/ideate-extract.md +84 -63
- package/template/.agent/workflows/ideate-validate.md +26 -22
- package/template/.agent/workflows/ideate.md +9 -9
- package/template/.agent/workflows/implement-slice-setup.md +25 -23
- package/template/.agent/workflows/implement-slice-tdd.md +73 -89
- package/template/.agent/workflows/implement-slice.md +4 -4
- package/template/.agent/workflows/plan-phase-preflight.md +6 -2
- package/template/.agent/workflows/plan-phase-write.md +6 -8
- package/template/.agent/workflows/remediate-pipeline-assess.md +2 -1
- package/template/.agent/workflows/resolve-ambiguity.md +2 -2
- package/template/.agent/workflows/update-architecture-map.md +22 -5
- package/template/.agent/workflows/validate-phase-quality.md +155 -0
- package/template/.agent/workflows/validate-phase-readiness.md +167 -0
- package/template/.agent/workflows/validate-phase.md +19 -157
- package/template/.agent/workflows/verify-infrastructure.md +10 -10
- package/template/.agent/workflows/write-architecture-spec-design.md +23 -14
- package/template/.agent/workflows/write-be-spec-classify.md +25 -21
- package/template/.agent/workflows/write-be-spec.md +1 -1
- package/template/.agent/workflows/write-fe-spec-classify.md +6 -12
- package/template/.agent/workflows/write-fe-spec-write.md +1 -1
- package/template/AGENTS.md +6 -2
- package/template/GEMINI.md +5 -3
- package/template/docs/README.md +10 -10
- package/template/docs/kit-architecture.md +126 -33
- package/template/docs/plans/ideation/README.md +8 -3
- 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
|
|
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.
|
|
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
|
|
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
|
-
##
|
|
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
|
-
|
|
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
|
-
|
|
112
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
###
|
|
36
|
+
### 1. Bidirectional Migrations
|
|
143
37
|
|
|
144
|
-
|
|
38
|
+
Every migration must have both directions:
|
|
39
|
+
- **UP** — applies the change
|
|
40
|
+
- **DOWN** — reverses the change
|
|
145
41
|
|
|
146
|
-
|
|
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
|
-
|
|
150
|
-
CREATE INDEX CONCURRENTLY idx_orders_user_created
|
|
151
|
-
ON orders(user_id, created_at DESC);
|
|
44
|
+
### 2. Migration Ordering
|
|
152
45
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
158
|
-
SELECT schemaname, tablename, indexname, idx_scan
|
|
159
|
-
FROM pg_stat_user_indexes
|
|
160
|
-
WHERE indexname LIKE 'idx_%';
|
|
51
|
+
### 3. Idempotency
|
|
161
52
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
58
|
+
### 4. Atomic Migrations
|
|
168
59
|
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
173
|
-
ALTER TABLE orders
|
|
174
|
-
ADD INDEX idx_user_created (user_id, created_at),
|
|
175
|
-
ALGORITHM=INPLACE, LOCK=NONE;
|
|
65
|
+
---
|
|
176
66
|
|
|
177
|
-
|
|
178
|
-
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST
|
|
179
|
-
WHERE INFO LIKE 'ALTER TABLE%';
|
|
180
|
-
```
|
|
67
|
+
## Common Migration Patterns
|
|
181
68
|
|
|
182
|
-
###
|
|
69
|
+
### Safe Schema Changes
|
|
183
70
|
|
|
184
|
-
|
|
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
|
-
|
|
187
|
-
-- Migration: 20240115_004_normalize_email_addresses.sql
|
|
80
|
+
### Multi-Phase Migrations
|
|
188
81
|
|
|
189
|
-
|
|
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
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
203
|
-
```
|
|
88
|
+
This pattern prevents downtime and allows rollback at any phase.
|
|
204
89
|
|
|
205
|
-
|
|
90
|
+
### Data Transformation Migrations
|
|
206
91
|
|
|
207
|
-
|
|
208
|
-
-- Migration: 20240115_004_update_product_categories.sql
|
|
92
|
+
When transforming data (normalization, cleanup, format changes):
|
|
209
93
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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
|
-
|
|
217
|
-
SELECT COUNT(*) as updated_count
|
|
218
|
-
FROM products
|
|
219
|
-
WHERE category_id IS NOT NULL;
|
|
220
|
-
```
|
|
99
|
+
---
|
|
221
100
|
|
|
222
|
-
|
|
101
|
+
## Rollback Strategies
|
|
223
102
|
|
|
224
|
-
|
|
103
|
+
### Per-Migration Rollback
|
|
225
104
|
|
|
226
|
-
|
|
227
|
-
|
|
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
|
-
|
|
230
|
-
ALTER TABLE orders
|
|
231
|
-
ADD COLUMN status_updated_at TIMESTAMP;
|
|
111
|
+
### Point-in-Time Recovery
|
|
232
112
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
113
|
+
For data-destructive migrations:
|
|
114
|
+
- Require backup before execution
|
|
115
|
+
- Document the backup location
|
|
116
|
+
- Test restore procedure before migration
|
|
237
117
|
|
|
238
|
-
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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
|
-
|
|
268
|
-
ROLLBACK;
|
|
137
|
+
### Staging Validation
|
|
269
138
|
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
|
|
145
|
+
### Pre-Production Checklist
|
|
275
146
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
329
|
-
- Have rollback plan ready
|
|
330
|
-
-
|
|
331
|
-
-
|
|
332
|
-
-
|
|
333
|
-
-
|
|
334
|
-
-
|
|
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
|
-
|
|
172
|
+
### Post-Migration Verification
|
|
337
173
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
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
|
-
|
|
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
|
-
|
|
368
|
-
ALTER TABLE users
|
|
369
|
-
ADD CONSTRAINT email_unique UNIQUE(email);
|
|
182
|
+
## Migration Tracking
|
|
370
183
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
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
|
-
|
|
376
|
-
```
|
|
192
|
+
---
|
|
377
193
|
|
|
378
194
|
## Resources
|
|
379
195
|
|
|
380
|
-
- [Flyway
|
|
381
|
-
- [Liquibase
|
|
382
|
-
- [Alembic
|
|
383
|
-
- [
|
|
384
|
-
- [
|
|
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
|