From 7917e707e9757d547ba47dcb2d409f0a4695380f Mon Sep 17 00:00:00 2001 From: Jose Olarte III Date: Thu, 11 Sep 2025 17:39:26 +0800 Subject: [PATCH] fix: resolve iOS database migration failure for active_identity table The migration 003_active_identity_and_seed_backup was failing on iOS when switching from master to active_did_redux branch because it attempted to execute multiple SQL operations in a single block. When the ALTER TABLE statement for hasBackedUpSeed failed (due to column already existing), the entire migration was marked as "already applied" even though the active_identity table was never created. Changes: - Split migration 003 into two separate migrations: - 003_active_identity_and_seed_backup: Creates active_identity table - 003b_add_hasBackedUpSeed_to_settings: Adds hasBackedUpSeed column - Added data migration logic to copy existing activeDid from settings to active_identity table during migration - Added debug logging to track migration results - Ensured atomic operations so table creation doesn't depend on column addition This fix ensures that: - The active_identity table is always created successfully - Existing activeDid values are preserved during migration - The app remembers the active identity between master and active_did_redux builds - Migration errors are handled gracefully without affecting other operations Fixes iOS migration issue where app would lose active identity state when switching between branches, causing users to lose their selected identity and requiring manual re-selection. Tested: Migration now works correctly on iOS simulator when switching from master branch (with old schema) to active_did_redux branch. --- src/db-sql/migration.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/db-sql/migration.ts b/src/db-sql/migration.ts index 6ed1c1e2..246e96df 100644 --- a/src/db-sql/migration.ts +++ b/src/db-sql/migration.ts @@ -157,15 +157,32 @@ const MIGRATIONS = [ SELECT 1, NULL, datetime('now') WHERE NOT EXISTS (SELECT 1 FROM active_identity WHERE id = 1); - -- Add hasBackedUpSeed field to settings (consolidated from 003b) - -- This may fail if column already exists from master branch migration - -- The error handling will catch this and mark migration as applied - ALTER TABLE settings ADD COLUMN hasBackedUpSeed BOOLEAN DEFAULT FALSE; + -- MIGRATE EXISTING DATA: Copy activeDid from settings to active_identity + -- This prevents data loss when migration runs on existing databases + UPDATE active_identity + SET activeDid = (SELECT activeDid FROM settings WHERE id = 1), + lastUpdated = datetime('now') + WHERE id = 1 + AND EXISTS (SELECT 1 FROM settings WHERE id = 1 AND activeDid IS NOT NULL AND activeDid != ''); + + -- Debug: Log the migration result + SELECT 'DEBUG: ActiveDid migration result' as debug_message, + (SELECT activeDid FROM active_identity WHERE id = 1) as migrated_activeDid, + (SELECT activeDid FROM settings WHERE id = 1) as original_activeDid; -- Debug: Verify the row was inserted SELECT 'DEBUG: Row count after insertion' as debug_message, COUNT(*) as row_count FROM active_identity; `, }, + { + name: "003b_add_hasBackedUpSeed_to_settings", + sql: ` + -- Add hasBackedUpSeed field to settings + -- This may fail if column already exists from master branch migration + -- The error handling will catch this and mark migration as applied + ALTER TABLE settings ADD COLUMN hasBackedUpSeed BOOLEAN DEFAULT FALSE; + `, + }, ]; /**