Skip to content

Commit

Permalink
Don't eager load relations on tag update
Browse files Browse the repository at this point in the history
closes #5810

- switch from using bookshelf's eager loading, to loading separately
- should resolve the TOO MANY SQL VARIABLES error
  • Loading branch information
ErisDS committed Sep 17, 2015
1 parent 07edc94 commit 0aea83f
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions core/server/data/fixtures/index.js
Expand Up @@ -283,27 +283,36 @@ to004 = function to004() {

// Add post_tag order
upgradeOp = function () {
return models.Post.findAll(_.extend({}, options, {withRelated: ['tags']})).then(function (posts) {
var tagOps = [];
var tagOps = [];
logInfo('Collecting data on tag order for posts...');
return models.Post.findAll(_.extend({}, options)).then(function (posts) {
if (posts) {
posts.each(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 (post) {
return post.load(['tags']);
});
}
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;
});
});

if (tagOps.length > 0) {
logInfo('Updating order on ' + tagOps.length + ' tags');
return sequence(tagOps);
logInfo('Updating order on ' + tagOps.length + ' tag relationships (could take a while)...');
return sequence(tagOps).then(function () {
logInfo('Tag order successfully updated');
});
}
});
};
Expand Down

0 comments on commit 0aea83f

Please sign in to comment.