summaryrefslogtreecommitdiff
path: root/tools/migration
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-12-22 05:41:21 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-12-22 05:41:21 +0900
commit11e05a3a3298ea072f24ece0ad7a5c8c00bb1b23 (patch)
tree0a9ab52c9ed9e1b252f6f50892a1cd91f9d36a92 /tools/migration
parentwip (diff)
downloadsharkey-11e05a3a3298ea072f24ece0ad7a5c8c00bb1b23.tar.gz
sharkey-11e05a3a3298ea072f24ece0ad7a5c8c00bb1b23.tar.bz2
sharkey-11e05a3a3298ea072f24ece0ad7a5c8c00bb1b23.zip
wip
Diffstat (limited to 'tools/migration')
-rw-r--r--tools/migration/node.2017-12-22.hiseikika.js67
1 files changed, 67 insertions, 0 deletions
diff --git a/tools/migration/node.2017-12-22.hiseikika.js b/tools/migration/node.2017-12-22.hiseikika.js
new file mode 100644
index 0000000000..ff8294c8d1
--- /dev/null
+++ b/tools/migration/node.2017-12-22.hiseikika.js
@@ -0,0 +1,67 @@
+// for Node.js interpret
+
+const { default: Post } = require('../../built/api/models/post')
+const { default: zip } = require('@prezzemolo/zip')
+
+const migrate = async (post) => {
+ const x = {};
+ if (post.reply_id != null) {
+ const reply = await Post.findOne({
+ _id: post.reply_id
+ });
+ x['_reply.user_id'] = reply.user_id;
+ }
+ if (post.repost_id != null) {
+ const repost = await Post.findOne({
+ _id: post.repost_id
+ });
+ x['_repost.user_id'] = repost.user_id;
+ }
+ if (post.reply_id != null || post.repost_id != null) {
+ const result = await Post.update(post._id, {
+ $set: x,
+ });
+ return result.ok === 1;
+ } else {
+ return true;
+ }
+}
+
+async function main() {
+ const query = {
+ $or: [{
+ reply_id: {
+ $exists: true,
+ $ne: null
+ }
+ }, {
+ repost_id: {
+ $exists: true,
+ $ne: null
+ }
+ }]
+ }
+
+ const count = await Post.count(query);
+
+ 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(query, {
+ 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)