From 07e0f39b558257887cf61043f54358485f394a45 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 7 Apr 2015 17:15:38 +0200 Subject: [PATCH] Add update script for DB version 6 --- src/js/app-config.js | 2 +- src/js/util/update/update-handler.js | 5 ++- src/js/util/update/update-v6.js | 18 +++++++++ test/unit/util/update-handler-test.js | 53 +++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/js/util/update/update-v6.js diff --git a/src/js/app-config.js b/src/js/app-config.js index 2a1ff2d..45eabf7 100644 --- a/src/js/app-config.js +++ b/src/js/app-config.js @@ -42,7 +42,7 @@ appCfg.config = { iconPath: '/img/icon-128-chrome.png', verificationUrl: '/verify/', verificationUuidLength: 36, - dbVersion: 5, + dbVersion: 6, appVersion: undefined, outboxMailboxPath: 'OUTBOX', outboxMailboxName: 'Outbox', diff --git a/src/js/util/update/update-handler.js b/src/js/util/update/update-handler.js index 20c86f3..1262b6e 100644 --- a/src/js/util/update/update-handler.js +++ b/src/js/util/update/update-handler.js @@ -10,7 +10,8 @@ var axe = require('axe-logger'), updateV2 = require('./update-v2'), updateV3 = require('./update-v3'), updateV4 = require('./update-v4'), - updateV5 = require('./update-v5'); + updateV5 = require('./update-v5'), + updateV6 = require('./update-v6'); /** * Handles database migration @@ -18,7 +19,7 @@ var axe = require('axe-logger'), function UpdateHandler(appConfigStore, accountStore, auth, dialog) { this._appConfigStorage = appConfigStore; this._userStorage = accountStore; - this._updateScripts = [updateV1, updateV2, updateV3, updateV4, updateV5]; + this._updateScripts = [updateV1, updateV2, updateV3, updateV4, updateV5, updateV6]; this._auth = auth; this._dialog = dialog; } diff --git a/src/js/util/update/update-v6.js b/src/js/util/update/update-v6.js new file mode 100644 index 0000000..47e80cb --- /dev/null +++ b/src/js/util/update/update-v6.js @@ -0,0 +1,18 @@ +'use strict'; + +/** + * Update handler for transition database version 5 -> 6 + */ +function update(options) { + var emailDbType = 'email_', + versionDbType = 'dbVersion', + postUpdateDbVersion = 6; + + // remove the emails + return options.userStorage.removeList(emailDbType).then(function() { + // update the database version to postUpdateDbVersion + return options.appConfigStorage.storeList([postUpdateDbVersion], versionDbType); + }); +} + +module.exports = update; \ No newline at end of file diff --git a/test/unit/util/update-handler-test.js b/test/unit/util/update-handler-test.js index bac9973..a6fb2fd 100644 --- a/test/unit/util/update-handler-test.js +++ b/test/unit/util/update-handler-test.js @@ -468,5 +468,58 @@ describe('UpdateHandler', function() { }); }); }); + + describe('v5 -> v6', function() { + var emailDbType = 'email_'; + + beforeEach(function() { + cfg.dbVersion = 6; // app requires database version 6 + appConfigStorageStub.listItems.withArgs(versionDbType).returns(resolves([5])); // database version is 5 + }); + + afterEach(function() { + // database version is only queried for version checking prior to the update script + // so no need to check this in case-specific tests + expect(appConfigStorageStub.listItems.calledOnce).to.be.true; + }); + + it('should work', function(done) { + userStorageStub.removeList.withArgs(emailDbType).returns(resolves()); + appConfigStorageStub.storeList.withArgs([6], versionDbType).returns(resolves()); + + updateHandler.update().then(function() { + expect(userStorageStub.removeList.calledOnce).to.be.true; + expect(appConfigStorageStub.storeList.calledOnce).to.be.true; + + done(); + }); + }); + + it('should fail when persisting database version fails', function(done) { + userStorageStub.removeList.returns(resolves()); + appConfigStorageStub.storeList.returns(rejects(new Error())); + + updateHandler.update().catch(function(error) { + expect(error).to.exist; + expect(userStorageStub.removeList.calledOnce).to.be.true; + expect(appConfigStorageStub.storeList.calledOnce).to.be.true; + + done(); + }); + }); + + it('should fail when wiping emails from database fails', function(done) { + userStorageStub.removeList.returns(rejects(new Error())); + + updateHandler.update().catch(function(error) { + expect(error).to.exist; + expect(userStorageStub.removeList.calledOnce).to.be.true; + expect(appConfigStorageStub.storeList.called).to.be.false; + + done(); + }); + }); + }); + }); }); \ No newline at end of file