commit 1f60c9e78d6363756265a8b7c007e4b5e8c31b18
parent 56ee705ad483da4c2aef53fd09c8c599b36aead0
Author: Florian Dold <dold@taler.net>
Date: Mon, 23 Nov 2026 19:48:40 +0100
wallet-core: fix migration recovery for older wallet databases
When a file contains both IndexedDB and native SQLite records, check the
tables for its schema version. Older wallets can still choose which copy
to keep even if they predate the inbox tables.
Diffstat:
3 files changed, 50 insertions(+), 14 deletions(-)
diff --git a/packages/taler-wallet-core/src/db/migration/native.test.ts b/packages/taler-wallet-core/src/db/migration/native.test.ts
@@ -626,9 +626,13 @@ test("native migration: unrelated emulated databases are not wallet records", as
await db.close();
});
-test("native migration: explicit resolution can keep IndexedDB", async () => {
- const { db } = await makePopulatedIdbDb();
- await openNativeSqliteWalletDb(db);
+test("native migration: an older mixed wallet can keep its IndexedDB records", async () => {
+ const { db } = await makeMinimalIdbDb();
+ await initSqliteWalletDb(
+ db,
+ schemaMigrations.filter((m) => m.version <= 29),
+ );
+ await db.exec("PRAGMA user_version = 29");
await (
await db.prepare(
"INSERT INTO config (key, value) VALUES ('native-only', '\"discard\"')",
@@ -637,6 +641,7 @@ test("native migration: explicit resolution can keep IndexedDB", async () => {
await resolveAmbiguousWalletDb(db, "indexeddb");
assert.strictEqual(await inspectWalletDbFile(db), "indexeddb");
assert.strictEqual(await countRows(db, "config"), 0);
+ assert.strictEqual(await countRows(db, "user_attention_items"), 0);
assert.strictEqual(
(await readNativeMigrationInfo(db))?.status,
"rolled-back",
@@ -660,6 +665,25 @@ test("native migration: explicit resolution can keep native", async () => {
await db.close();
});
+test("native migration: an expected missing table prevents resolution", async () => {
+ const { db } = await makeMinimalIdbDb();
+ try {
+ await openNativeSqliteWalletDb(db);
+ await db.exec(
+ "INSERT INTO config (key, value) VALUES ('native-only', '1')",
+ );
+ await db.exec("DROP TABLE user_attention_items");
+ await assert.rejects(
+ () => resolveAmbiguousWalletDb(db, "indexeddb"),
+ /expected native table user_attention_items is missing/,
+ );
+ assert.strictEqual(await countRows(db, "config"), 1);
+ assert.ok((await countRows(db, "object_data")) > 0);
+ } finally {
+ await db.close();
+ }
+});
+
test("native migration: rollback preflight failure preserves native rows", async () => {
const { db, handle } = await makePopulatedIdbDb();
const { handle: native } = await migrateWalletDbToNative(db, handle);
diff --git a/packages/taler-wallet-core/src/db/migration/native.ts b/packages/taler-wallet-core/src/db/migration/native.ts
@@ -64,6 +64,7 @@ import {
IDB_BACKUP_PREFIX,
IDB_EMULATION_TABLES,
NATIVE_DATA_TABLES,
+ nativeDataTablesAtVersion,
} from "../sqlite/schema.js";
import { SqliteWalletDbHandle } from "../sqlite/handle.js";
import { WalletDbHandle } from "../handle.js";
@@ -656,7 +657,10 @@ async function requireResolutionTables(db: Sqlite3Database): Promise<void> {
);
}
}
- for (const table of NATIVE_DATA_TABLES) {
+ const migration = await (
+ await db.prepare("SELECT MAX(version) AS version FROM schema_migrations")
+ ).getFirst({});
+ for (const table of nativeDataTablesAtVersion(Number(migration?.version))) {
if (!(await tableExists(db, table))) {
throw Error(
`cannot resolve migration: expected native table ${table} is missing`,
diff --git a/packages/taler-wallet-core/src/db/sqlite/schema.ts b/packages/taler-wallet-core/src/db/sqlite/schema.ts
@@ -1577,13 +1577,21 @@ export const schemaMigrations: SchemaMigration[] = [
},
];
-/** Native tables that contain wallet records (not schema bookkeeping). */
-export const NATIVE_DATA_TABLES = [
- ...[...SQLITE_BASELINE_SCHEMA.matchAll(/CREATE TABLE IF NOT EXISTS (\w+)/g)]
- .map((m) => m[1])
- .filter((name) => !NON_DATA_TABLES.includes(name)),
- "transaction_coin_conflicts",
- "user_attention_items",
- "coin_recoveries",
- "refresh_melt_confirmations",
-];
+/** Native data tables present after the specified migration. */
+export function nativeDataTablesAtVersion(version: number): string[] {
+ const ddl = [
+ SQLITE_BASELINE_SCHEMA,
+ ...schemaMigrations
+ .filter((m) => m.version <= version)
+ .flatMap((m) => m.statements),
+ ];
+ return ddl.flatMap((sql) =>
+ [...sql.matchAll(/^CREATE TABLE (?:IF NOT EXISTS )?(\w+)/gm)]
+ .map((m) => m[1])
+ .filter((name) => !NON_DATA_TABLES.includes(name)),
+ );
+}
+
+export const NATIVE_DATA_TABLES = nativeDataTablesAtVersion(
+ SQLITE_SCHEMA_VERSION,
+);