fix: Resolve database migration issues and consolidate 003 migrations

- Fix $getActiveIdentity() logic flow preventing false "empty table" warnings
- Implement auto-selection of first account when activeDid is null after migration
- Consolidate 003 and 003b migrations into single 003 migration
- Re-introduce foreign key constraint for activeDid referential integrity
- Add comprehensive debug logging for migration troubleshooting
- Remove 003b validation logic and update migration name mapping

Fixes migration from master to active_did_redux branch and ensures system
always has valid activeDid for proper functionality.
This commit is contained in:
Matthew Raymer
2025-09-11 05:07:23 +00:00
parent 6da9e14b8a
commit 616bef655a
3 changed files with 115 additions and 10 deletions

View File

@@ -668,6 +668,42 @@ export const PlatformServiceMixin = {
{ activeDid },
);
// Handle null activeDid (initial state after migration)
if (activeDid === null) {
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",
);
return { activeDid: firstAccountDid };
} else {
logger.warn(
"[PlatformServiceMixin] No accounts available for auto-selection",
);
return { activeDid: "" };
}
}
// Validate activeDid exists in accounts
if (activeDid) {
const accountExists = await this.$dbQuery(
@@ -690,6 +726,12 @@ export const PlatformServiceMixin = {
);
return { activeDid: "" };
}
} else {
// activeDid is empty string, return it
logger.debug(
"[PlatformServiceMixin] Active identity is empty string, returning as-is",
);
return { activeDid: "" };
}
}