From 621c36c42be3b2d9cbea525e4929f2155c28c5ab Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 31 Mar 2018 21:54:29 +0900 Subject: [PATCH] Update migration script --- tools/migration/nighthike/7.js | 44 ++++++++++++++++++++++++++-------- tools/migration/nighthike/8.js | 40 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 tools/migration/nighthike/8.js diff --git a/tools/migration/nighthike/7.js b/tools/migration/nighthike/7.js index c5055da8b..c8efb8952 100644 --- a/tools/migration/nighthike/7.js +++ b/tools/migration/nighthike/7.js @@ -1,16 +1,40 @@ -// for Node.js interpretation +// for Node.js interpret -const Message = require('../../../built/models/messaging-message').default; -const Post = require('../../../built/models/post').default; +const { default: Post } = require('../../../built/api/models/post'); +const { default: zip } = require('@prezzemolo/zip') const html = require('../../../built/common/text/html').default; const parse = require('../../../built/common/text/parse').default; -Promise.all([Message, Post].map(async model => { - const documents = await model.find(); - - return Promise.all(documents.map(({ _id, text }) => model.update(_id, { +const migrate = async (post) => { + const result = await Post.update(post._id, { $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) diff --git a/tools/migration/nighthike/8.js b/tools/migration/nighthike/8.js new file mode 100644 index 000000000..5e0cf9507 --- /dev/null +++ b/tools/migration/nighthike/8.js @@ -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)