diff --git a/src/services/migrationService.ts b/src/services/migrationService.ts index b8e2091e..4c39beb9 100644 --- a/src/services/migrationService.ts +++ b/src/services/migrationService.ts @@ -521,9 +521,18 @@ export async function runMigrations( // Update any old migration names to new ones for (const [oldName, newName] of migrationNameMap) { try { - await sqlExec("UPDATE migrations SET name = ? WHERE name = ?", [newName, oldName]); - if (await sqlQuery("SELECT 1 FROM migrations WHERE name = ? LIMIT 1", [newName])) { - migrationLog(`🔄 [Migration] Renamed migration: ${oldName} → ${newName}`); + await sqlExec("UPDATE migrations SET name = ? WHERE name = ?", [ + newName, + oldName, + ]); + if ( + await sqlQuery("SELECT 1 FROM migrations WHERE name = ? LIMIT 1", [ + newName, + ]) + ) { + migrationLog( + `🔄 [Migration] Renamed migration: ${oldName} → ${newName}`, + ); } } catch (error) { // Ignore errors - migration might not exist @@ -594,21 +603,35 @@ export async function runMigrations( migrationLog(`🔧 [Migration] Executing SQL for: ${migration.name}`); migrationLog(`🔧 [Migration] SQL content: ${migration.sql}`); const execResult = await sqlExec(migration.sql); - migrationLog(`🔧 [Migration] SQL execution result: ${JSON.stringify(execResult)}`); - + migrationLog( + `🔧 [Migration] SQL execution result: ${JSON.stringify(execResult)}`, + ); + // Debug: Check if active_identity table exists and has data if (migration.name === "003_active_identity_and_seed_backup") { try { - const tableCheck = await sqlQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='active_identity'"); - migrationLog(`🔍 [Migration] Table check result: ${JSON.stringify(tableCheck)}`); - - const rowCount = await sqlQuery("SELECT COUNT(*) as count FROM active_identity"); - migrationLog(`🔍 [Migration] Row count in active_identity: ${JSON.stringify(rowCount)}`); - + const tableCheck = await sqlQuery( + "SELECT name FROM sqlite_master WHERE type='table' AND name='active_identity'", + ); + migrationLog( + `🔍 [Migration] Table check result: ${JSON.stringify(tableCheck)}`, + ); + + const rowCount = await sqlQuery( + "SELECT COUNT(*) as count FROM active_identity", + ); + migrationLog( + `🔍 [Migration] Row count in active_identity: ${JSON.stringify(rowCount)}`, + ); + const allRows = await sqlQuery("SELECT * FROM active_identity"); - migrationLog(`🔍 [Migration] All rows in active_identity: ${JSON.stringify(allRows)}`); + migrationLog( + `🔍 [Migration] All rows in active_identity: ${JSON.stringify(allRows)}`, + ); } catch (error) { - migrationLog(`❌ [Migration] Debug query failed: ${JSON.stringify(error)}`); + migrationLog( + `❌ [Migration] Debug query failed: ${JSON.stringify(error)}`, + ); } } diff --git a/src/utils/PlatformServiceMixin.ts b/src/utils/PlatformServiceMixin.ts index 92763338..39b40afe 100644 --- a/src/utils/PlatformServiceMixin.ts +++ b/src/utils/PlatformServiceMixin.ts @@ -673,25 +673,25 @@ export const PlatformServiceMixin = { logger.debug( "[PlatformServiceMixin] Active identity is null (initial state), attempting auto-selection", ); - + // Try to auto-select first available account const availableAccounts = await this.$dbQuery( "SELECT did FROM accounts ORDER BY dateCreated, did LIMIT 1", ); - + if (availableAccounts?.values?.length) { const firstAccountDid = availableAccounts.values[0][0] as string; logger.debug( "[PlatformServiceMixin] Auto-selecting first account as active:", { firstAccountDid }, ); - + // Update active_identity table with the first account await this.$dbExec( "UPDATE active_identity SET activeDid = ?, lastUpdated = datetime('now') WHERE id = 1", [firstAccountDid], ); - + logger.debug( "[PlatformServiceMixin] Active identity auto-selected successfully", );