fix account datebase migration

This commit is contained in:
hzrd149 2023-11-29 12:28:51 -06:00
parent 1798b2dc2f
commit 84db6eb30d
2 changed files with 55 additions and 5 deletions

View File

@ -1,12 +1,12 @@
import { openDB, deleteDB, IDBPDatabase } from "idb";
import { SchemaV1, SchemaV2, SchemaV3, SchemaV4 } from "./schema";
import { SchemaV1, SchemaV2, SchemaV3, SchemaV4, SchemaV5 } from "./schema";
import { logger } from "../../helpers/debug";
const log = logger.extend("Database");
const dbName = "storage";
const version = 4;
const db = await openDB<SchemaV4>(dbName, version, {
const version = 5;
const db = await openDB<SchemaV5>(dbName, version, {
upgrade(db, oldVersion, newVersion, transaction, event) {
if (oldVersion < 1) {
const v0 = db as unknown as IDBPDatabase<SchemaV1>;
@ -87,6 +87,27 @@ const db = await openDB<SchemaV4>(dbName, version, {
keyPath: "pubkey",
});
}
if (oldVersion < 5) {
const v4 = db as unknown as IDBPDatabase<SchemaV4>;
const v5 = db as unknown as IDBPDatabase<SchemaV5>;
// migrate accounts table
const objectStore = transaction.objectStore("accounts");
objectStore.getAll().then((accounts: SchemaV4["accounts"]["value"][]) => {
for (const account of accounts) {
const newAccount: SchemaV5["accounts"] = {
...account,
connectionType: account.useExtension ? "extension" : undefined,
};
// @ts-ignore
delete newAccount.useExtension;
objectStore.put(newAccount);
}
});
}
},
});

View File

@ -1,7 +1,7 @@
import { DBSchema } from "idb";
import { NostrEvent } from "../../types/nostr-event";
import { Account } from "../account";
import { RelayInformationDocument } from "../relay-info";
import { AppSettings } from "../settings/migrations";
export interface SchemaV1 extends DBSchema {
userMetadata: {
@ -46,11 +46,20 @@ export interface SchemaV1 extends DBSchema {
};
accounts: {
key: string;
value: Account;
value: {
pubkey: string;
readonly: boolean;
relays?: string[];
secKey?: ArrayBuffer;
iv?: Uint8Array;
useExtension?: boolean;
localSettings?: AppSettings;
};
};
}
export interface SchemaV2 extends SchemaV1 {
accounts: SchemaV1["accounts"];
settings: {
key: string;
value: NostrEvent;
@ -63,6 +72,7 @@ export interface SchemaV2 extends SchemaV1 {
}
export interface SchemaV3 {
accounts: SchemaV2["accounts"];
replaceableEvents: {
key: string;
value: {
@ -79,6 +89,7 @@ export interface SchemaV3 {
}
export interface SchemaV4 {
accounts: SchemaV3["accounts"];
replaceableEvents: SchemaV3["replaceableEvents"];
dnsIdentifiers: SchemaV3["dnsIdentifiers"];
relayInfo: SchemaV3["relayInfo"];
@ -92,3 +103,21 @@ export interface SchemaV4 {
};
misc: SchemaV3["misc"];
}
export interface SchemaV5 {
accounts: {
pubkey: string;
readonly: boolean;
relays?: string[];
secKey?: ArrayBuffer;
iv?: Uint8Array;
connectionType?: "extension" | "serial";
localSettings?: AppSettings;
};
replaceableEvents: SchemaV4["replaceableEvents"];
dnsIdentifiers: SchemaV4["dnsIdentifiers"];
relayInfo: SchemaV4["relayInfo"];
relayScoreboardStats: SchemaV4["relayScoreboardStats"];
userSearch: SchemaV4["userSearch"];
misc: SchemaV4["misc"];
}