2014-07-11 11:12:07 +04:00
|
|
|
// # Fixtures
|
|
|
|
// This module handles populating or updating fixtures.
|
|
|
|
//
|
|
|
|
// Currently fixtures only change between data version 002 and 003, therefore the update logic is hard coded
|
|
|
|
// rather than abstracted into a migration system. The upgrade function checks that its changes are safe before
|
|
|
|
// making them.
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
|
|
|
sequence = require('../../utils/sequence'),
|
2014-03-14 21:36:45 +04:00
|
|
|
_ = require('lodash'),
|
2014-07-10 19:10:00 +04:00
|
|
|
errors = require('../../errors'),
|
2014-07-10 14:00:51 +04:00
|
|
|
utils = require('../../utils'),
|
2014-05-16 06:29:42 +04:00
|
|
|
models = require('../../models'),
|
2014-07-11 11:12:07 +04:00
|
|
|
fixtures = require('./fixtures'),
|
|
|
|
permissions = require('./permissions'),
|
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
// Private
|
|
|
|
logInfo,
|
2014-07-11 11:12:07 +04:00
|
|
|
to003,
|
|
|
|
convertAdminToOwner,
|
2014-07-10 19:10:00 +04:00
|
|
|
createOwner,
|
2014-07-15 15:03:12 +04:00
|
|
|
options = {context: {internal: true}},
|
2014-07-10 19:10:00 +04:00
|
|
|
|
|
|
|
// Public
|
|
|
|
populate,
|
|
|
|
update;
|
2014-07-11 11:12:07 +04:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo = function logInfo(message) {
|
2014-07-17 16:26:16 +04:00
|
|
|
errors.logInfo('Migrations', message);
|
2014-07-10 19:10:00 +04:00
|
|
|
};
|
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
/**
|
|
|
|
* Convert admin to Owner
|
|
|
|
* Changes an admin user to have the owner role
|
|
|
|
* @returns {Promise|*}
|
|
|
|
*/
|
|
|
|
convertAdminToOwner = function () {
|
|
|
|
var adminUser;
|
2013-05-17 01:16:09 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
return models.User.findOne({role: 'Administrator'}).then(function (user) {
|
2014-07-11 11:12:07 +04:00
|
|
|
adminUser = user;
|
|
|
|
return models.Role.findOne({name: 'Owner'});
|
|
|
|
}).then(function (ownerRole) {
|
|
|
|
if (adminUser) {
|
2014-07-17 16:26:16 +04:00
|
|
|
logInfo('Converting admin to owner');
|
2014-07-11 11:12:07 +04:00
|
|
|
return adminUser.roles().updatePivot({role_id: ownerRole.id});
|
2013-09-19 08:59:33 +04:00
|
|
|
}
|
2014-07-11 11:12:07 +04:00
|
|
|
});
|
|
|
|
};
|
2013-09-19 08:59:33 +04:00
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
/**
|
|
|
|
* Create Owner
|
|
|
|
* Creates the user fixture and gives it the owner role
|
|
|
|
* @returns {Promise|*}
|
|
|
|
*/
|
|
|
|
createOwner = function () {
|
|
|
|
var user = fixtures.users[0];
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
return models.Role.findOne({name: 'Owner'}).then(function (ownerRole) {
|
2014-07-30 19:40:30 +04:00
|
|
|
user.roles = [ownerRole.id];
|
2014-07-11 11:12:07 +04:00
|
|
|
user.password = utils.uid(50);
|
2014-03-14 21:36:45 +04:00
|
|
|
|
2014-07-17 16:26:16 +04:00
|
|
|
logInfo('Creating owner');
|
2014-07-15 15:03:12 +04:00
|
|
|
return models.User.add(user, options);
|
2014-07-11 11:12:07 +04:00
|
|
|
});
|
2013-09-02 05:49:08 +04:00
|
|
|
};
|
2013-09-15 02:14:39 +04:00
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
populate = function () {
|
2014-03-14 21:36:45 +04:00
|
|
|
var ops = [],
|
2014-05-16 06:29:42 +04:00
|
|
|
relations = [],
|
|
|
|
Post = models.Post,
|
|
|
|
Tag = models.Tag,
|
|
|
|
Role = models.Role,
|
2014-07-11 11:12:07 +04:00
|
|
|
Client = models.Client;
|
2014-03-14 21:36:45 +04:00
|
|
|
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo('Populating fixtures');
|
|
|
|
|
2014-03-14 21:36:45 +04:00
|
|
|
_.each(fixtures.posts, function (post) {
|
2014-07-15 15:03:12 +04:00
|
|
|
ops.push(Post.add(post, options));
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
_.each(fixtures.tags, function (tag) {
|
2014-07-15 15:03:12 +04:00
|
|
|
ops.push(Tag.add(tag, options));
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
_.each(fixtures.roles, function (role) {
|
2014-07-15 15:03:12 +04:00
|
|
|
ops.push(Role.add(role, options));
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
|
|
|
|
2014-07-17 16:26:16 +04:00
|
|
|
_.each(fixtures.clients, function (client) {
|
2014-07-15 15:03:12 +04:00
|
|
|
ops.push(Client.add(client, options));
|
2014-06-30 16:58:10 +04:00
|
|
|
});
|
|
|
|
|
2014-03-14 21:36:45 +04:00
|
|
|
// add the tag to the post
|
|
|
|
relations.push(function () {
|
2014-07-15 15:03:12 +04:00
|
|
|
return Post.forge({slug: fixtures.posts[0].slug}).fetch().then(function (post) {
|
2014-07-11 11:12:07 +04:00
|
|
|
return Tag.forge({slug: fixtures.tags[0].slug}).fetch().then(function (tag) {
|
2014-07-15 15:03:12 +04:00
|
|
|
return post.related('tags').attach(tag.id);
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
2013-09-15 02:14:39 +04:00
|
|
|
});
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(ops).then(function () {
|
2014-07-10 14:00:51 +04:00
|
|
|
return sequence(relations);
|
|
|
|
}).then(function () {
|
2014-07-15 15:03:12 +04:00
|
|
|
return permissions.populate(options);
|
2014-07-11 11:12:07 +04:00
|
|
|
}).then(function () {
|
|
|
|
return createOwner();
|
2014-07-15 15:03:12 +04:00
|
|
|
}).catch(function (errs) {
|
|
|
|
errors.logError(errs);
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
/**
|
|
|
|
* ### Update fixtures to 003
|
|
|
|
* Need to add client & owner role, then update permissions to 003 as well
|
|
|
|
* By doing this in a way that checks before adding, we can ensure that it's possible to force a migration safely.
|
|
|
|
*
|
|
|
|
* Note: At the moment this is pretty adhoc & untestable, in future it would be better to have a config based system.
|
|
|
|
* @returns {Promise|*}
|
|
|
|
*/
|
|
|
|
to003 = function () {
|
2014-03-14 21:36:45 +04:00
|
|
|
var ops = [],
|
2014-07-11 11:12:07 +04:00
|
|
|
upgradeOp,
|
2014-05-16 06:29:42 +04:00
|
|
|
Role = models.Role,
|
2014-07-11 11:12:07 +04:00
|
|
|
Client = models.Client;
|
2014-06-30 16:58:10 +04:00
|
|
|
|
2014-07-15 15:03:12 +04:00
|
|
|
logInfo('Upgrading fixtures');
|
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
// Add the client fixture if missing
|
2014-07-17 16:26:16 +04:00
|
|
|
upgradeOp = Client.findOne({secret: fixtures.clients[0].secret}).then(function (client) {
|
2014-07-11 11:12:07 +04:00
|
|
|
if (!client) {
|
2014-07-17 16:26:16 +04:00
|
|
|
logInfo('Adding client fixture');
|
|
|
|
_.each(fixtures.clients, function (client) {
|
2014-07-15 15:03:12 +04:00
|
|
|
return Client.add(client, options);
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
2014-07-11 11:12:07 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
ops.push(upgradeOp);
|
2013-09-19 08:59:33 +04:00
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
// Add the owner role if missing
|
|
|
|
upgradeOp = Role.findOne({name: fixtures.roles[3].name}).then(function (owner) {
|
|
|
|
if (!owner) {
|
2014-07-17 16:26:16 +04:00
|
|
|
logInfo('Adding owner role fixture');
|
2014-07-11 11:12:07 +04:00
|
|
|
_.each(fixtures.roles.slice(3), function (role) {
|
2014-07-15 15:03:12 +04:00
|
|
|
return Role.add(role, options);
|
2013-09-15 02:14:39 +04:00
|
|
|
});
|
2014-07-11 11:12:07 +04:00
|
|
|
}
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
2014-07-11 11:12:07 +04:00
|
|
|
ops.push(upgradeOp);
|
2013-09-15 02:14:39 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(ops).then(function () {
|
2014-07-15 15:03:12 +04:00
|
|
|
return permissions.to003(options);
|
2014-07-10 14:00:51 +04:00
|
|
|
}).then(function () {
|
2014-07-11 11:12:07 +04:00
|
|
|
return convertAdminToOwner();
|
2014-03-14 21:36:45 +04:00
|
|
|
});
|
2014-02-27 06:44:09 +04:00
|
|
|
};
|
2014-03-14 21:36:45 +04:00
|
|
|
|
2014-07-11 11:12:07 +04:00
|
|
|
update = function (fromVersion, toVersion) {
|
2014-07-10 19:10:00 +04:00
|
|
|
logInfo('Updating fixtures');
|
2014-07-17 16:26:16 +04:00
|
|
|
// Are we migrating to, or past 003?
|
|
|
|
if ((fromVersion < '003' && toVersion >= '003') ||
|
|
|
|
fromVersion === '003' && toVersion === '003' && process.env.FORCE_MIGRATION) {
|
2014-07-11 11:12:07 +04:00
|
|
|
return to003();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-14 21:36:45 +04:00
|
|
|
module.exports = {
|
2014-07-11 11:12:07 +04:00
|
|
|
populate: populate,
|
|
|
|
update: update
|
2014-04-03 17:03:09 +04:00
|
|
|
};
|