2014-03-11 11:06:19 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var cfg = require('js/app-config').config,
|
2014-05-06 11:23:08 -04:00
|
|
|
updateV1 = require('js/util/update/update-v1'),
|
2014-05-23 08:23:50 -04:00
|
|
|
updateV2 = require('js/util/update/update-v2'),
|
2014-07-01 13:49:19 -04:00
|
|
|
updateV3 = require('js/util/update/update-v3'),
|
|
|
|
updateV4 = require('js/util/update/update-v4');
|
2014-03-11 11:06:19 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles database migration
|
|
|
|
*/
|
2014-07-01 13:49:19 -04:00
|
|
|
var UpdateHandler = function(appConfigStorage, userStorage, auth) {
|
2014-03-11 11:06:19 -04:00
|
|
|
this._appConfigStorage = appConfigStorage;
|
|
|
|
this._userStorage = userStorage;
|
2014-07-01 13:49:19 -04:00
|
|
|
this._updateScripts = [updateV1, updateV2, updateV3, updateV4];
|
|
|
|
this._auth = auth;
|
2014-03-11 11:06:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes all the necessary updates
|
|
|
|
* @param {Function} callback(error) Invoked when all the database updates were executed, or if an error occurred
|
|
|
|
*/
|
|
|
|
UpdateHandler.prototype.update = function(callback) {
|
|
|
|
var self = this,
|
|
|
|
currentVersion = 0,
|
|
|
|
targetVersion = cfg.dbVersion,
|
|
|
|
versionDbType = 'dbVersion';
|
|
|
|
|
|
|
|
self._appConfigStorage.listItems(versionDbType, 0, null, function(err, items) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the database version number
|
|
|
|
if (items && items.length > 0) {
|
|
|
|
currentVersion = parseInt(items[0], 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
self._applyUpdate({
|
|
|
|
currentVersion: currentVersion,
|
|
|
|
targetVersion: targetVersion
|
|
|
|
}, callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedules necessary updates and executes thom in order
|
|
|
|
*/
|
|
|
|
UpdateHandler.prototype._applyUpdate = function(options, callback) {
|
|
|
|
var self = this,
|
2014-07-01 13:49:19 -04:00
|
|
|
scriptOptions,
|
2014-03-11 11:06:19 -04:00
|
|
|
queue = [];
|
|
|
|
|
|
|
|
if (options.currentVersion >= options.targetVersion) {
|
|
|
|
// the current database version is up to date
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-01 13:49:19 -04:00
|
|
|
scriptOptions = {
|
2014-03-11 11:06:19 -04:00
|
|
|
appConfigStorage: self._appConfigStorage,
|
2014-07-01 13:49:19 -04:00
|
|
|
userStorage: self._userStorage,
|
|
|
|
auth: self._auth
|
2014-03-11 11:06:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// add all the necessary database updates to the queue
|
|
|
|
for (var i = options.currentVersion; i < options.targetVersion; i++) {
|
|
|
|
queue.push(self._updateScripts[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// takes the next update from the queue and executes it
|
|
|
|
function executeNextUpdate(err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (queue.length < 1) {
|
|
|
|
// we're done
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// process next update
|
|
|
|
var script = queue.shift();
|
2014-07-01 13:49:19 -04:00
|
|
|
script(scriptOptions, executeNextUpdate);
|
2014-03-11 11:06:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
executeNextUpdate();
|
|
|
|
};
|
|
|
|
|
|
|
|
return UpdateHandler;
|
|
|
|
});
|