Make migrations more robust

Occasionally these will fail if they happen to be executed before the
necessary dependencies (storage, ConversationCollection) are declared.

// FREEBIE
This commit is contained in:
lilia 2016-02-12 17:18:36 -08:00
parent 48626ceafb
commit b8602a3b42
2 changed files with 47 additions and 29 deletions

View file

@ -68,6 +68,7 @@
storage.fetch(); storage.fetch();
storage.onready(function() { storage.onready(function() {
window.dispatchEvent(new Event('storage_ready'));
setUnreadCount(storage.get("unreadCount", 0)); setUnreadCount(storage.get("unreadCount", 0));
if (textsecure.registration.isDone()) { if (textsecure.registration.isDone()) {

View file

@ -14,6 +14,7 @@
version: "1.0", version: "1.0",
migrate: function(transaction, next) { migrate: function(transaction, next) {
console.log('migration 1.0'); console.log('migration 1.0');
console.log('creating object stores');
var messages = transaction.db.createObjectStore("messages"); var messages = transaction.db.createObjectStore("messages");
messages.createIndex("conversation", ["conversationId", "received_at"], { unique: false }); messages.createIndex("conversation", ["conversationId", "received_at"], { unique: false });
messages.createIndex("receipt", "sent_at", { unique: false }); messages.createIndex("receipt", "sent_at", { unique: false });
@ -42,6 +43,8 @@
var conversations = transaction.objectStore("conversations"); var conversations = transaction.objectStore("conversations");
conversations.createIndex("search", "tokens", { unique: false, multiEntry: true }); conversations.createIndex("search", "tokens", { unique: false, multiEntry: true });
window.addEventListener('storage_ready', function() {
console.log('migrating search tokens');
var all = new Whisper.ConversationCollection(); var all = new Whisper.ConversationCollection();
all.fetch().then(function() { all.fetch().then(function() {
all.each(function(model) { all.each(function(model) {
@ -49,6 +52,7 @@
model.save(); model.save();
}); });
}); });
});
next(); next();
} }
}, },
@ -58,6 +62,8 @@
console.log('migration 3.0'); console.log('migration 3.0');
var conversations = transaction.objectStore("items"); var conversations = transaction.objectStore("items");
window.addEventListener('storage_ready', function() {
console.log('migrating unread count');
var all = new Whisper.ConversationCollection(); var all = new Whisper.ConversationCollection();
all.fetch().then(function() { all.fetch().then(function() {
var unreadCount = all.reduce(function(total, model) { var unreadCount = all.reduce(function(total, model) {
@ -70,6 +76,7 @@
storage.remove('unreadCount'); storage.remove('unreadCount');
storage.put('unreadCount', unreadCount); storage.put('unreadCount', unreadCount);
}); });
});
next(); next();
} }
}, },
@ -77,6 +84,8 @@
version: "4.0", version: "4.0",
migrate: function(transaction, next) { migrate: function(transaction, next) {
console.log('migration 4.0'); console.log('migration 4.0');
window.addEventListener('storage_ready', function() {
console.log('migrating search tokens');
var all = new Whisper.ConversationCollection(); var all = new Whisper.ConversationCollection();
all.fetch().then(function() { all.fetch().then(function() {
all.each(function(c) { all.each(function(c) {
@ -84,6 +93,7 @@
c.save(); c.save();
}); });
}); });
});
next(); next();
} }
}, },
@ -91,9 +101,12 @@
version: "5.0", version: "5.0",
migrate: function(transaction, next) { migrate: function(transaction, next) {
console.log('migration 5.0'); console.log('migration 5.0');
window.addEventListener('storage_ready', function() {
console.log('migrating registration flags');
if (storage.get("chromiumRegistrationDone") === "") { if (storage.get("chromiumRegistrationDone") === "") {
storage.put("chromiumRegistrationDoneEver", ""); storage.put("chromiumRegistrationDoneEver", "");
} }
});
next(); next();
} }
}, },
@ -101,12 +114,15 @@
version: "6.0", version: "6.0",
migrate: function(transaction, next) { migrate: function(transaction, next) {
console.log('migration 6.0'); console.log('migration 6.0');
window.addEventListener('storage_ready', function() {
console.log('migrating registration flags');
storage.onready(function() { storage.onready(function() {
if (storage.get("chromiumRegistrationDone") === "") { if (storage.get("chromiumRegistrationDone") === "") {
storage.put("chromiumRegistrationDoneEver", ""); storage.put("chromiumRegistrationDoneEver", "");
next(); next();
} }
}); });
});
next(); next();
} }
}, },
@ -114,6 +130,7 @@
version: "7.0", version: "7.0",
migrate: function(transaction, next) { migrate: function(transaction, next) {
console.log('migration 7.0'); console.log('migration 7.0');
console.log('creating debug log');
transaction.db.createObjectStore("debug"); transaction.db.createObjectStore("debug");
next(); next();
} }