From 99db5deb778ee5dafff5251014b79114fd844bed Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Sun, 5 Nov 2023 15:43:57 -0700 Subject: [PATCH] attempt migration to consolidate name fields, but it fails with these errors: - Transaction aborted - Not yet support for changing primary key - TypeError: WeakMap key must be an object, got pr --- src/db/index.ts | 27 ++++++++++++++++++++++++++- src/db/tables/settings.ts | 7 ++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/db/index.ts b/src/db/index.ts index d39ea5e..6a32a0d 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -6,6 +6,7 @@ import { MASTER_SETTINGS_KEY, Settings, SettingsSchema, + SettingsSchemaV1, } from "./tables/settings"; import { AppString } from "@/constants/app"; @@ -34,6 +35,8 @@ const SensitiveSchemas = Object.assign({}, AccountsSchema); export type NonsensitiveDexie = BaseDexie & T; export const db = new BaseDexie("TimeSafari") as NonsensitiveDexie; +// eslint-disable-next-line prettier/prettier +const NonsensitiveSchemasV1 = Object.assign({}, ContactsSchema, SettingsSchemaV1); const NonsensitiveSchemas = Object.assign({}, ContactsSchema, SettingsSchema); /** @@ -57,7 +60,29 @@ if (localStorage.getItem("secret") == null) { encrypted(accountsDB, { secretKey: secret }); accountsDB.version(1).stores(SensitiveSchemas); -db.version(1).stores(NonsensitiveSchemas); +db.version(1).stores(NonsensitiveSchemasV1); + +db.version(2) + .stores(NonsensitiveSchemas) + .upgrade((tx) => { + return tx + .table("settings") + .toCollection() + .modify((settings) => { + if ( + typeof settings.firstName === "string" && + typeof settings.lastName === "string" + ) { + settings.firstName += " " + settings.lastName; + } else if (typeof settings.lastName === "string") { + settings.firstName = settings.lastName; + } + delete settings.lastName; + }) + .catch((e) => { + console.log("caught modify exception", e); + }); + }); // initialize, a la https://dexie.org/docs/Tutorial/Design#the-populate-event db.on("populate", function () { diff --git a/src/db/tables/settings.ts b/src/db/tables/settings.ts index 8298dfe..65c9573 100644 --- a/src/db/tables/settings.ts +++ b/src/db/tables/settings.ts @@ -21,8 +21,13 @@ export type Settings = { showContactGivesInline?: boolean; }; -export const SettingsSchema = { +export const SettingsSchemaV1 = { settings: "id", }; +export const SettingsSchema = { + settings: + "id, activeDid, apiServer, firstName, lastname, lastViewedClaimId, searchBoxes, showContactGivesInline", +}; + export const MASTER_SETTINGS_KEY = 1;