Skip to content

Commit

Permalink
Ensure fixture operations only run if needed
Browse files Browse the repository at this point in the history
refs #6301

- Make sure that every fixture operation has a check to ensure that it hasn't already run
- E.g. The update of sort_order on posts_tags should only happen if there are no values which aren't a zero
  - This makes sure that we don't accidentally overwrite data on FORCE_MIGRATE
- No need to try to set settings types if they are already correct
- Only update the admin client if it needs it, else we're regenerating secrets each time
  • Loading branch information
ErisDS committed Mar 21, 2016
1 parent 90dca16 commit 9030620
Show file tree
Hide file tree
Showing 5 changed files with 400 additions and 55 deletions.
Expand Up @@ -4,7 +4,7 @@ var models = require('../../../../models'),

module.exports = function updatePrivateSetting(options, logInfo) {
return models.Settings.findOne('isPrivate').then(function (setting) {
if (setting) {
if (setting && setting.get('type') !== 'private') {
logInfo('Update isPrivate setting');
return models.Settings.edit({key: 'isPrivate', type: 'private'}, options);
}
Expand Down
Expand Up @@ -4,7 +4,7 @@ var models = require('../../../../models'),

module.exports = function updatePasswordSetting(options, logInfo) {
return models.Settings.findOne('password').then(function (setting) {
if (setting) {
if (setting && setting.get('type') !== 'private') {
logInfo('Update password setting');
return models.Settings.edit({key: 'password', type: 'private'}, options);
}
Expand Down
Expand Up @@ -9,7 +9,7 @@ var models = require('../../../../models'),
module.exports = function updateGhostAdminClient(options, logInfo) {
// ghost-admin should already exist from 003 version
return models.Client.findOne({slug: adminClient.slug}).then(function (client) {
if (client) {
if (client && (client.get('secret') === 'not_available' || client.get('status') !== 'enabled')) {
logInfo('Update ghost-admin client fixture');
return models.Client.edit(
_.extend({}, adminClient, {secret: crypto.randomBytes(6).toString('hex')}),
Expand Down
85 changes: 54 additions & 31 deletions core/server/data/migration/fixtures/004/07-add-post-tag-order.js
@@ -1,39 +1,62 @@
// Add a new order value to posts_tags based on the existing info
var models = require('../../../../models'),
_ = require('lodash'),
sequence = require('../../../../utils/sequence');
sequence = require('../../../../utils/sequence'),
migrationHasRunFlag,
modelOptions;

module.exports = function addPostTagOrder(options, logInfo) {
var tagOps = [];
logInfo('Collecting data on tag order for posts...');
return models.Post.findAll(_.extend({}, options)).then(function (posts) {
if (posts) {
return posts.mapThen(function (post) {
return post.load(['tags']);
});
}
function loadTagsForEachPost(posts) {
if (!posts) {
return [];
}).then(function (posts) {
_.each(posts, function (post) {
var order = 0;
post.related('tags').each(function (tag) {
tagOps.push((function (order) {
var sortOrder = order;
return function () {
return post.tags().updatePivot(
{sort_order: sortOrder}, _.extend({}, options, {query: {where: {tag_id: tag.id}}})
);
};
}(order)));
order += 1;
});
});
}
return posts.mapThen(function loadTagsForPost(post) {
return post.load(['tags']);
});
}

function updatePostTagsSortOrder(post, tagId, order) {
var sortOrder = order;
return function doUpdatePivot() {
return post.tags().updatePivot(
{sort_order: sortOrder}, _.extend({}, modelOptions, {query: {where: {tag_id: tagId}}})
);
};
}

if (tagOps.length > 0) {
logInfo('Updating order on ' + tagOps.length + ' tag relationships (could take a while)...');
return sequence(tagOps).then(function () {
logInfo('Tag order successfully updated');
});
function buildTagOpsArray(tagOps, post) {
var order = 0;

return post.related('tags').reduce(function processTag(tagOps, tag) {
if (tag.pivot.get('sort_order') > 0) {
// if any entry in the posts_tags table has already run, we shouldn't run this again
migrationHasRunFlag = true;
}
});

tagOps.push(updatePostTagsSortOrder(post, tag.id, order));
order += 1;

return tagOps;
}, tagOps);
}

function processPostsArray(postsArray) {
return postsArray.reduce(buildTagOpsArray, []);
}

module.exports = function addPostTagOrder(options, logInfo) {
modelOptions = options;
migrationHasRunFlag = false;

logInfo('Collecting data on tag order for posts...');
return models.Post.findAll(_.extend({}, modelOptions))
.then(loadTagsForEachPost)
.then(processPostsArray)
.then(function (tagOps) {
if (tagOps.length > 0 && !migrationHasRunFlag) {
logInfo('Updating order on ' + tagOps.length + ' tag relationships (could take a while)...');
return sequence(tagOps).then(function () {
logInfo('Tag order successfully updated');
});
}
});
};

0 comments on commit 9030620

Please sign in to comment.