gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] branch master updated: add missing file


From: gnunet
Subject: [taler-wallet-core] branch master updated: add missing file
Date: Thu, 19 Aug 2021 13:56:05 +0200

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository wallet-core.

The following commit(s) were added to refs/heads/master by this push:
     new defc393d add missing file
defc393d is described below

commit defc393d6e320f6cc76df059a94936a7b88571a1
Author: Florian Dold <florian@dold.me>
AuthorDate: Thu Aug 19 13:56:00 2021 +0200

    add missing file
---
 packages/taler-wallet-core/src/db-utils.ts | 154 +++++++++++++++++++++++++++++
 1 file changed, 154 insertions(+)

diff --git a/packages/taler-wallet-core/src/db-utils.ts 
b/packages/taler-wallet-core/src/db-utils.ts
new file mode 100644
index 00000000..424d12b8
--- /dev/null
+++ b/packages/taler-wallet-core/src/db-utils.ts
@@ -0,0 +1,154 @@
+/*
+ This file is part of GNU Taler
+ (C) 2021 Taler Systems S.A.
+
+ GNU Taler is free software; you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with
+ GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+
+/**
+ * Imports.
+ */
+import { IDBDatabase, IDBFactory, IDBTransaction } from 
"@gnu-taler/idb-bridge";
+import { Logger } from "@gnu-taler/taler-util";
+import {
+  CURRENT_DB_CONFIG_KEY,
+  TALER_DB_NAME,
+  TALER_META_DB_NAME,
+  walletMetadataStore,
+  WalletStoresV1,
+  WALLET_DB_MINOR_VERSION,
+} from "./db.js";
+import {
+  DbAccess,
+  IndexDescriptor,
+  openDatabase,
+  StoreDescriptor,
+  StoreWithIndexes,
+} from "./util/query.js";
+
+const logger = new Logger("db-utils.ts");
+
+function upgradeFromStoreMap(
+  storeMap: any,
+  db: IDBDatabase,
+  oldVersion: number,
+  newVersion: number,
+  upgradeTransaction: IDBTransaction,
+): void {
+  if (oldVersion === 0) {
+    for (const n in storeMap) {
+      const swi: StoreWithIndexes<StoreDescriptor<unknown>, any> = storeMap[n];
+      const storeDesc: StoreDescriptor<unknown> = swi.store;
+      const s = db.createObjectStore(storeDesc.name, {
+        autoIncrement: storeDesc.autoIncrement,
+        keyPath: storeDesc.keyPath,
+      });
+      for (const indexName in swi.indexMap as any) {
+        const indexDesc: IndexDescriptor = swi.indexMap[indexName];
+        s.createIndex(indexDesc.name, indexDesc.keyPath, {
+          multiEntry: indexDesc.multiEntry,
+        });
+      }
+    }
+    return;
+  }
+  if (oldVersion === newVersion) {
+    return;
+  }
+  logger.info(`upgrading database from ${oldVersion} to ${newVersion}`);
+  throw Error("upgrade not supported");
+}
+
+function onTalerDbUpgradeNeeded(
+  db: IDBDatabase,
+  oldVersion: number,
+  newVersion: number,
+  upgradeTransaction: IDBTransaction,
+) {
+  upgradeFromStoreMap(
+    WalletStoresV1,
+    db,
+    oldVersion,
+    newVersion,
+    upgradeTransaction,
+  );
+}
+
+function onMetaDbUpgradeNeeded(
+  db: IDBDatabase,
+  oldVersion: number,
+  newVersion: number,
+  upgradeTransaction: IDBTransaction,
+) {
+  upgradeFromStoreMap(
+    walletMetadataStore,
+    db,
+    oldVersion,
+    newVersion,
+    upgradeTransaction,
+  );
+}
+
+/**
+ * Return a promise that resolves
+ * to the taler wallet db.
+ */
+export async function openTalerDatabase(
+  idbFactory: IDBFactory,
+  onVersionChange: () => void,
+): Promise<DbAccess<typeof WalletStoresV1>> {
+  const metaDbHandle = await openDatabase(
+    idbFactory,
+    TALER_META_DB_NAME,
+    1,
+    () => {},
+    onMetaDbUpgradeNeeded,
+  );
+
+  const metaDb = new DbAccess(metaDbHandle, walletMetadataStore);
+  let currentMainVersion: string | undefined;
+  await metaDb
+    .mktx((x) => ({
+      metaConfig: x.metaConfig,
+    }))
+    .runReadWrite(async (tx) => {
+      const dbVersionRecord = await tx.metaConfig.get(CURRENT_DB_CONFIG_KEY);
+      if (!dbVersionRecord) {
+        currentMainVersion = TALER_DB_NAME;
+        await tx.metaConfig.put({
+          key: CURRENT_DB_CONFIG_KEY,
+          value: TALER_DB_NAME,
+        });
+      } else {
+        currentMainVersion = dbVersionRecord.value;
+      }
+    });
+
+  if (currentMainVersion !== TALER_DB_NAME) {
+    // In the future, the migration logic will be implemented here.
+    throw Error(`migration from database ${currentMainVersion} not supported`);
+  }
+
+  const mainDbHandle = await openDatabase(
+    idbFactory,
+    TALER_DB_NAME,
+    WALLET_DB_MINOR_VERSION,
+    onVersionChange,
+    onTalerDbUpgradeNeeded,
+  );
+
+  return new DbAccess(mainDbHandle, WalletStoresV1);
+}
+
+export function deleteTalerDatabase(idbFactory: IDBFactory): void {
+  idbFactory.deleteDatabase(TALER_DB_NAME);
+}

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]