From 8cd9bb243efb8b2a9c77c593338e7e0f44a04309 Mon Sep 17 00:00:00 2001 From: portree_kid Date: Thu, 5 Mar 2020 16:50:59 +0100 Subject: [PATCH] Migrate Database --- src/renderer/store/api/airports.js | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/renderer/store/api/airports.js b/src/renderer/store/api/airports.js index da46d22..14dad62 100644 --- a/src/renderer/store/api/airports.js +++ b/src/renderer/store/api/airports.js @@ -24,9 +24,30 @@ export default { }; request.onupgradeneeded = e => { - console.log('onupgradeneeded'); - let db = e.target.result; - db.createObjectStore("airports", { autoIncrement: true, keyPath:'properties.icao' }); + var db = event.target.result; + console.log("Migrate " + event); + if (event.oldVersion < 1) { + // Version 1 is the first version of the database. + var objectStore = db.createObjectStore("airports", { keyPath: "properties.icao" }); + } + if (event.oldVersion < 2) { + // Version 2 is the first version of the database. + var objectStore = event.target.transaction.objectStore("airports"); + var indexNames = objectStore.indexNames; + var desiredKeyPathForMyIndex = "properties.icao"; + console.log(indexNames); + + if(indexNames.contains('icaoIndex')) { + var myIndex = objectStore.index('icaoIndex'); + var currentKeyPath = myIndex.keyPath; + if(currentKeyPath != desiredKeyPathForMyIndex) { + objectStore.deleteIndex('icaoIndex'); + objectStore.createIndex('icaoIndex', desiredKeyPathForMyIndex); + } + } else { + objectStore.createIndex('icaoIndex', desiredKeyPathForMyIndex); + } + } }; }); },