diff --git a/doc/activeDid-migration-plan.md b/doc/activeDid-migration-plan.md index 2615374b..b69d3b30 100644 --- a/doc/activeDid-migration-plan.md +++ b/doc/activeDid-migration-plan.md @@ -65,14 +65,6 @@ Follow this implementation checklist step-by-step to complete the migration. -- Insert default record (will be updated during migration) INSERT OR IGNORE INTO active_identity (id, activeDid, lastUpdated) VALUES (1, '', datetime('now')); - - -- 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 != ''); `, }, ``` @@ -124,7 +116,7 @@ async $getActiveIdentity(): Promise<{ activeDid: string }> { async $accountSettings(did?: string, defaults: Settings = {}): Promise { try { // Get settings without activeDid (unchanged logic) - const settings = await this._getSettingsWithoutActiveDid(); + const settings = await this.$getMasterSettings(defaults); if (!settings) { return defaults; @@ -142,9 +134,61 @@ async $accountSettings(did?: string, defaults: Settings = {}): Promise } ``` -### 4. Component Updates Required +### 4. Updated $updateActiveDid Method + +```typescript +// Update in PlatformServiceMixin.ts +async $updateActiveDid(newDid: string | null): Promise { + try { + if (newDid === null) { + // Clear active identity in both tables + await this.$dbExec( + "UPDATE active_identity SET activeDid = '', lastUpdated = datetime('now') WHERE id = 1" + ); + + // Keep legacy field in sync (backward compatibility) + await this.$dbExec( + "UPDATE settings SET activeDid = '' WHERE id = ?", + [MASTER_SETTINGS_KEY] + ); + } else { + // Validate DID exists before setting + const accountExists = await this.$dbQuery( + "SELECT did FROM accounts WHERE did = ?", + [newDid] + ); + + if (!accountExists?.values?.length) { + logger.error(`[PlatformServiceMixin] Cannot set activeDid to non-existent DID: ${newDid}`); + return false; + } + + // Update active identity in new table + await this.$dbExec( + "UPDATE active_identity SET activeDid = ?, lastUpdated = datetime('now') WHERE id = 1", + [newDid] + ); + + // Keep legacy field in sync (backward compatibility) + await this.$dbExec( + "UPDATE settings SET activeDid = ? WHERE id = ?", + [newDid, MASTER_SETTINGS_KEY] + ); + } + + // Update internal tracking + await this._updateInternalActiveDid(newDid); + return true; + } catch (error) { + logger.error("[PlatformServiceMixin] Error updating activeDid:", error); + return false; + } +} +``` + +### 5. Component Updates Required -**35+ components need this pattern change:** +**35 components need this pattern change:** ```typescript // CURRENT PATTERN (replace in all components): @@ -157,7 +201,7 @@ this.activeDid = activeIdentity.activeDid || ""; **Components requiring updates:** -#### Views (25 components) +#### Views (28 components) - `src/views/DIDView.vue` (line 378) - `src/views/TestView.vue` (line 654) - `src/views/ContactAmountsView.vue` (line 226) @@ -188,7 +232,7 @@ this.activeDid = activeIdentity.activeDid || ""; - `src/views/ConfirmGiftView.vue` (line 549) - `src/views/ContactImportView.vue` (line 342) -#### Components (10 components) +#### Components (7 components) - `src/components/OfferDialog.vue` (line 177) - `src/components/PhotoDialog.vue` (line 270) - `src/components/GiftedDialog.vue` (line 223) @@ -241,7 +285,7 @@ private async initializeSettings() { } ``` -### 5. Data Migration Function +### 6. Data Migration Function ```typescript // Add to migration.ts @@ -290,9 +334,11 @@ async function migrateActiveDidToSeparateTable(): Promise { result.warnings.push(`Successfully migrated activeDid: ${activeDid}`); } catch (error) { - logger.error("[PlatformServiceMixin] Error getting active identity:", error); - throw error; + result.errors.push(`Migration failed: ${error}`); + logger.error("[ActiveDid Migration] Critical error during migration:", error); } + + return result; } ```