From b8602a3b428db1509ba434c595ef3bd3c5725dc2 Mon Sep 17 00:00:00 2001 From: lilia Date: Fri, 12 Feb 2016 17:18:36 -0800 Subject: [PATCH] Make migrations more robust Occasionally these will fail if they happen to be executed before the necessary dependencies (storage, ConversationCollection) are declared. // FREEBIE --- js/background.js | 1 + js/database.js | 75 +++++++++++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/js/background.js b/js/background.js index 52a5e009..0e145ffc 100644 --- a/js/background.js +++ b/js/background.js @@ -68,6 +68,7 @@ storage.fetch(); storage.onready(function() { + window.dispatchEvent(new Event('storage_ready')); setUnreadCount(storage.get("unreadCount", 0)); if (textsecure.registration.isDone()) { diff --git a/js/database.js b/js/database.js index 36c10bed..eb720b89 100644 --- a/js/database.js +++ b/js/database.js @@ -14,6 +14,7 @@ version: "1.0", migrate: function(transaction, next) { console.log('migration 1.0'); + console.log('creating object stores'); var messages = transaction.db.createObjectStore("messages"); messages.createIndex("conversation", ["conversationId", "received_at"], { unique: false }); messages.createIndex("receipt", "sent_at", { unique: false }); @@ -42,11 +43,14 @@ var conversations = transaction.objectStore("conversations"); conversations.createIndex("search", "tokens", { unique: false, multiEntry: true }); - var all = new Whisper.ConversationCollection(); - all.fetch().then(function() { - all.each(function(model) { - model.updateTokens(); - model.save(); + window.addEventListener('storage_ready', function() { + console.log('migrating search tokens'); + var all = new Whisper.ConversationCollection(); + all.fetch().then(function() { + all.each(function(model) { + model.updateTokens(); + model.save(); + }); }); }); next(); @@ -58,17 +62,20 @@ console.log('migration 3.0'); var conversations = transaction.objectStore("items"); - var all = new Whisper.ConversationCollection(); - all.fetch().then(function() { - var unreadCount = all.reduce(function(total, model) { - var count = model.get('unreadCount'); - if (count === undefined) { - count = 0; - } - return total + count; - }, 0); - storage.remove('unreadCount'); - storage.put('unreadCount', unreadCount); + window.addEventListener('storage_ready', function() { + console.log('migrating unread count'); + var all = new Whisper.ConversationCollection(); + all.fetch().then(function() { + var unreadCount = all.reduce(function(total, model) { + var count = model.get('unreadCount'); + if (count === undefined) { + count = 0; + } + return total + count; + }, 0); + storage.remove('unreadCount'); + storage.put('unreadCount', unreadCount); + }); }); next(); } @@ -77,11 +84,14 @@ version: "4.0", migrate: function(transaction, next) { console.log('migration 4.0'); - var all = new Whisper.ConversationCollection(); - all.fetch().then(function() { - all.each(function(c) { - c.updateTokens(); - c.save(); + window.addEventListener('storage_ready', function() { + console.log('migrating search tokens'); + var all = new Whisper.ConversationCollection(); + all.fetch().then(function() { + all.each(function(c) { + c.updateTokens(); + c.save(); + }); }); }); next(); @@ -91,9 +101,12 @@ version: "5.0", migrate: function(transaction, next) { console.log('migration 5.0'); - if (storage.get("chromiumRegistrationDone") === "") { - storage.put("chromiumRegistrationDoneEver", ""); - } + window.addEventListener('storage_ready', function() { + console.log('migrating registration flags'); + if (storage.get("chromiumRegistrationDone") === "") { + storage.put("chromiumRegistrationDoneEver", ""); + } + }); next(); } }, @@ -101,11 +114,14 @@ version: "6.0", migrate: function(transaction, next) { console.log('migration 6.0'); - storage.onready(function() { - if (storage.get("chromiumRegistrationDone") === "") { - storage.put("chromiumRegistrationDoneEver", ""); - next(); - } + window.addEventListener('storage_ready', function() { + console.log('migrating registration flags'); + storage.onready(function() { + if (storage.get("chromiumRegistrationDone") === "") { + storage.put("chromiumRegistrationDoneEver", ""); + next(); + } + }); }); next(); } @@ -114,6 +130,7 @@ version: "7.0", migrate: function(transaction, next) { console.log('migration 7.0'); + console.log('creating debug log'); transaction.db.createObjectStore("debug"); next(); }