Skip to content

Commit

Permalink
Implement extension database upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
calpaterson committed Sep 14, 2020
1 parent 818aea9 commit d906d01
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/extension/src/quarchive-background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,31 @@ function disableListeners() {
}
}

function clearVersion2IDBSchema (db: IDBDatabase): void {
console.log("clearing idb schema from version 2");
try {
db.deleteObjectStore("bookmarks");
} catch (e) {
if (e.name === "NotFoundError"){
// It wasn't there - fine
console.log("no previous object store existed");
} else {
// Something else went wrong - can't continue
console.error("got exception when trying to delete previous object store")
throw e;
}
}
}

function createIDBSchema(db: IDBDatabase) : void {
console.log("creating idb schema for version %d", SCHEMA_VERSION);
var objectStore = db.createObjectStore("bookmarks", {keyPath: "idbKey"});
objectStore.createIndex("browserId", "browserId", {unique: true});
objectStore.transaction.oncomplete = function(event){
console.log("upgrade transaction complete: %o", event);
}
}

// De facto Main method
if (typeof window !== 'undefined') {
console.log("starting quarchive load");
Expand All @@ -456,12 +481,14 @@ if (typeof window !== 'undefined') {
console.log("upgrade needed: %o", event);
let target = <IDBOpenDBRequest> event.target;
var db = target.result;
db.deleteObjectStore("bookmarks");
var objectStore = db.createObjectStore("bookmarks", {keyPath: "idbKey"});
objectStore.createIndex("browserId", "browserId", {unique: true});
objectStore.transaction.oncomplete = function(event){
console.log("upgrade transaction complete: %o", event);
const oldVersion = event.oldVersion;

console.log("running upgrade %d -> %d", oldVersion, db.version);
if (oldVersion < 3) {
clearVersion2IDBSchema(db);
}
createIDBSchema(db);

}
dbOpenRequest.onsuccess = function(event){
console.log("opened database: %o, %o", event, dbOpenRequest.result);
Expand Down

0 comments on commit d906d01

Please sign in to comment.