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,11 +43,14 @@
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 });
var all = new Whisper.ConversationCollection(); window.addEventListener('storage_ready', function() {
all.fetch().then(function() { console.log('migrating search tokens');
all.each(function(model) { var all = new Whisper.ConversationCollection();
model.updateTokens(); all.fetch().then(function() {
model.save(); all.each(function(model) {
model.updateTokens();
model.save();
});
}); });
}); });
next(); next();
@ -58,17 +62,20 @@
console.log('migration 3.0'); console.log('migration 3.0');
var conversations = transaction.objectStore("items"); var conversations = transaction.objectStore("items");
var all = new Whisper.ConversationCollection(); window.addEventListener('storage_ready', function() {
all.fetch().then(function() { console.log('migrating unread count');
var unreadCount = all.reduce(function(total, model) { var all = new Whisper.ConversationCollection();
var count = model.get('unreadCount'); all.fetch().then(function() {
if (count === undefined) { var unreadCount = all.reduce(function(total, model) {
count = 0; var count = model.get('unreadCount');
} if (count === undefined) {
return total + count; count = 0;
}, 0); }
storage.remove('unreadCount'); return total + count;
storage.put('unreadCount', unreadCount); }, 0);
storage.remove('unreadCount');
storage.put('unreadCount', unreadCount);
});
}); });
next(); next();
} }
@ -77,11 +84,14 @@
version: "4.0", version: "4.0",
migrate: function(transaction, next) { migrate: function(transaction, next) {
console.log('migration 4.0'); console.log('migration 4.0');
var all = new Whisper.ConversationCollection(); window.addEventListener('storage_ready', function() {
all.fetch().then(function() { console.log('migrating search tokens');
all.each(function(c) { var all = new Whisper.ConversationCollection();
c.updateTokens(); all.fetch().then(function() {
c.save(); all.each(function(c) {
c.updateTokens();
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');
if (storage.get("chromiumRegistrationDone") === "") { window.addEventListener('storage_ready', function() {
storage.put("chromiumRegistrationDoneEver", ""); console.log('migrating registration flags');
} if (storage.get("chromiumRegistrationDone") === "") {
storage.put("chromiumRegistrationDoneEver", "");
}
});
next(); next();
} }
}, },
@ -101,11 +114,14 @@
version: "6.0", version: "6.0",
migrate: function(transaction, next) { migrate: function(transaction, next) {
console.log('migration 6.0'); console.log('migration 6.0');
storage.onready(function() { window.addEventListener('storage_ready', function() {
if (storage.get("chromiumRegistrationDone") === "") { console.log('migrating registration flags');
storage.put("chromiumRegistrationDoneEver", ""); storage.onready(function() {
next(); if (storage.get("chromiumRegistrationDone") === "") {
} storage.put("chromiumRegistrationDoneEver", "");
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();
} }