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; + `, + }, ]; /**