Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure fixture operations only run if needed
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
Showing
5 changed files
with
400 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 54 additions & 31 deletions
85
core/server/data/migration/fixtures/004/07-add-post-tag-order.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
} | ||
}); | ||
}; |
Oops, something went wrong.