aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/db.ts
diff options
context:
space:
mode:
authorFlorian Dold <florian@dold.me>2020-11-16 14:12:37 +0100
committerFlorian Dold <florian@dold.me>2020-11-16 14:12:37 +0100
commit292160f7e979a0d9fdea7430f0d26b2dc1122a16 (patch)
treef82a576398408ebbe04d96a93355de61d745c925 /packages/taler-wallet-core/src/db.ts
parentcdf5cc583cd7fc938f38137da25aaee2aeaf28a9 (diff)
fix tip record creation, migrate DB
Diffstat (limited to 'packages/taler-wallet-core/src/db.ts')
-rw-r--r--packages/taler-wallet-core/src/db.ts56
1 files changed, 40 insertions, 16 deletions
diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts
index b94fd1547..ac3c79e20 100644
--- a/packages/taler-wallet-core/src/db.ts
+++ b/packages/taler-wallet-core/src/db.ts
@@ -1,6 +1,7 @@
import { Stores } from "./types/dbTypes";
import { openDatabase, Database, Store, Index } from "./util/query";
-import { IDBFactory, IDBDatabase } from "idb-bridge";
+import { IDBFactory, IDBDatabase, IDBObjectStore, IDBTransaction } from "idb-bridge";
+import { Logger } from './util/logging';
/**
* Name of the Taler database. This is effectively the major
@@ -17,7 +18,9 @@ const TALER_DB_NAME = "taler-wallet-prod-v1";
* backwards-compatible way or object stores and indices
* are added.
*/
-export const WALLET_DB_MINOR_VERSION = 1;
+export const WALLET_DB_MINOR_VERSION = 2;
+
+const logger = new Logger("db.ts");
/**
* Return a promise that resolves
@@ -31,24 +34,45 @@ export function openTalerDatabase(
db: IDBDatabase,
oldVersion: number,
newVersion: number,
+ upgradeTransaction: IDBTransaction,
): void => {
- switch (oldVersion) {
- case 0: // DB does not exist yet
- for (const n in Stores) {
- if ((Stores as any)[n] instanceof Store) {
- const si: Store<any> = (Stores as any)[n];
- const s = db.createObjectStore(si.name, si.storeParams);
- for (const indexName in si as any) {
- if ((si as any)[indexName] instanceof Index) {
- const ii: Index<any, any> = (si as any)[indexName];
- s.createIndex(ii.indexName, ii.keyPath, ii.options);
- }
+ if (oldVersion === 0) {
+ for (const n in Stores) {
+ if ((Stores as any)[n] instanceof Store) {
+ const si: Store<any> = (Stores as any)[n];
+ const s = db.createObjectStore(si.name, si.storeParams);
+ for (const indexName in si as any) {
+ if ((si as any)[indexName] instanceof Index) {
+ const ii: Index<any, any> = (si as any)[indexName];
+ s.createIndex(ii.indexName, ii.keyPath, ii.options);
+ }
+ }
+ }
+ }
+ return;
+ }
+ if (oldVersion === newVersion) {
+ return;
+ }
+ logger.info(`upgrading database from ${oldVersion} to ${newVersion}`);
+ for (const n in Stores) {
+ if ((Stores as any)[n] instanceof Store) {
+ const si: Store<any> = (Stores as any)[n];
+ let s: IDBObjectStore;
+ if ((si.storeParams?.versionAdded ?? 1) > oldVersion) {
+ s = db.createObjectStore(si.name, si.storeParams);
+ } else {
+ s = upgradeTransaction.objectStore(si.name);
+ }
+ for (const indexName in si as any) {
+ if ((si as any)[indexName] instanceof Index) {
+ const ii: Index<any, any> = (si as any)[indexName];
+ if ((ii.options?.versionAdded ?? 0) > oldVersion) {
+ s.createIndex(ii.indexName, ii.keyPath, ii.options);
}
}
}
- break;
- default:
- throw Error("unsupported existig DB version");
+ }
}
};