fix platform services to use correct settings table schema

- Fix WebPlatformService settings methods to use id/accountDid columns
- Fix CapacitorPlatformService settings methods to use id/accountDid columns
- Replace WHERE key = 'default' with WHERE id = 1 for default settings
- Replace WHERE key = ? with WHERE accountDid = ? for user settings
- Update insertDidSpecificSettings to use accountDid column
- Update retrieveSettingsForActiveAccount to select all columns and convert to object
- Resolves "no such column: key" SQL errors after util.ts migration
- Ensures compatibility with new settings table structure

All platform services now use correct database schema for settings operations.
This commit is contained in:
Matthew Raymer
2025-07-16 09:43:09 +00:00
parent 8825c52ebc
commit 9cbb9bf3c6
6 changed files with 184 additions and 36 deletions

View File

@@ -33,7 +33,6 @@ import { logger } from "../utils/logger";
import { PlatformServiceFactory } from "../services/PlatformServiceFactory";
import { IIdentifier } from "@veramo/core";
import { DEFAULT_ROOT_DERIVATION_PATH } from "./crypto";
import * as databaseUtil from "../db/databaseUtil";
// Self-contained utility functions to replace databaseUtil dependencies
function parseJsonField<T>(value: unknown, defaultValue: T): T {
@@ -667,9 +666,9 @@ export async function saveNewIdentity(
];
await platformService.dbExec(sql, params);
await databaseUtil.updateDefaultSettings({ activeDid: identity.did });
await platformService.updateDefaultSettings({ activeDid: identity.did });
await databaseUtil.insertDidSpecificSettings(identity.did);
await platformService.insertDidSpecificSettings(identity.did);
} catch (error) {
logger.error("Failed to update default settings:", error);
throw new Error(
@@ -691,7 +690,8 @@ export const generateSaveAndActivateIdentity = async (): Promise<string> => {
const newId = newIdentifier(address, publicHex, privateHex, derivationPath);
await saveNewIdentity(newId, mnemonic, derivationPath);
await databaseUtil.updateDidSpecificSettings(newId.did, {
const platformService = await getPlatformService();
await platformService.updateDidSpecificSettings(newId.did, {
isRegistered: false,
});
return newId.did;

View File

@@ -1323,16 +1323,13 @@ export class CapacitorPlatformService implements PlatformService {
): Promise<void> {
const keys = Object.keys(settings);
const setClause = keys.map((key) => `${key} = ?`).join(", ");
const sql = `UPDATE settings SET ${setClause} WHERE key = 'default'`;
const sql = `UPDATE settings SET ${setClause} WHERE id = 1`;
const params = keys.map((key) => settings[key]);
await this.dbExec(sql, params);
}
async insertDidSpecificSettings(did: string): Promise<void> {
await this.dbExec("INSERT INTO settings (key, value) VALUES (?, ?)", [
did,
"{}",
]);
await this.dbExec("INSERT INTO settings (accountDid) VALUES (?)", [did]);
}
async updateDidSpecificSettings(
@@ -1341,7 +1338,7 @@ export class CapacitorPlatformService implements PlatformService {
): Promise<void> {
const keys = Object.keys(settings);
const setClause = keys.map((key) => `${key} = ?`).join(", ");
const sql = `UPDATE settings SET ${setClause} WHERE key = ?`;
const sql = `UPDATE settings SET ${setClause} WHERE accountDid = ?`;
const params = [...keys.map((key) => settings[key]), did];
await this.dbExec(sql, params);
}
@@ -1350,15 +1347,21 @@ export class CapacitorPlatformService implements PlatformService {
string,
unknown
> | null> {
const result = await this.dbQuery(
"SELECT value FROM settings WHERE key = 'default'",
);
if (result?.values?.[0]?.[0]) {
try {
return JSON.parse(result.values[0][0] as string);
} catch {
return null;
}
const result = await this.dbQuery("SELECT * FROM settings WHERE id = 1");
if (result?.values?.[0]) {
// Convert the row to an object
const row = result.values[0];
const columns = result.columns || [];
const settings: Record<string, unknown> = {};
columns.forEach((column, index) => {
if (column !== "id") {
// Exclude the id column
settings[column] = row[index];
}
});
return settings;
}
return null;
}

View File

@@ -687,16 +687,13 @@ export class WebPlatformService implements PlatformService {
): Promise<void> {
const keys = Object.keys(settings);
const setClause = keys.map((key) => `${key} = ?`).join(", ");
const sql = `UPDATE settings SET ${setClause} WHERE key = 'default'`;
const sql = `UPDATE settings SET ${setClause} WHERE id = 1`;
const params = keys.map((key) => settings[key]);
await this.dbExec(sql, params);
}
async insertDidSpecificSettings(did: string): Promise<void> {
await this.dbExec("INSERT INTO settings (key, value) VALUES (?, ?)", [
did,
"{}",
]);
await this.dbExec("INSERT INTO settings (accountDid) VALUES (?)", [did]);
}
async updateDidSpecificSettings(
@@ -705,7 +702,7 @@ export class WebPlatformService implements PlatformService {
): Promise<void> {
const keys = Object.keys(settings);
const setClause = keys.map((key) => `${key} = ?`).join(", ");
const sql = `UPDATE settings SET ${setClause} WHERE key = ?`;
const sql = `UPDATE settings SET ${setClause} WHERE accountDid = ?`;
const params = [...keys.map((key) => settings[key]), did];
await this.dbExec(sql, params);
}
@@ -714,15 +711,21 @@ export class WebPlatformService implements PlatformService {
string,
unknown
> | null> {
const result = await this.dbQuery(
"SELECT value FROM settings WHERE key = 'default'",
);
if (result?.values?.[0]?.[0]) {
try {
return JSON.parse(result.values[0][0] as string);
} catch {
return null;
}
const result = await this.dbQuery("SELECT * FROM settings WHERE id = 1");
if (result?.values?.[0]) {
// Convert the row to an object
const row = result.values[0];
const columns = result.columns || [];
const settings: Record<string, unknown> = {};
columns.forEach((column, index) => {
if (column !== "id") {
// Exclude the id column
settings[column] = row[index];
}
});
return settings;
}
return null;
}