From bee36510477b04dee500a9a624584544b9c8bdb2 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin.auder@somewhere>
Date: Thu, 19 Mar 2020 16:47:28 +0100
Subject: [PATCH] Fix DBOpenRequest.onupgradeneeded

---
 client/src/utils/compgameStorage.js |  6 +++++-
 client/src/utils/gameStorage.js     | 17 ++++++++++++-----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/client/src/utils/compgameStorage.js b/client/src/utils/compgameStorage.js
index a5c58af1..a8bcaa2a 100644
--- a/client/src/utils/compgameStorage.js
+++ b/client/src/utils/compgameStorage.js
@@ -27,7 +27,11 @@ function dbOperation(callback) {
 
   DBOpenRequest.onupgradeneeded = function(event) {
     let db = event.target.result;
-    db.createObjectStore("compgames", { keyPath: "vname" });
+    let upgradeTransaction = event.target.transaction;
+    if (!db.objectStoreNames.contains("compgames"))
+      db.createObjectStore("compgames", { keyPath: "vname" });
+    else
+      upgradeTransaction.objectStore("compgames");
   };
 }
 
diff --git a/client/src/utils/gameStorage.js b/client/src/utils/gameStorage.js
index fc179f19..514448b0 100644
--- a/client/src/utils/gameStorage.js
+++ b/client/src/utils/gameStorage.js
@@ -34,11 +34,18 @@ function dbOperation(callback) {
 
   DBOpenRequest.onupgradeneeded = function(event) {
     let db = event.target.result;
-    let objectStore = db.createObjectStore("games", { keyPath: "id" });
-    // To sarch games by score (useful for running games)
-    objectStore.createIndex("score", "score", { unique: false });
-    // To search by date intervals. Two games cannot start at the same time
-    objectStore.createIndex("created", "created", { unique: true });
+    let upgradeTransaction = event.target.transaction;
+    let objectStore = undefined;
+    if (!db.objectStoreNames.contains("games"))
+      objectStore = db.createObjectStore("games", { keyPath: "id" });
+    else
+      objectStore = upgradeTransaction.objectStore("games");
+    if (!objectStore.indexNames.contains("score"))
+      // To sarch games by score (useful for running games)
+      objectStore.createIndex("score", "score", { unique: false });
+    if (!objectStore.indexNames.contains("created"))
+      // To search by date intervals. Two games cannot start at the same time
+      objectStore.createIndex("created", "created", { unique: true });
   };
 }
 
-- 
2.44.0