Browse Source

feat: Add comprehensive database migration service for Dexie to SQLite

- Add migrationService.ts with functions to compare and transfer data between Dexie and SQLite
- Implement data comparison with detailed difference analysis (added/modified/missing)
- Add contact migration with overwrite options and error handling
- Add settings migration focusing on key user fields (firstName, isRegistered, profileImageUrl, showShortcutBvc, searchBoxes)
- Include YAML export functionality for data inspection
- Add comprehensive JSDoc documentation with examples and usage instructions
- Support both INSERT and UPDATE operations with parameterized SQL generation
- Include detailed logging and error reporting for migration operations

This service enables safe migration of user data from the legacy Dexie (IndexedDB)
database to the new SQLite implementation, with full comparison capabilities
and rollback safety through detailed reporting.
migrate-dexie-to-sqlite
Matthew Raymer 1 week ago
parent
commit
25c1d6ef4e
  1. 19
      doc/dexie-to-sqlite-mapping.md
  2. 2
      src/db/tables/README.md
  3. 1019
      src/services/migrationService.ts

19
doc/dexie-to-sqlite-mapping.md

@ -3,6 +3,7 @@
## Schema Mapping ## Schema Mapping
### Current Dexie Schema ### Current Dexie Schema
```typescript ```typescript
// Current Dexie schema // Current Dexie schema
const db = new Dexie('TimeSafariDB'); const db = new Dexie('TimeSafariDB');
@ -15,6 +16,7 @@ db.version(1).stores({
``` ```
### New SQLite Schema ### New SQLite Schema
```sql ```sql
-- New SQLite schema -- New SQLite schema
CREATE TABLE accounts ( CREATE TABLE accounts (
@ -50,6 +52,7 @@ CREATE INDEX idx_settings_updated_at ON settings(updated_at);
### 1. Account Operations ### 1. Account Operations
#### Get Account by DID #### Get Account by DID
```typescript ```typescript
// Dexie // Dexie
const account = await db.accounts.get(did); const account = await db.accounts.get(did);
@ -62,6 +65,7 @@ const account = result[0]?.values[0];
``` ```
#### Get All Accounts #### Get All Accounts
```typescript ```typescript
// Dexie // Dexie
const accounts = await db.accounts.toArray(); const accounts = await db.accounts.toArray();
@ -74,6 +78,7 @@ const accounts = result[0]?.values || [];
``` ```
#### Add Account #### Add Account
```typescript ```typescript
// Dexie // Dexie
await db.accounts.add({ await db.accounts.add({
@ -91,6 +96,7 @@ await db.run(`
``` ```
#### Update Account #### Update Account
```typescript ```typescript
// Dexie // Dexie
await db.accounts.update(did, { await db.accounts.update(did, {
@ -109,6 +115,7 @@ await db.run(`
### 2. Settings Operations ### 2. Settings Operations
#### Get Setting #### Get Setting
```typescript ```typescript
// Dexie // Dexie
const setting = await db.settings.get(key); const setting = await db.settings.get(key);
@ -121,6 +128,7 @@ const setting = result[0]?.values[0];
``` ```
#### Set Setting #### Set Setting
```typescript ```typescript
// Dexie // Dexie
await db.settings.put({ await db.settings.put({
@ -142,6 +150,7 @@ await db.run(`
### 3. Contact Operations ### 3. Contact Operations
#### Get Contacts by Account #### Get Contacts by Account
```typescript ```typescript
// Dexie // Dexie
const contacts = await db.contacts const contacts = await db.contacts
@ -159,6 +168,7 @@ const contacts = result[0]?.values || [];
``` ```
#### Add Contact #### Add Contact
```typescript ```typescript
// Dexie // Dexie
await db.contacts.add({ await db.contacts.add({
@ -179,6 +189,7 @@ await db.run(`
## Transaction Mapping ## Transaction Mapping
### Batch Operations ### Batch Operations
```typescript ```typescript
// Dexie // Dexie
await db.transaction('rw', [db.accounts, db.contacts], async () => { await db.transaction('rw', [db.accounts, db.contacts], async () => {
@ -210,6 +221,7 @@ try {
## Migration Helper Functions ## Migration Helper Functions
### 1. Data Export (Dexie to JSON) ### 1. Data Export (Dexie to JSON)
```typescript ```typescript
async function exportDexieData(): Promise<MigrationData> { async function exportDexieData(): Promise<MigrationData> {
const db = new Dexie('TimeSafariDB'); const db = new Dexie('TimeSafariDB');
@ -228,6 +240,7 @@ async function exportDexieData(): Promise<MigrationData> {
``` ```
### 2. Data Import (JSON to absurd-sql) ### 2. Data Import (JSON to absurd-sql)
```typescript ```typescript
async function importToAbsurdSql(data: MigrationData): Promise<void> { async function importToAbsurdSql(data: MigrationData): Promise<void> {
await db.exec('BEGIN TRANSACTION;'); await db.exec('BEGIN TRANSACTION;');
@ -264,6 +277,7 @@ async function importToAbsurdSql(data: MigrationData): Promise<void> {
``` ```
### 3. Verification ### 3. Verification
```typescript ```typescript
async function verifyMigration(dexieData: MigrationData): Promise<boolean> { async function verifyMigration(dexieData: MigrationData): Promise<boolean> {
// Verify account count // Verify account count
@ -307,18 +321,21 @@ async function verifyMigration(dexieData: MigrationData): Promise<boolean> {
## Performance Considerations ## Performance Considerations
### 1. Indexing ### 1. Indexing
- Dexie automatically creates indexes based on the schema - Dexie automatically creates indexes based on the schema
- absurd-sql requires explicit index creation - absurd-sql requires explicit index creation
- Added indexes for frequently queried fields - Added indexes for frequently queried fields
- Use `PRAGMA journal_mode=MEMORY;` for better performance - Use `PRAGMA journal_mode=MEMORY;` for better performance
### 2. Batch Operations ### 2. Batch Operations
- Dexie has built-in bulk operations - Dexie has built-in bulk operations
- absurd-sql uses transactions for batch operations - absurd-sql uses transactions for batch operations
- Consider chunking large datasets - Consider chunking large datasets
- Use prepared statements for repeated queries - Use prepared statements for repeated queries
### 3. Query Optimization ### 3. Query Optimization
- Dexie uses IndexedDB's native indexing - Dexie uses IndexedDB's native indexing
- absurd-sql requires explicit query optimization - absurd-sql requires explicit query optimization
- Use prepared statements for repeated queries - Use prepared statements for repeated queries
@ -327,6 +344,7 @@ async function verifyMigration(dexieData: MigrationData): Promise<boolean> {
## Error Handling ## Error Handling
### 1. Common Errors ### 1. Common Errors
```typescript ```typescript
// Dexie errors // Dexie errors
try { try {
@ -351,6 +369,7 @@ try {
``` ```
### 2. Transaction Recovery ### 2. Transaction Recovery
```typescript ```typescript
// Dexie transaction // Dexie transaction
try { try {

2
src/db/tables/README.md

@ -1 +1 @@
Check the contact & settings export to see whether you want your new table to be included in it. # Check the contact & settings export to see whether you want your new table to be included in it

1019
src/services/migrationService.ts

File diff suppressed because it is too large
Loading…
Cancel
Save