mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Speed up 1.22 migration script (#9541)
no issue - insert `posts_authors` relation via knex - massive speed improvement e.g. 1500 posts - before: ~1min - after: ~10sec
This commit is contained in:
parent
3e33849e47
commit
7bbde460af
1 changed files with 13 additions and 13 deletions
|
@ -2,6 +2,7 @@
|
|||
|
||||
const _ = require('lodash'),
|
||||
Promise = require('bluebird'),
|
||||
ObjectId = require('bson-objectid'),
|
||||
common = require('../../../../lib/common'),
|
||||
models = require('../../../../models');
|
||||
|
||||
|
@ -24,25 +25,24 @@ module.exports.up = function handleMultipleAuthors(options) {
|
|||
common.logging.info('Adding `posts_authors` relations');
|
||||
|
||||
return Promise.map(posts.models, function (post) {
|
||||
let authorIdToSet;
|
||||
|
||||
// CASE: ensure `post.author_id` is a valid user id
|
||||
return models.User.findOne({id: post.get('author_id')}, _.merge({columns: userColumns}, localOptions))
|
||||
.then(function (user) {
|
||||
if (!user) {
|
||||
authorIdToSet = ownerUser.id;
|
||||
} else {
|
||||
authorIdToSet = post.get('author_id');
|
||||
return models.Post.edit({
|
||||
author_id: ownerUser.id
|
||||
}, _.merge({id: post.id}, localOptions));
|
||||
}
|
||||
|
||||
return post;
|
||||
})
|
||||
.then(function () {
|
||||
// CASE: insert primary author
|
||||
return models.Post.edit({
|
||||
author_id: authorIdToSet,
|
||||
authors: [{
|
||||
id: post.get('author_id')
|
||||
}]
|
||||
}, _.merge({id: post.id}, localOptions));
|
||||
.then(function (post) {
|
||||
return options.transacting('posts_authors').insert({
|
||||
id: ObjectId.generate(),
|
||||
post_id: post.id,
|
||||
author_id: post.get('author_id'),
|
||||
sort_order: 0
|
||||
});
|
||||
});
|
||||
}, {concurrency: 100});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue