Ghost/core/server/data/migration/fixtures/update.js
Hannah Wolfe 2cfee3812f Split migrations/index.js & add tests
refs #6301

- changes createTable to use createTableIfNotExists, this is consistent with deletion
- splits out backup, reset, update and populate functions from migration/index into their own files
- moves the wrapped function for populatingDefaultSettings to fixtures.ensureDefaultSettings
- moves `modelOptions` down to the fixture files that actually use it
- adds test coverage for backup, reset and populate, but not for update as that needs refactoring
2016-03-15 10:11:33 +00:00

65 lines
1.5 KiB
JavaScript

// # Update Fixtures
// This module handles updating fixtures.
// This is done manually, through a series of files stored in an adjacent folder
// E.g. if we update to version 004, all the tasks in /004/ are executed
var sequence = require('../../../utils/sequence'),
// Private
getVersionTasks,
modelOptions = {context: {internal: true}},
// Public
update;
/**
* ### Get Version Tasks
* Tries to require a directory matching the version number
*
* This was split from update to make testing easier
*
* @param {String} version
* @param {Function} logInfo
* @returns {Array}
*/
getVersionTasks = function getVersionTasks(version, logInfo) {
var tasks = [];
try {
tasks = require('./' + version);
} catch (e) {
logInfo('No fixture updates found for version', version);
}
return tasks;
};
/**
* ## Update
* Handles doing subsequent updates for versions
*
* @param {Array} versions
* @param {Function} logInfo
* @returns {Promise<*>}
*/
update = function update(versions, logInfo) {
var ops = [];
logInfo('Updating fixtures');
versions.forEach(function (version) {
var tasks = getVersionTasks(version, logInfo);
if (tasks && tasks.length > 0) {
ops.push(function () {
logInfo('Updating fixtures to', version);
return sequence(require('./' + version), modelOptions, logInfo);
});
}
});
return sequence(ops, modelOptions, logInfo);
};
module.exports = update;