From c3534b54ae5be54eb507d67a1ed62160472d1c6b Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Fri, 22 Aug 2025 10:38:48 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20update=20legacy=20utility=20functions=20?= =?UTF-8?q?to=20use=20active=20identity=20fa=C3=A7ade?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace updateDefaultSettings calls with active_identity table operations - Add feature flag checks to avoid legacy settings table in Phase C - Update saveNewIdentity and registerSaveAndActivatePasskey functions - Ensure new identities are properly stored in active_identity table This fixes the 'no such column: activeDid' errors that occurred after Migration 004 dropped the legacy column. --- src/libs/util.ts | 68 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/src/libs/util.ts b/src/libs/util.ts index c65f2f8a..652ce5b7 100644 --- a/src/libs/util.ts +++ b/src/libs/util.ts @@ -655,7 +655,38 @@ export async function saveNewIdentity( ]; await platformService.dbExec(sql, params); - await platformService.updateDefaultSettings({ activeDid: identity.did }); + // Set the new identity as active using Active Identity façade + // Check if we need to avoid legacy settings table (Phase C) + const FLAGS = await import("@/config/featureFlags"); + + if (!FLAGS.FLAGS.DROP_SETTINGS_ACTIVEDID) { + // Phase A/B: Update legacy settings table + await platformService.updateDefaultSettings({ activeDid: identity.did }); + } + + // Always update/insert into new active_identity table + const DEFAULT_SCOPE = "default"; + const existingRecord = await platformService.dbQuery( + "SELECT id FROM active_identity WHERE scope = ? LIMIT 1", + [DEFAULT_SCOPE], + ); + + if (existingRecord?.values?.length) { + // Update existing record + await platformService.dbExec( + `UPDATE active_identity + SET active_did = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') + WHERE scope = ?`, + [identity.did, DEFAULT_SCOPE], + ); + } else { + // Insert new record + await platformService.dbExec( + `INSERT INTO active_identity (scope, active_did, updated_at) + VALUES (?, ?, strftime('%Y-%m-%dT%H:%M:%fZ','now'))`, + [DEFAULT_SCOPE, identity.did], + ); + } await platformService.insertDidSpecificSettings(identity.did); } catch (error) { @@ -714,7 +745,40 @@ export const registerSaveAndActivatePasskey = async ( ): Promise => { const account = await registerAndSavePasskey(keyName); const platformService = await getPlatformService(); - await platformService.updateDefaultSettings({ activeDid: account.did }); + + // Set the new account as active using Active Identity façade + // Check if we need to avoid legacy settings table (Phase C) + const FLAGS = await import("@/config/featureFlags"); + + if (!FLAGS.FLAGS.DROP_SETTINGS_ACTIVEDID) { + // Phase A/B: Update legacy settings table + await platformService.updateDefaultSettings({ activeDid: account.did }); + } + + // Always update/insert into new active_identity table + const DEFAULT_SCOPE = "default"; + const existingRecord = await platformService.dbQuery( + "SELECT id FROM active_identity WHERE scope = ? LIMIT 1", + [DEFAULT_SCOPE], + ); + + if (existingRecord?.values?.length) { + // Update existing record + await platformService.dbExec( + `UPDATE active_identity + SET active_did = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') + WHERE scope = ?`, + [account.did, DEFAULT_SCOPE], + ); + } else { + // Insert new record + await platformService.dbExec( + `INSERT INTO active_identity (scope, active_did, updated_at) + VALUES (?, ?, strftime('%Y-%m-%dT%H:%M:%fZ','now'))`, + [DEFAULT_SCOPE, account.did], + ); + } + await platformService.updateDidSpecificSettings(account.did, { isRegistered: false, });