sqlew 5.0.7 → 5.0.8

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,701 +0,0 @@
1
- # Data Export/Import
2
-
3
- > JSON-based project data migration for sharing and multi-database workflows
4
-
5
- ## 📢 v4.0.2 Update: JSON is Now Required for Cross-Database Migration
6
-
7
- Starting from v4.0.2, **JSON export/import is the ONLY supported method for cross-database migrations** (e.g., SQLite → MySQL, MySQL → PostgreSQL).
8
-
9
- SQL dump (`db:dump`) no longer supports cross-database format conversion. See [Complete Cross-Database Migration Guide](#complete-cross-database-migration-guide-v402) below for step-by-step instructions.
10
-
11
- ---
12
-
13
- ## Overview
14
-
15
- sqlew provides a complete JSON export/import system for migrating project data between databases. This is useful for:
16
-
17
- - **Project Sharing** - Share context with team members or between machines
18
- - **Database Migration** - Move projects to different sqlew databases (different machine, different DB type)
19
- - **Multi-Project Consolidation** - Merge multiple project exports into one database
20
-
21
- **⚠️ Important**: Import skips if project name already exists (default: `--skip-if-exists=true`). This is **NOT a
22
- backup/restore solution**.
23
-
24
- **For backup/restore, use `db:dump` instead**: See [Database Migration Guide](DATABASE_MIGRATION.md) for full backup
25
- solutions including schema + data using SQL dumps.
26
-
27
- ## Quick Start
28
-
29
- ### Export a Project
30
-
31
- ```bash
32
- # Export all projects (no installation required!)
33
- sqlew db:export backup.json
34
-
35
- # Export specific project to file
36
- sqlew db:export backup.json project=my-project
37
-
38
- # Export to stdout (pipe to another command)
39
- sqlew db:export project=visualizer
40
- ```
41
-
42
- ### Import a Project
43
-
44
- ```bash
45
- # Import from JSON export
46
- sqlew db:import backup.json
47
-
48
- # Import with custom project name
49
- sqlew db:import backup.json project-name=new-name
50
-
51
- # Dry-run validation (no actual import)
52
- sqlew db:import backup.json dry-run=true
53
- ```
54
-
55
- ## Export Command
56
-
57
- ### Syntax
58
-
59
- ```bash
60
- sqlew db:export [output-file] [key=value ...]
61
- ```
62
-
63
- ### Options
64
-
65
- | Option | Description | Default |
66
- | ---------------- | ------------------------------- | ----------------- |
67
- | `project=<name>` | Export specific project by name | All projects |
68
- | `db-path=<path>` | Database file path | `.sqlew/sqlew.db` |
69
- | `config=<path>` | Config file path | Auto-detect |
70
-
71
- ### What Gets Exported
72
-
73
- - **Master Tables** (filtered to used entries only):
74
- - Agents (m_agents)
75
- - Context keys (m_context_keys)
76
- - Files (m_files)
77
- - Tags (m_tags)
78
- - Scopes (m_scopes)
79
- - Layers (m_layers)
80
- - Project metadata (m_projects)
81
-
82
- - **Transaction Tables** (all data for selected project):
83
- - Decisions with context (t_decisions, t_decision_context)
84
- - Tasks with details (t_tasks, t_task_details)
85
- - File changes (t_file_changes)
86
- - Constraints (t_constraints)
87
-
88
- - **Junction Tables** (relationships):
89
- - Task tags, file links, decision links, dependencies
90
- - Decision tags, scopes
91
- - Constraint tags
92
-
93
- ## Import Command
94
-
95
- ### Syntax
96
-
97
- ```bash
98
- sqlew db:import <source-file> [key=value ...]
99
- ```
100
-
101
- ### Options
102
-
103
- | Option | Description | Default |
104
- | --------------------- | ----------------------------- | ------------------ |
105
- | `<source-file>` | JSON export file path | **Required** |
106
- | `project-name=<name>` | Target project name | Use name from JSON |
107
- | `skip-if-exists=true` | Skip import if project exists | `true` |
108
- | `dry-run=true` | Validate only, don't import | `false` |
109
- | `db-path=<path>` | Database file path | `.sqlew/sqlew.db` |
110
- | `config=<path>` | Config file path | Auto-detect |
111
-
112
- ### Import Process
113
-
114
- 1. **Validation** - Checks JSON format, required fields, data types
115
- 2. **Conflict Detection** - Checks if project name already exists
116
- 3. **Topological Sort** - Resolves task dependencies (circular detection)
117
- 4. **ID Remapping** - Creates new IDs for all imported data
118
- 5. **Master Table Merge** - Reuses existing agents/tags/files by name
119
- 6. **Transaction Import** - Imports with fresh IDs and translated foreign keys
120
- 7. **Junction Table Import** - Restores all relationships
121
- 8. **Validation** - Verifies all foreign key references
122
-
123
- ### Smart Features
124
-
125
- **ID Remapping**:
126
-
127
- - All imported data gets fresh auto-incremented IDs
128
- - Original IDs are mapped to new IDs during transaction
129
- - Foreign key references automatically updated
130
-
131
- **Master Table Deduplication**:
132
-
133
- - Agents, tags, scopes, files reused if they already exist (by name/path)
134
- - Prevents duplicate entries for common metadata
135
- - Project-scoped master tables (m_files, m_tags, m_scopes) isolated by project_id
136
-
137
- **Dependency Resolution**:
138
-
139
- - Task dependencies sorted topologically
140
- - Circular dependency detection prevents invalid imports
141
- - Dependencies always imported before dependents
142
-
143
- **Transaction Safety**:
144
-
145
- - All-or-nothing semantics (full rollback on any error)
146
- - Idempotent operations (safe to retry on failure)
147
- - Comprehensive error messages with validation details
148
-
149
- ## CLI Usage
150
-
151
- **No installation required!** The unified entry point allows direct use via npx:
152
-
153
- ```bash
154
- # Export data
155
- sqlew db:export backup.json
156
-
157
- # Import data
158
- sqlew db:import backup.json
159
-
160
- # SQL dump (same-database backup)
161
- sqlew db:dump sqlite backup.sql
162
- ```
163
-
164
- **Note**: Both MCP server mode and CLI commands use the same `sqlew` entry point. The first argument determines the mode:
165
-
166
- - `db:export`, `db:import`, `db:dump`, `query` → CLI mode
167
- - No argument or MCP-related args → MCP server mode
168
-
169
- ## Use Cases
170
-
171
- ### Multi-Project Single Database (Permission-Constrained Environments)
172
-
173
- **Scenario**: You work on multiple projects but don't have permissions to create separate MySQL databases. You want to
174
- consolidate all project contexts into one shared database.
175
-
176
- **Solution**: Use export/import to merge multiple project contexts:
177
-
178
- ```bash
179
- # Step 1: Export from each project's SQLite database
180
- cd ~/project-a
181
- sqlew db:export /tmp/project-a.json project=project-a
182
-
183
- cd ~/project-b
184
- sqlew db:export /tmp/project-b.json project=project-b
185
-
186
- cd ~/project-c
187
- sqlew db:export /tmp/project-c.json project=project-c
188
-
189
- # Step 2: Create shared database and import all projects
190
- cd ~/shared-database
191
-
192
- # Configure to use single MySQL database (edit .sqlew/config.toml)
193
- # [database]
194
- # type = "mysql"
195
- # host = "localhost"
196
- # port = 3306
197
- # user = "myuser"
198
- # password = "mypassword"
199
- # database = "shared_sqlew_db"
200
-
201
- sqlew db:import /tmp/project-a.json
202
- sqlew db:import /tmp/project-b.json
203
- sqlew db:import /tmp/project-c.json
204
-
205
- # Step 3: Configure each project to use shared database
206
- # In each project's .mcp.json:
207
- # {
208
- # "mcpServers": {
209
- # "sqlew": {
210
- # "command": "npx",
211
- # "args": ["sqlew", "--config-path=/path/to/shared-database/.sqlew/config.toml"]
212
- # }
213
- # }
214
- # }
215
- ```
216
-
217
- **Benefits**:
218
-
219
- - ✅ Single database for multiple projects (saves database quota)
220
- - ✅ Cross-project context visibility (search decisions across all projects)
221
- - ✅ Centralized backup and maintenance
222
- - ✅ Works with permission-constrained MySQL/PostgreSQL environments
223
-
224
- **Trade-offs**:
225
-
226
- - ⚠️ All projects share same database connection pool
227
- - ⚠️ Requires manual config path in each project's .mcp.json
228
- - ⚠️ Project isolation maintained via project_id, not separate databases
229
-
230
- ### Database Migration (Cross-Machine or Cross-Database)
231
-
232
- ```bash
233
- # Export from source database
234
- sqlew db:export main-export.json project=main
235
-
236
- # Import to different database (different machine or different database type)
237
- # This works because the project doesn't exist in the target database yet
238
- sqlew db:import main-export.json db-path=/path/to/new/database.db
239
- ```
240
-
241
- **Note**: Import skips if project name exists.
242
-
243
- **For backup/restore, use `db:dump` instead**:
244
-
245
- ```bash
246
- # Backup with SQL dump (preserves schema + data)
247
- sqlew db:dump sqlite backup-$(date +%Y%m%d).sql
248
-
249
- # Or simple SQLite file copy
250
- cp .sqlew/sqlew.db .sqlew/backup-$(date +%Y%m%d).db
251
- ```
252
-
253
- See `sqlew db:dump --help` for full backup options.
254
-
255
- ### Project Sharing
256
-
257
- ```bash
258
- # Developer A: Export project
259
- sqlew db:export feature-x.json project=feature-x
260
-
261
- # Developer B: Import project
262
- sqlew db:import feature-x.json
263
- ```
264
-
265
- ### Multi-Project Consolidation
266
-
267
- ```bash
268
- # Export from different databases
269
- sqlew db:export vis.json project=visualizer
270
- sqlew db:export api.json project=api
271
-
272
- # Import to single database
273
- sqlew db:import vis.json
274
- sqlew db:import api.json
275
- ```
276
-
277
- ### Cross-Database Migration
278
-
279
- ```bash
280
- # Export from SQLite
281
- sqlew db:export data.json db-path=.sqlew/sqlew.db
282
-
283
- # Import to MySQL (configure .sqlew/config.toml for MySQL first)
284
- sqlew db:import data.json
285
- ```
286
-
287
- ---
288
-
289
- ## Complete Cross-Database Migration Guide (v4.0.2+)
290
-
291
- This section provides step-by-step instructions for migrating your sqlew database between different database systems.
292
-
293
- ### Pre-Migration Checklist
294
-
295
- Before starting a migration, ensure you have:
296
-
297
- - [ ] **Backup your current database** (copy `.sqlew/sqlew.db` or use `db:dump`)
298
- - [ ] **Note your current sqlew version** (`npm list sqlew`)
299
- - [ ] **Target database is created and accessible**
300
- - [ ] **Database credentials are available**
301
- - [ ] **Required privileges**: SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP, REFERENCES
302
-
303
- ### Migration: SQLite → MySQL
304
-
305
- #### Step 1: Export from SQLite
306
-
307
- ```bash
308
- cd /path/to/your/project
309
-
310
- # Export all data to JSON
311
- sqlew db:export migration-backup.json
312
- ```
313
-
314
- #### Step 2: Prepare MySQL Database
315
-
316
- ```sql
317
- -- Connect to MySQL as admin
318
- mysql -u root -p
319
-
320
- -- Create database (UTF-8 required for proper text handling)
321
- CREATE DATABASE sqlew_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
322
-
323
- -- Create user with required privileges
324
- CREATE USER 'sqlew_user'@'localhost' IDENTIFIED BY 'your-secure-password';
325
- GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP, REFERENCES
326
- ON sqlew_db.* TO 'sqlew_user'@'localhost';
327
- FLUSH PRIVILEGES;
328
- ```
329
-
330
- #### Step 3: Configure config.toml for MySQL
331
-
332
- Create or edit `.sqlew/config.toml`:
333
-
334
- ```toml
335
- [database]
336
- type = "mysql"
337
-
338
- [database.connection]
339
- host = "localhost"
340
- port = 3306
341
- database = "sqlew_db"
342
-
343
- [database.auth]
344
- type = "direct"
345
- user = "sqlew_user"
346
- password = "your-secure-password"
347
-
348
- [project]
349
- name = "your-project-name"
350
- ```
351
-
352
- #### Step 4: Import to MySQL
353
-
354
- ```bash
355
- # Import data to MySQL (config.toml will be used automatically)
356
- sqlew db:import migration-backup.json
357
- ```
358
-
359
- #### Step 5: Verify Migration
360
-
361
- ```bash
362
- # Test MCP server connection
363
- sqlew --config-path=.sqlew/config.toml
364
-
365
- # Or test with MCP Inspector
366
- npx @modelcontextprotocol/inspector sqlew
367
- ```
368
-
369
- ---
370
-
371
- ### Migration: SQLite → PostgreSQL
372
-
373
- #### Step 1: Export from SQLite
374
-
375
- ```bash
376
- cd /path/to/your/project
377
-
378
- # Export all data to JSON
379
- sqlew db:export migration-backup.json
380
- ```
381
-
382
- #### Step 2: Prepare PostgreSQL Database
383
-
384
- ```sql
385
- -- Connect to PostgreSQL as admin
386
- psql -U postgres
387
-
388
- -- Create database
389
- CREATE DATABASE sqlew_db WITH ENCODING 'UTF8';
390
-
391
- -- Create user with required privileges
392
- CREATE USER sqlew_user WITH PASSWORD 'your-secure-password';
393
- GRANT ALL PRIVILEGES ON DATABASE sqlew_db TO sqlew_user;
394
-
395
- -- Connect to the new database and grant schema privileges
396
- \c sqlew_db
397
- GRANT ALL ON SCHEMA public TO sqlew_user;
398
- ```
399
-
400
- #### Step 3: Configure config.toml for PostgreSQL
401
-
402
- Create or edit `.sqlew/config.toml`:
403
-
404
- ```toml
405
- [database]
406
- type = "postgres"
407
-
408
- [database.connection]
409
- host = "localhost"
410
- port = 5432
411
- database = "sqlew_db"
412
-
413
- [database.auth]
414
- type = "direct"
415
- user = "sqlew_user"
416
- password = "your-secure-password"
417
-
418
- [project]
419
- name = "your-project-name"
420
- ```
421
-
422
- #### Step 4: Import to PostgreSQL
423
-
424
- ```bash
425
- # Import data to PostgreSQL
426
- sqlew db:import migration-backup.json
427
- ```
428
-
429
- #### Step 5: Verify Migration
430
-
431
- ```bash
432
- # Test MCP server connection
433
- sqlew --config-path=.sqlew/config.toml
434
- ```
435
-
436
- ---
437
-
438
- ### Migration: MySQL → PostgreSQL
439
-
440
- #### Step 1: Export from MySQL
441
-
442
- First, configure config.toml to connect to MySQL:
443
-
444
- ```toml
445
- [database]
446
- type = "mysql"
447
-
448
- [database.connection]
449
- host = "localhost"
450
- port = 3306
451
- database = "sqlew_db"
452
-
453
- [database.auth]
454
- type = "direct"
455
- user = "sqlew_user"
456
- password = "mysql-password"
457
- ```
458
-
459
- Then export:
460
-
461
- ```bash
462
- sqlew db:export migration-backup.json
463
- ```
464
-
465
- #### Step 2: Prepare PostgreSQL Database
466
-
467
- ```sql
468
- -- Connect to PostgreSQL as admin
469
- psql -U postgres
470
-
471
- -- Create database
472
- CREATE DATABASE sqlew_db WITH ENCODING 'UTF8';
473
-
474
- -- Create user
475
- CREATE USER sqlew_user WITH PASSWORD 'postgres-password';
476
- GRANT ALL PRIVILEGES ON DATABASE sqlew_db TO sqlew_user;
477
- \c sqlew_db
478
- GRANT ALL ON SCHEMA public TO sqlew_user;
479
- ```
480
-
481
- #### Step 3: Update config.toml for PostgreSQL
482
-
483
- ```toml
484
- [database]
485
- type = "postgres"
486
-
487
- [database.connection]
488
- host = "localhost"
489
- port = 5432
490
- database = "sqlew_db"
491
-
492
- [database.auth]
493
- type = "direct"
494
- user = "sqlew_user"
495
- password = "postgres-password"
496
-
497
- [project]
498
- name = "your-project-name"
499
- ```
500
-
501
- #### Step 4: Import to PostgreSQL
502
-
503
- ```bash
504
- sqlew db:import migration-backup.json
505
- ```
506
-
507
- ---
508
-
509
- ### Post-Migration Verification
510
-
511
- After any migration, verify your data:
512
-
513
- #### 1. Check Row Counts
514
-
515
- Use your database client to compare row counts:
516
-
517
- ```sql
518
- -- Count decisions
519
- SELECT COUNT(*) FROM v4_decisions;
520
-
521
- -- Count tasks
522
- SELECT COUNT(*) FROM v4_tasks;
523
-
524
- -- Count file changes
525
- SELECT COUNT(*) FROM v4_file_changes;
526
- ```
527
-
528
- #### 2. Test MCP Server
529
-
530
- ```bash
531
- # Start MCP server with new config
532
- sqlew
533
-
534
- # Or use MCP Inspector for interactive testing
535
- npx @modelcontextprotocol/inspector sqlew
536
- ```
537
-
538
- #### 3. Verify in Claude Code
539
-
540
- Update your `.mcp.json` to use the new database:
541
-
542
- ```json
543
- {
544
- "mcpServers": {
545
- "sqlew": {
546
- "command": "npx",
547
- "args": ["sqlew", "--config-path", "/path/to/.sqlew/config.toml"]
548
- }
549
- }
550
- }
551
- ```
552
-
553
- ---
554
-
555
- ### Troubleshooting
556
-
557
- #### Connection Refused
558
-
559
- ```
560
- Error: connect ECONNREFUSED 127.0.0.1:3306
561
- ```
562
-
563
- **Solution**: Ensure the database server is running and accepting connections on the specified port.
564
-
565
- #### Authentication Failed
566
-
567
- ```
568
- Error: Access denied for user 'sqlew_user'@'localhost'
569
- ```
570
-
571
- **Solution**: Verify username and password in config.toml. Check that the user has proper privileges.
572
-
573
- #### Database Does Not Exist
574
-
575
- ```
576
- Error: Unknown database 'sqlew_db'
577
- ```
578
-
579
- **Solution**: Create the database first (see Step 2 in migration guides above).
580
-
581
- #### Permission Denied
582
-
583
- ```
584
- Error: permission denied for schema public
585
- ```
586
-
587
- **Solution**: Grant schema privileges to the user:
588
-
589
- ```sql
590
- -- PostgreSQL
591
- GRANT ALL ON SCHEMA public TO sqlew_user;
592
- ```
593
-
594
- #### Import Skipped (Project Exists)
595
-
596
- ```
597
- Project "my-project" already exists in target database
598
- ```
599
-
600
- **Solution**: Use `--project-name` to specify a different name, or manually delete the existing project from the target database.
601
-
602
- ---
603
-
604
- ## Error Handling
605
-
606
- ### Common Errors
607
-
608
- **Project Already Exists**:
609
-
610
- ```
611
- Error: Project "my-project" already exists in target database
612
- ```
613
-
614
- Solution: Use `--project-name` to specify different name, or remove existing project
615
-
616
- **Circular Dependencies**:
617
-
618
- ```
619
- Error: Circular dependency detected in task dependencies
620
- ```
621
-
622
- Solution: Fix dependency graph in source database before exporting
623
-
624
- **Invalid Foreign Keys**:
625
-
626
- ```
627
- Error: Foreign key constraint violation for task_id=123
628
- ```
629
-
630
- Solution: Ensure all referenced entities exist in export
631
-
632
- ### Dry-Run Validation
633
-
634
- Always test imports with `dry-run=true` first:
635
-
636
- ```bash
637
- sqlew db:import data.json dry-run=true
638
- ```
639
-
640
- This validates:
641
-
642
- - JSON format and structure
643
- - Project name conflicts
644
- - Task dependency cycles
645
- - Foreign key references
646
- - Data type correctness
647
-
648
- ## Technical Details
649
-
650
- ### Performance
651
-
652
- - **Batch Inserts**: 10-row batches to avoid SQLite limits
653
- - **Transaction Scope**: Single transaction for atomicity
654
- - **Memory Efficient**: Streams large datasets
655
-
656
- ### Limitations
657
-
658
- - **Max JSON Size**: Limited by available memory
659
- - **SQLite Batch Limit**: 500 UNION ALL clauses (handled automatically)
660
- - **Cross-Database**: JSON format only (no SQL dump)
661
-
662
- ### Data Integrity
663
-
664
- - **Foreign Key Validation**: All references validated before insertion
665
- - **Orphan Cleanup**: Invalid references automatically filtered
666
- - **View Restoration**: Views temporarily dropped and restored during import
667
- - **Idempotent Operations**: Safe to retry on network/disk failures
668
-
669
- ## Comparison with db:dump
670
-
671
- | Feature | db:export (JSON) | db:dump (SQL) |
672
- | ------------------ | ------------------------------------- | ----------------------------- |
673
- | Format | JSON data only | SQL DDL + data |
674
- | Schema | Not included | Full schema included |
675
- | Use Case | **Cross-DB migration**, sharing | **Same-DB backup/restore** |
676
- | Cross-DB | ✅ **Yes (ONLY option for cross-DB)** | ❌ No (v4.0.2+ same-DB only) |
677
- | Size | Smaller (~40% reduction) | Larger (includes schema) |
678
- | Import Speed | Slower (ID remapping) | Faster (direct SQL execution) |
679
- | Conflict Handling | Smart deduplication | Overwrite or fail |
680
- | Restore Capability | ❌ Skips if exists | ✅ Full restore |
681
-
682
- **When to use db:export (JSON)** - **REQUIRED FOR CROSS-DATABASE**:
683
-
684
- - ✅ **Cross-database migration** (SQLite → MySQL → PostgreSQL) - **ONLY option**
685
- - ✅ Migrating projects between different sqlew databases
686
- - ✅ Sharing specific projects with team members
687
- - ✅ Merging multiple projects into one database
688
-
689
- **When to use db:dump (SQL)** - **SAME-DATABASE ONLY**:
690
-
691
- - ✅ **Full database backup with schema** (same DB type)
692
- - ✅ **Database restore/recovery** (same DB type)
693
- - ✅ Database replication (same DB type)
694
- - ❌ Cross-database migration (use JSON instead)
695
-
696
- See [Database Migration Guide](DATABASE_MIGRATION.md) for complete `db:dump` documentation.
697
-
698
- ## See Also
699
-
700
- - [Database Migration](DATABASE_MIGRATION.md) - SQLite → MySQL/PostgreSQL migration
701
- - [CHANGELOG.md](../../CHANGELOG.md#374) - v3.7.4 release notes