sqlew 3.6.2 → 3.6.3

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.
@@ -1,293 +1,293 @@
1
- # Migration Chain Documentation
2
-
3
- > **⚠️ HISTORICAL DOCUMENT (v1.0.0 - v2.1.0)**
4
- >
5
- > This document describes the **old custom migration system** used before v3.6.2.
6
- >
7
- > **As of v3.6.2**, sqlew uses **Knex.js migrations only**. See the migration files in:
8
- > - `src/migrations/knex/upgrades/` - Version upgrade paths
9
- > - `src/migrations/knex/bootstrap/` - Fresh install schema
10
- > - `src/migrations/knex/enhancements/` - Feature additions
11
- >
12
- > This document is kept for historical reference only.
13
-
14
- ---
15
-
16
- This document describes the complete migration path from v1.0.0 to v2.1.0 for mcp-sqlew databases.
17
-
18
- ## Schema Version History
19
-
20
- ### v1.0.0 (Initial Release)
21
- - **Table Names:** No prefixes
22
- - Master: `agents`, `files`, `context_keys`, `constraint_categories`, `layers`, `tags`, `scopes`
23
- - Transaction: `decisions`, `decisions_numeric`, `decision_history`, `decision_tags`, `decision_scopes`, `agent_messages`, `file_changes`, `constraints`, `constraint_tags`
24
- - Views: `tagged_decisions`, `active_context`, `layer_summary`, `unread_messages_by_priority`, `recent_file_changes`, `tagged_constraints`
25
- - Triggers: `auto_delete_old_messages`, `auto_delete_old_file_changes`, `record_decision_history`
26
- - **Configuration:** No `config` table (hardcoded settings)
27
- - **Comments:** Japanese
28
-
29
- ### v1.1.0 - v1.1.2 & v2.0.0 (Table Prefixes)
30
- - **Table Names:** WITH prefixes
31
- - Master: `m_agents`, `m_files`, `m_context_keys`, `m_constraint_categories`, `m_layers`, `m_tags`, `m_scopes`, **`m_config` (NEW)**
32
- - Transaction: `t_decisions`, `t_decisions_numeric`, `t_decision_history`, `t_decision_tags`, `t_decision_scopes`, `t_agent_messages`, `t_file_changes`, `t_constraints`, `t_constraint_tags`
33
- - Views: `v_tagged_decisions`, `v_active_context`, `v_layer_summary`, `v_unread_messages_by_priority`, `v_recent_file_changes`, `v_tagged_constraints`
34
- - Triggers: `trg_record_decision_history` (auto-delete triggers removed)
35
- - **Configuration:** `m_config` table added with weekend-aware auto-deletion settings
36
- - **Comments:** Japanese
37
- - **Notes:**
38
- - v1.1.1: Hotfix for migration failure (creates m_config if missing)
39
- - v1.1.2: Hotfix for schema validation (checks for prefixed names)
40
- - v2.0.0: API changes only (20 tools → 6 action-based tools), **no schema changes**
41
-
42
- ### v2.1.0 (Feature Release - Current)
43
- - **New Tables:**
44
- - `t_activity_log` - Automatic activity logging (agent_id, action_type, target, layer_id, details, ts)
45
- - `t_decision_templates` - Decision templates (name, defaults, required_fields, created_by, ts)
46
- - **New Triggers:**
47
- - `trg_log_decision_set` - Auto-log decision creation
48
- - `trg_log_decision_update` - Auto-log decision updates
49
- - `trg_log_message_send` - Auto-log message sending
50
- - `trg_log_file_record` - Auto-log file changes
51
- - **New Indexes:**
52
- - `idx_activity_log_ts` - Time-based activity queries
53
- - `idx_activity_log_agent` - Agent-based activity queries
54
- - `idx_activity_log_action` - Action type filtering
55
- - **Comments:** English (translated from Japanese)
56
- - **Built-in Data:** 5 decision templates seeded
57
-
58
- ## Migration Scripts
59
-
60
- ### 1. v1.0.0 → v1.1.x: add-table-prefixes.ts
61
- **Purpose:** Rename all tables to add category-based prefixes and create m_config table
62
-
63
- **Actions:**
64
- 1. Drop old views (they reference old table names)
65
- 2. Drop old triggers
66
- 3. Rename master tables (agents → m_agents, etc.)
67
- 4. Rename transaction tables (decisions → t_decisions, etc.)
68
- 5. Create m_config table if missing
69
- 6. Insert default config values
70
- 7. Schema initialization creates new views/triggers
71
-
72
- **Transaction:** Yes (rollback on failure)
73
- **Auto-detect:** Checks for existence of `agents` (old) and absence of `m_agents` (new)
74
-
75
- ### 2. v1.1.x / v2.0.0 → v2.1.0: add-v2.1.0-features.ts
76
- **Purpose:** Add activity logging and template system
77
-
78
- **Actions:**
79
- 1. Create `t_activity_log` table with 3 indexes
80
- 2. Create `t_decision_templates` table
81
- 3. Create 4 activity logging triggers
82
- 4. Seed 5 built-in templates
83
-
84
- **Transaction:** Yes (rollback on failure)
85
- **Auto-detect:** Checks for absence of `t_activity_log` OR `t_decision_templates`
86
-
87
- ## Migration Execution Order
88
-
89
- The migrations run automatically on server startup in this order:
90
-
91
- ```
92
- 1. Check v1.0.0 → v1.1.x migration (add-table-prefixes.ts)
93
- ├─ If needed: Run migration
94
- ├─ Run schema initialization (creates views/triggers)
95
- └─ Continue to next check
96
-
97
- 2. Check v1.1.x/v2.0.0 → v2.1.0 migration (add-v2.1.0-features.ts)
98
- ├─ If needed: Run migration
99
- └─ Continue
100
-
101
- 3. Validate schema integrity
102
- ├─ Check all required tables exist
103
- ├─ Check all required views exist
104
- ├─ Check all required triggers exist
105
- ├─ Verify standard data (5 layers, 3 categories, 10+ tags)
106
- └─ Exit with error if validation fails
107
-
108
- 4. Start MCP server
109
- ```
110
-
111
- ## Complete Migration Paths
112
-
113
- ### Path 1: v1.0.0 → v2.1.0 (Full Chain)
114
- ```
115
- v1.0.0 (no prefixes, no config)
116
- ↓ add-table-prefixes.ts
117
- v1.1.0 (prefixes, m_config added)
118
- ↓ add-v2.1.0-features.ts
119
- v2.1.0 (activity_log, templates added)
120
- ```
121
-
122
- **Steps:**
123
- 1. Start server with v1.0.0 database
124
- 2. Detects unprefixed tables → runs add-table-prefixes.ts
125
- 3. Migration adds prefixes and m_config table
126
- 4. Schema initialization creates new views/triggers
127
- 5. Detects missing v2.1.0 tables → runs add-v2.1.0-features.ts
128
- 6. Migration adds t_activity_log, t_decision_templates, 4 triggers
129
- 7. Schema validation passes
130
- 8. Server starts with v2.1.0 schema
131
-
132
- ### Path 2: v1.1.x / v2.0.0 → v2.1.0 (Single Migration)
133
- ```
134
- v1.1.0-v1.1.2 or v2.0.0 (prefixes, m_config present)
135
- ↓ add-v2.1.0-features.ts
136
- v2.1.0 (activity_log, templates added)
137
- ```
138
-
139
- **Steps:**
140
- 1. Start server with v1.1.x or v2.0.0 database
141
- 2. Detects prefixed tables exist → skips add-table-prefixes.ts
142
- 3. Detects missing v2.1.0 tables → runs add-v2.1.0-features.ts
143
- 4. Migration adds t_activity_log, t_decision_templates, 4 triggers
144
- 5. Schema validation passes
145
- 6. Server starts with v2.1.0 schema
146
-
147
- ### Path 3: Fresh Install → v2.1.0 (No Migration)
148
- ```
149
- Empty database
150
- ↓ schema initialization
151
- v2.1.0 (full schema created)
152
- ```
153
-
154
- **Steps:**
155
- 1. Start server with empty/new database path
156
- 2. No existing schema detected
157
- 3. Schema initialization runs (executes assets/schema.sql)
158
- 4. All tables, views, triggers created directly from v2.1.0 schema
159
- 5. Schema validation passes
160
- 6. Server starts with v2.1.0 schema
161
-
162
- ## Schema Validation (v2.1.0 Requirements)
163
-
164
- The schema validator (`src/schema.ts`) checks for:
165
-
166
- **Tables (19 total):**
167
- - Master (8): m_agents, m_files, m_context_keys, m_constraint_categories, m_layers, m_tags, m_scopes, m_config
168
- - Transaction (11): t_decisions, t_decisions_numeric, t_decision_history, t_decision_tags, t_decision_scopes, t_agent_messages, t_file_changes, t_constraints, t_constraint_tags, **t_activity_log**, **t_decision_templates**
169
-
170
- **Views (6 total):**
171
- - v_tagged_decisions, v_active_context, v_layer_summary, v_unread_messages_by_priority, v_recent_file_changes, v_tagged_constraints
172
-
173
- **Triggers (5 total):**
174
- - trg_record_decision_history
175
- - **trg_log_decision_set** (NEW in v2.1.0)
176
- - **trg_log_decision_update** (NEW in v2.1.0)
177
- - **trg_log_message_send** (NEW in v2.1.0)
178
- - **trg_log_file_record** (NEW in v2.1.0)
179
-
180
- **Standard Data:**
181
- - 5 layers (presentation, business, data, infrastructure, cross-cutting)
182
- - 3 constraint categories (performance, architecture, security)
183
- - 10+ tags (authentication, authorization, performance, security, api, database, caching, testing, validation, error-handling)
184
- - 3+ config entries (autodelete_ignore_weekend, autodelete_message_hours, autodelete_file_history_days)
185
-
186
- ## Testing Migration Chain
187
-
188
- ### Test 1: v1.0.0 → v2.1.0
189
- ```bash
190
- # Create v1.0.0 database (use git checkout)
191
- git checkout v1.0.0
192
- node dist/index.js --db-path=.test/v1.0.0.db
193
- # Let it initialize with v1.0.0 schema
194
- # Stop server (Ctrl+C)
195
-
196
- # Upgrade to v2.1.0
197
- git checkout main
198
- npm run build
199
- node dist/index.js --db-path=.test/v1.0.0.db
200
- # Should run BOTH migrations automatically
201
- # Check logs for:
202
- # "Migration required: Adding table prefixes (v1.2.0 -> v1.3.0)"
203
- # "Migration required: Adding v2.1.0 features (v2.0.0 -> v2.1.0)"
204
- ```
205
-
206
- ### Test 2: v2.0.0 → v2.1.0
207
- ```bash
208
- # Create v2.0.0 database
209
- git checkout v2.0.0
210
- npm run build
211
- node dist/index.js --db-path=.test/v2.0.0.db
212
- # Stop server
213
-
214
- # Upgrade to v2.1.0
215
- git checkout main
216
- npm run build
217
- node dist/index.js --db-path=.test/v2.0.0.db
218
- # Should run ONLY v2.1.0 migration
219
- # Check logs for:
220
- # "Migration required: Adding v2.1.0 features (v2.0.0 -> v2.1.0)"
221
- ```
222
-
223
- ### Test 3: Fresh v2.1.0
224
- ```bash
225
- # Fresh install
226
- node dist/index.js --db-path=.test/fresh-v2.1.0.db
227
- # Should initialize directly to v2.1.0
228
- # No migration messages
229
- ```
230
-
231
- ## Rollback Strategy
232
-
233
- If a migration fails:
234
- 1. **Transaction rollback** - All changes reverted automatically
235
- 2. **Database unchanged** - Original schema preserved
236
- 3. **Server exits** - Prevents running with corrupted schema
237
- 4. **Error message displayed** - Clear indication of failure
238
-
239
- To manually rollback:
240
- ```bash
241
- # Restore from backup (recommended)
242
- cp backup.db current.db
243
-
244
- # Or delete and reinitialize
245
- rm .sqlew/sqlew.db
246
- node dist/index.js # Creates fresh v2.1.0 database
247
- ```
248
-
249
- ## Troubleshooting
250
-
251
- ### Migration Doesn't Run
252
- - Check that database file exists and is accessible
253
- - Verify SQLite version supports required features
254
- - Check file permissions
255
-
256
- ### Migration Fails Midway
257
- - Check error message in console
258
- - Database is automatically rolled back
259
- - No partial state - safe to retry
260
-
261
- ### Schema Validation Fails After Migration
262
- - Report as bug (migration should guarantee valid schema)
263
- - Check logs for missing components
264
- - May need to restore from backup or delete database
265
-
266
- ## Future Migrations
267
-
268
- When adding new features in future versions:
269
-
270
- 1. Create new migration script: `src/migrations/add-vX.Y.Z-features.ts`
271
- 2. Implement `needsMigration()`, `runMigration()`, `getMigrationInfo()`
272
- 3. Import in `src/database.ts`
273
- 4. Add check AFTER previous migrations, BEFORE schema validation
274
- 5. Update `src/schema.ts` validation requirements
275
- 6. Update `assets/schema.sql` with new schema
276
- 7. Document in CHANGELOG.md and this file
277
- 8. Test complete migration chain from oldest supported version
278
-
279
- ## Backward Compatibility
280
-
281
- **Database:** v2.1.0 can read and migrate v1.0.0, v1.1.x, and v2.0.0 databases
282
- **API:** v2.0.0 tool calls work unchanged in v2.1.0 (backward compatible)
283
- **Forward Compatibility:** Older versions CANNOT read v2.1.0 databases (missing tables/triggers)
284
-
285
- ## Summary
286
-
287
- - ✅ v1.0.0 → v2.1.0: Full migration chain supported
288
- - ✅ v1.1.x → v2.1.0: Single migration
289
- - ✅ v2.0.0 → v2.1.0: Single migration
290
- - ✅ Fresh install: Direct v2.1.0 schema
291
- - ✅ Transaction-based: Safe rollback on failure
292
- - ✅ Auto-detect: Runs only needed migrations
293
- - ✅ Schema validation: Ensures integrity
1
+ # Migration Chain Documentation
2
+
3
+ > **⚠️ HISTORICAL DOCUMENT (v1.0.0 - v2.1.0)**
4
+ >
5
+ > This document describes the **old custom migration system** used before v3.6.2.
6
+ >
7
+ > **As of v3.6.2**, sqlew uses **Knex.js migrations only**. See the migration files in:
8
+ > - `src/migrations/knex/upgrades/` - Version upgrade paths
9
+ > - `src/migrations/knex/bootstrap/` - Fresh install schema
10
+ > - `src/migrations/knex/enhancements/` - Feature additions
11
+ >
12
+ > This document is kept for historical reference only.
13
+
14
+ ---
15
+
16
+ This document describes the complete migration path from v1.0.0 to v2.1.0 for mcp-sqlew databases.
17
+
18
+ ## Schema Version History
19
+
20
+ ### v1.0.0 (Initial Release)
21
+ - **Table Names:** No prefixes
22
+ - Master: `agents`, `files`, `context_keys`, `constraint_categories`, `layers`, `tags`, `scopes`
23
+ - Transaction: `decisions`, `decisions_numeric`, `decision_history`, `decision_tags`, `decision_scopes`, `agent_messages`, `file_changes`, `constraints`, `constraint_tags`
24
+ - Views: `tagged_decisions`, `active_context`, `layer_summary`, `unread_messages_by_priority`, `recent_file_changes`, `tagged_constraints`
25
+ - Triggers: `auto_delete_old_messages`, `auto_delete_old_file_changes`, `record_decision_history`
26
+ - **Configuration:** No `config` table (hardcoded settings)
27
+ - **Comments:** Japanese
28
+
29
+ ### v1.1.0 - v1.1.2 & v2.0.0 (Table Prefixes)
30
+ - **Table Names:** WITH prefixes
31
+ - Master: `m_agents`, `m_files`, `m_context_keys`, `m_constraint_categories`, `m_layers`, `m_tags`, `m_scopes`, **`m_config` (NEW)**
32
+ - Transaction: `t_decisions`, `t_decisions_numeric`, `t_decision_history`, `t_decision_tags`, `t_decision_scopes`, `t_agent_messages`, `t_file_changes`, `t_constraints`, `t_constraint_tags`
33
+ - Views: `v_tagged_decisions`, `v_active_context`, `v_layer_summary`, `v_unread_messages_by_priority`, `v_recent_file_changes`, `v_tagged_constraints`
34
+ - Triggers: `trg_record_decision_history` (auto-delete triggers removed)
35
+ - **Configuration:** `m_config` table added with weekend-aware auto-deletion settings
36
+ - **Comments:** Japanese
37
+ - **Notes:**
38
+ - v1.1.1: Hotfix for migration failure (creates m_config if missing)
39
+ - v1.1.2: Hotfix for schema validation (checks for prefixed names)
40
+ - v2.0.0: API changes only (20 tools → 6 action-based tools), **no schema changes**
41
+
42
+ ### v2.1.0 (Feature Release - Current)
43
+ - **New Tables:**
44
+ - `t_activity_log` - Automatic activity logging (agent_id, action_type, target, layer_id, details, ts)
45
+ - `t_decision_templates` - Decision templates (name, defaults, required_fields, created_by, ts)
46
+ - **New Triggers:**
47
+ - `trg_log_decision_set` - Auto-log decision creation
48
+ - `trg_log_decision_update` - Auto-log decision updates
49
+ - `trg_log_message_send` - Auto-log message sending
50
+ - `trg_log_file_record` - Auto-log file changes
51
+ - **New Indexes:**
52
+ - `idx_activity_log_ts` - Time-based activity queries
53
+ - `idx_activity_log_agent` - Agent-based activity queries
54
+ - `idx_activity_log_action` - Action type filtering
55
+ - **Comments:** English (translated from Japanese)
56
+ - **Built-in Data:** 5 decision templates seeded
57
+
58
+ ## Migration Scripts
59
+
60
+ ### 1. v1.0.0 → v1.1.x: add-table-prefixes.ts
61
+ **Purpose:** Rename all tables to add category-based prefixes and create m_config table
62
+
63
+ **Actions:**
64
+ 1. Drop old views (they reference old table names)
65
+ 2. Drop old triggers
66
+ 3. Rename master tables (agents → m_agents, etc.)
67
+ 4. Rename transaction tables (decisions → t_decisions, etc.)
68
+ 5. Create m_config table if missing
69
+ 6. Insert default config values
70
+ 7. Schema initialization creates new views/triggers
71
+
72
+ **Transaction:** Yes (rollback on failure)
73
+ **Auto-detect:** Checks for existence of `agents` (old) and absence of `m_agents` (new)
74
+
75
+ ### 2. v1.1.x / v2.0.0 → v2.1.0: add-v2.1.0-features.ts
76
+ **Purpose:** Add activity logging and template system
77
+
78
+ **Actions:**
79
+ 1. Create `t_activity_log` table with 3 indexes
80
+ 2. Create `t_decision_templates` table
81
+ 3. Create 4 activity logging triggers
82
+ 4. Seed 5 built-in templates
83
+
84
+ **Transaction:** Yes (rollback on failure)
85
+ **Auto-detect:** Checks for absence of `t_activity_log` OR `t_decision_templates`
86
+
87
+ ## Migration Execution Order
88
+
89
+ The migrations run automatically on server startup in this order:
90
+
91
+ ```
92
+ 1. Check v1.0.0 → v1.1.x migration (add-table-prefixes.ts)
93
+ ├─ If needed: Run migration
94
+ ├─ Run schema initialization (creates views/triggers)
95
+ └─ Continue to next check
96
+
97
+ 2. Check v1.1.x/v2.0.0 → v2.1.0 migration (add-v2.1.0-features.ts)
98
+ ├─ If needed: Run migration
99
+ └─ Continue
100
+
101
+ 3. Validate schema integrity
102
+ ├─ Check all required tables exist
103
+ ├─ Check all required views exist
104
+ ├─ Check all required triggers exist
105
+ ├─ Verify standard data (5 layers, 3 categories, 10+ tags)
106
+ └─ Exit with error if validation fails
107
+
108
+ 4. Start MCP server
109
+ ```
110
+
111
+ ## Complete Migration Paths
112
+
113
+ ### Path 1: v1.0.0 → v2.1.0 (Full Chain)
114
+ ```
115
+ v1.0.0 (no prefixes, no config)
116
+ ↓ add-table-prefixes.ts
117
+ v1.1.0 (prefixes, m_config added)
118
+ ↓ add-v2.1.0-features.ts
119
+ v2.1.0 (activity_log, templates added)
120
+ ```
121
+
122
+ **Steps:**
123
+ 1. Start server with v1.0.0 database
124
+ 2. Detects unprefixed tables → runs add-table-prefixes.ts
125
+ 3. Migration adds prefixes and m_config table
126
+ 4. Schema initialization creates new views/triggers
127
+ 5. Detects missing v2.1.0 tables → runs add-v2.1.0-features.ts
128
+ 6. Migration adds t_activity_log, t_decision_templates, 4 triggers
129
+ 7. Schema validation passes
130
+ 8. Server starts with v2.1.0 schema
131
+
132
+ ### Path 2: v1.1.x / v2.0.0 → v2.1.0 (Single Migration)
133
+ ```
134
+ v1.1.0-v1.1.2 or v2.0.0 (prefixes, m_config present)
135
+ ↓ add-v2.1.0-features.ts
136
+ v2.1.0 (activity_log, templates added)
137
+ ```
138
+
139
+ **Steps:**
140
+ 1. Start server with v1.1.x or v2.0.0 database
141
+ 2. Detects prefixed tables exist → skips add-table-prefixes.ts
142
+ 3. Detects missing v2.1.0 tables → runs add-v2.1.0-features.ts
143
+ 4. Migration adds t_activity_log, t_decision_templates, 4 triggers
144
+ 5. Schema validation passes
145
+ 6. Server starts with v2.1.0 schema
146
+
147
+ ### Path 3: Fresh Install → v2.1.0 (No Migration)
148
+ ```
149
+ Empty database
150
+ ↓ schema initialization
151
+ v2.1.0 (full schema created)
152
+ ```
153
+
154
+ **Steps:**
155
+ 1. Start server with empty/new database path
156
+ 2. No existing schema detected
157
+ 3. Schema initialization runs (executes assets/schema.sql)
158
+ 4. All tables, views, triggers created directly from v2.1.0 schema
159
+ 5. Schema validation passes
160
+ 6. Server starts with v2.1.0 schema
161
+
162
+ ## Schema Validation (v2.1.0 Requirements)
163
+
164
+ The schema validator (`src/schema.ts`) checks for:
165
+
166
+ **Tables (19 total):**
167
+ - Master (8): m_agents, m_files, m_context_keys, m_constraint_categories, m_layers, m_tags, m_scopes, m_config
168
+ - Transaction (11): t_decisions, t_decisions_numeric, t_decision_history, t_decision_tags, t_decision_scopes, t_agent_messages, t_file_changes, t_constraints, t_constraint_tags, **t_activity_log**, **t_decision_templates**
169
+
170
+ **Views (6 total):**
171
+ - v_tagged_decisions, v_active_context, v_layer_summary, v_unread_messages_by_priority, v_recent_file_changes, v_tagged_constraints
172
+
173
+ **Triggers (5 total):**
174
+ - trg_record_decision_history
175
+ - **trg_log_decision_set** (NEW in v2.1.0)
176
+ - **trg_log_decision_update** (NEW in v2.1.0)
177
+ - **trg_log_message_send** (NEW in v2.1.0)
178
+ - **trg_log_file_record** (NEW in v2.1.0)
179
+
180
+ **Standard Data:**
181
+ - 5 layers (presentation, business, data, infrastructure, cross-cutting)
182
+ - 3 constraint categories (performance, architecture, security)
183
+ - 10+ tags (authentication, authorization, performance, security, api, database, caching, testing, validation, error-handling)
184
+ - 3+ config entries (autodelete_ignore_weekend, autodelete_message_hours, autodelete_file_history_days)
185
+
186
+ ## Testing Migration Chain
187
+
188
+ ### Test 1: v1.0.0 → v2.1.0
189
+ ```bash
190
+ # Create v1.0.0 database (use git checkout)
191
+ git checkout v1.0.0
192
+ node dist/index.js --db-path=.test/v1.0.0.db
193
+ # Let it initialize with v1.0.0 schema
194
+ # Stop server (Ctrl+C)
195
+
196
+ # Upgrade to v2.1.0
197
+ git checkout main
198
+ npm run build
199
+ node dist/index.js --db-path=.test/v1.0.0.db
200
+ # Should run BOTH migrations automatically
201
+ # Check logs for:
202
+ # "Migration required: Adding table prefixes (v1.2.0 -> v1.3.0)"
203
+ # "Migration required: Adding v2.1.0 features (v2.0.0 -> v2.1.0)"
204
+ ```
205
+
206
+ ### Test 2: v2.0.0 → v2.1.0
207
+ ```bash
208
+ # Create v2.0.0 database
209
+ git checkout v2.0.0
210
+ npm run build
211
+ node dist/index.js --db-path=.test/v2.0.0.db
212
+ # Stop server
213
+
214
+ # Upgrade to v2.1.0
215
+ git checkout main
216
+ npm run build
217
+ node dist/index.js --db-path=.test/v2.0.0.db
218
+ # Should run ONLY v2.1.0 migration
219
+ # Check logs for:
220
+ # "Migration required: Adding v2.1.0 features (v2.0.0 -> v2.1.0)"
221
+ ```
222
+
223
+ ### Test 3: Fresh v2.1.0
224
+ ```bash
225
+ # Fresh install
226
+ node dist/index.js --db-path=.test/fresh-v2.1.0.db
227
+ # Should initialize directly to v2.1.0
228
+ # No migration messages
229
+ ```
230
+
231
+ ## Rollback Strategy
232
+
233
+ If a migration fails:
234
+ 1. **Transaction rollback** - All changes reverted automatically
235
+ 2. **Database unchanged** - Original schema preserved
236
+ 3. **Server exits** - Prevents running with corrupted schema
237
+ 4. **Error message displayed** - Clear indication of failure
238
+
239
+ To manually rollback:
240
+ ```bash
241
+ # Restore from backup (recommended)
242
+ cp backup.db current.db
243
+
244
+ # Or delete and reinitialize
245
+ rm .sqlew/sqlew.db
246
+ node dist/index.js # Creates fresh v2.1.0 database
247
+ ```
248
+
249
+ ## Troubleshooting
250
+
251
+ ### Migration Doesn't Run
252
+ - Check that database file exists and is accessible
253
+ - Verify SQLite version supports required features
254
+ - Check file permissions
255
+
256
+ ### Migration Fails Midway
257
+ - Check error message in console
258
+ - Database is automatically rolled back
259
+ - No partial state - safe to retry
260
+
261
+ ### Schema Validation Fails After Migration
262
+ - Report as bug (migration should guarantee valid schema)
263
+ - Check logs for missing components
264
+ - May need to restore from backup or delete database
265
+
266
+ ## Future Migrations
267
+
268
+ When adding new features in future versions:
269
+
270
+ 1. Create new migration script: `src/migrations/add-vX.Y.Z-features.ts`
271
+ 2. Implement `needsMigration()`, `runMigration()`, `getMigrationInfo()`
272
+ 3. Import in `src/database.ts`
273
+ 4. Add check AFTER previous migrations, BEFORE schema validation
274
+ 5. Update `src/schema.ts` validation requirements
275
+ 6. Update `assets/schema.sql` with new schema
276
+ 7. Document in CHANGELOG.md and this file
277
+ 8. Test complete migration chain from oldest supported version
278
+
279
+ ## Backward Compatibility
280
+
281
+ **Database:** v2.1.0 can read and migrate v1.0.0, v1.1.x, and v2.0.0 databases
282
+ **API:** v2.0.0 tool calls work unchanged in v2.1.0 (backward compatible)
283
+ **Forward Compatibility:** Older versions CANNOT read v2.1.0 databases (missing tables/triggers)
284
+
285
+ ## Summary
286
+
287
+ - ✅ v1.0.0 → v2.1.0: Full migration chain supported
288
+ - ✅ v1.1.x → v2.1.0: Single migration
289
+ - ✅ v2.0.0 → v2.1.0: Single migration
290
+ - ✅ Fresh install: Direct v2.1.0 schema
291
+ - ✅ Transaction-based: Safe rollback on failure
292
+ - ✅ Auto-detect: Runs only needed migrations
293
+ - ✅ Schema validation: Ensures integrity