2016-03-13 23:49:30 +03:00
|
|
|
// # Update Database
|
|
|
|
// Handles migrating a database between two different database versions
|
|
|
|
var _ = require('lodash'),
|
|
|
|
backup = require('./backup'),
|
|
|
|
fixtures = require('./fixtures'),
|
|
|
|
sequence = require('../../utils/sequence'),
|
|
|
|
versioning = require('../schema').versioning,
|
|
|
|
|
|
|
|
updateDatabaseSchema,
|
2016-03-14 20:39:00 +03:00
|
|
|
|
|
|
|
// Public
|
2016-03-13 23:49:30 +03:00
|
|
|
update;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Update Database Schema
|
2016-03-14 20:39:00 +03:00
|
|
|
* Fetch the update tasks for each version, and iterate through them in order
|
2016-03-13 23:49:30 +03:00
|
|
|
*
|
2016-03-14 20:39:00 +03:00
|
|
|
* @param {Array} versions
|
2016-03-13 23:49:30 +03:00
|
|
|
* @param {Function} logInfo
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2016-03-14 20:39:00 +03:00
|
|
|
updateDatabaseSchema = function updateDatabaseSchema(versions, logInfo) {
|
|
|
|
var migrateOps = versions.reduce(function updateToVersion(migrateOps, version) {
|
|
|
|
var tasks = versioning.getUpdateDatabaseTasks(version, logInfo);
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
if (tasks && tasks.length > 0) {
|
|
|
|
migrateOps.push(function runVersionTasks() {
|
|
|
|
logInfo('Updating database to ', version);
|
|
|
|
return sequence(tasks, logInfo);
|
|
|
|
});
|
2016-03-13 23:49:30 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
return migrateOps;
|
|
|
|
}, []);
|
2016-03-13 23:49:30 +03:00
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
// execute the commands in sequence
|
|
|
|
if (!_.isEmpty(migrateOps)) {
|
|
|
|
logInfo('Running migrations');
|
|
|
|
}
|
|
|
|
|
|
|
|
return sequence(migrateOps, logInfo);
|
2016-03-13 23:49:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Update
|
|
|
|
* Does a backup, then updates the database and fixtures
|
|
|
|
*
|
|
|
|
* @param {String} fromVersion
|
|
|
|
* @param {String} toVersion
|
|
|
|
* @param {Function} logInfo
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
update = function update(fromVersion, toVersion, logInfo) {
|
|
|
|
// Is the current version lower than the version we can migrate from?
|
|
|
|
// E.g. is this blog's DB older than 003?
|
|
|
|
if (fromVersion < versioning.canMigrateFromVersion) {
|
|
|
|
return versioning.showCannotMigrateError();
|
|
|
|
}
|
|
|
|
|
2016-03-14 20:39:00 +03:00
|
|
|
fromVersion = process.env.FORCE_MIGRATION ? versioning.canMigrateFromVersion : fromVersion;
|
|
|
|
|
|
|
|
// Figure out which versions we're updating through.
|
|
|
|
// This shouldn't include the from/current version (which we're already on)
|
|
|
|
var versionsToUpdate = versioning.getMigrationVersions(fromVersion, toVersion).slice(1);
|
|
|
|
|
2016-03-13 23:49:30 +03:00
|
|
|
return backup(logInfo).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
return updateDatabaseSchema(versionsToUpdate, logInfo);
|
2016-03-13 23:49:30 +03:00
|
|
|
}).then(function () {
|
|
|
|
// Ensure all of the current default settings are created (these are fixtures, so should be inserted first)
|
|
|
|
return fixtures.ensureDefaultSettings(logInfo);
|
|
|
|
}).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
// Next, run any updates to the fixtures, including default settings, that are required
|
|
|
|
return fixtures.update(versionsToUpdate, logInfo);
|
2016-03-13 23:49:30 +03:00
|
|
|
}).then(function () {
|
2016-03-14 20:39:00 +03:00
|
|
|
// Finally update the database's current version
|
2016-03-13 23:49:30 +03:00
|
|
|
return versioning.setDatabaseVersion();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = update;
|