Update migration script

This commit is contained in:
syuilo 2018-03-31 21:54:29 +09:00
parent ae58046265
commit 621c36c42b
2 changed files with 74 additions and 10 deletions

View file

@ -1,16 +1,40 @@
// for Node.js interpretation // for Node.js interpret
const Message = require('../../../built/models/messaging-message').default; const { default: Post } = require('../../../built/api/models/post');
const Post = require('../../../built/models/post').default; const { default: zip } = require('@prezzemolo/zip')
const html = require('../../../built/common/text/html').default; const html = require('../../../built/common/text/html').default;
const parse = require('../../../built/common/text/parse').default; const parse = require('../../../built/common/text/parse').default;
Promise.all([Message, Post].map(async model => { const migrate = async (post) => {
const documents = await model.find(); const result = await Post.update(post._id, {
return Promise.all(documents.map(({ _id, text }) => model.update(_id, {
$set: { $set: {
textHtml: html(parse(text)) textHtml: post.text ? html(parse(post.text)) : null
} }
}))); });
})).catch(console.error).then(process.exit); return result.ok === 1;
}
async function main() {
const count = await Post.count({});
const dop = Number.parseInt(process.argv[2]) || 5
const idop = ((count - (count % dop)) / dop) + 1
return zip(
1,
async (time) => {
console.log(`${time} / ${idop}`)
const doc = await Post.find({}, {
limit: dop, skip: time * dop
})
return Promise.all(doc.map(migrate))
},
idop
).then(a => {
const rv = []
a.forEach(e => rv.push(...e))
return rv
})
}
main().then(console.dir).catch(console.error)

View file

@ -0,0 +1,40 @@
// for Node.js interpret
const { default: Message } = require('../../../built/api/models/message');
const { default: zip } = require('@prezzemolo/zip')
const html = require('../../../built/common/text/html').default;
const parse = require('../../../built/common/text/parse').default;
const migrate = async (message) => {
const result = await Message.update(message._id, {
$set: {
textHtml: message.text ? html(parse(message.text)) : null
}
});
return result.ok === 1;
}
async function main() {
const count = await Message.count({});
const dop = Number.parseInt(process.argv[2]) || 5
const idop = ((count - (count % dop)) / dop) + 1
return zip(
1,
async (time) => {
console.log(`${time} / ${idop}`)
const doc = await Message.find({}, {
limit: dop, skip: time * dop
})
return Promise.all(doc.map(migrate))
},
idop
).then(a => {
const rv = []
a.forEach(e => rv.push(...e))
return rv
})
}
main().then(console.dir).catch(console.error)