/*
 * vim: ts=4:sw=4:expandtab
 */

(function () {
    'use strict';
    window.Whisper             = window.Whisper             || {};
    window.Whisper.Database    = window.Whisper.Database    || {};
    window.Whisper.Database.id = window.Whisper.Database.id || 'signal';
    window.Whisper.Database.nolog = true;

    Whisper.Database.migrations = [
        {
            version: "1.0",
            migrate: function(transaction, next) {
                console.log('migration 1.0');
                var messages = transaction.db.createObjectStore("messages");
                messages.createIndex("conversation", ["conversationId", "received_at"], { unique: false });
                messages.createIndex("receipt", "sent_at", { unique: false });

                var conversations = transaction.db.createObjectStore("conversations");
                conversations.createIndex("inbox", "active_at", { unique: false });
                conversations.createIndex("group", "members", { unique: false, multiEntry: true });
                conversations.createIndex("type", "type", { unique: false });

                var groups = transaction.db.createObjectStore('groups');

                var sessions = transaction.db.createObjectStore('sessions');
                var identityKeys = transaction.db.createObjectStore('identityKeys');

                var preKeys = transaction.db.createObjectStore("preKeys");
                var signedPreKeys = transaction.db.createObjectStore("signedPreKeys");

                var items = transaction.db.createObjectStore("items");
                next();
            }
        },
        {
            version: "2.0",
            migrate: function(transaction, next) {
                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();
                    });
                });
            }
        },
        {
            version: "3.0",
            migrate: function(transaction, next) {
                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);
                });
            }
        },
        {
            version: "4.0",
            migrate: function(transaction, next) {
                var all = new Whisper.ConversationCollection();
                all.fetch().then(function() {
                    all.each(function(c) {
                        c.updateTokens();
                        c.save();
                    });
                });
            }
        },
        {
            version: "5.0",
            migrate: function(transaction, next) {
                if (storage.get("chromiumRegistrationDone") === "") {
                    storage.put("chromiumRegistrationDoneEver", "");
                }
            }
        },
        {
            version: "6.0",
            migrate: function(transaction, next) {
                storage.onready(function() {
                    if (storage.get("chromiumRegistrationDone") === "") {
                        storage.put("chromiumRegistrationDoneEver", "");
                        next();
                    }
                });
                storage.fetch();
            }
        },
        {
            version: "7.0",
            migrate: function(transaction, next) {
                transaction.db.createObjectStore("debug");
            }
        }
    ];
}());

@@ -1699,32 +1699,78 @@
 action, next) %7B%0A
+                console.log('migration 2.0');%0A
                 
@@ -1823,32 +1823,32 @@
 onversations%22);%0A
-
                 
@@ -2209,32 +2209,56 @@
             %7D);%0A
+                next();%0A
             %7D%0A  
@@ -2347,32 +2347,78 @@
 action, next) %7B%0A
+                console.log('migration 3.0');%0A
                 
@@ -3031,32 +3031,56 @@
             %7D);%0A
+                next();%0A
             %7D%0A  
@@ -3169,32 +3169,78 @@
 action, next) %7B%0A
+                console.log('migration 4.0');%0A
                 
@@ -3488,32 +3488,56 @@
             %7D);%0A
+                next();%0A
             %7D%0A  
@@ -3626,32 +3626,78 @@
 action, next) %7B%0A
+                console.log('migration 5.0');%0A
                 
@@ -3829,32 +3829,56 @@
               %7D%0A
+                next();%0A
             %7D%0A  
@@ -3967,32 +3967,78 @@
 action, next) %7B%0A
+                console.log('migration 6.0');%0A
                 
@@ -4316,24 +4316,48 @@
 ge.fetch();%0A
+                next();%0A
             
@@ -4450,32 +4450,78 @@
 action, next) %7B%0A
+                console.log('migration 7.0');%0A
                 
@@ -4559,24 +4559,48 @@
 e(%22debug%22);%0A
+                next();%0A
             

Fix migrations