summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-04-02 23:19:07 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-04-02 23:19:07 +0900
commit5ce6fa67d212823be43c796e8632b721ede065bb (patch)
treecb188b5e97741625feb095bfb577d46b91cbacc2 /tools
parentMerge pull request #1374 from akihikodaki/log (diff)
downloadsharkey-5ce6fa67d212823be43c796e8632b721ede065bb.tar.gz
sharkey-5ce6fa67d212823be43c796e8632b721ede065bb.tar.bz2
sharkey-5ce6fa67d212823be43c796e8632b721ede065bb.zip
Make migration scripts
and use createdAt instead of _id
Diffstat (limited to 'tools')
-rw-r--r--tools/migration/nighthike/10.js3
-rw-r--r--tools/migration/nighthike/9.js79
2 files changed, 82 insertions, 0 deletions
diff --git a/tools/migration/nighthike/10.js b/tools/migration/nighthike/10.js
new file mode 100644
index 0000000000..3c57b8d484
--- /dev/null
+++ b/tools/migration/nighthike/10.js
@@ -0,0 +1,3 @@
+db.following.remove({
+ deletedAt: { $exists: true }
+});
diff --git a/tools/migration/nighthike/9.js b/tools/migration/nighthike/9.js
new file mode 100644
index 0000000000..a904feb06f
--- /dev/null
+++ b/tools/migration/nighthike/9.js
@@ -0,0 +1,79 @@
+// for Node.js interpret
+
+const { default: Following } = require('../../../built/models/following');
+const { default: FollowingLog } = require('../../../built/models/following-log');
+const { default: FollowedLog } = require('../../../built/models/followed-log');
+const { default: zip } = require('@prezzemolo/zip')
+const html = require('../../../built/text/html').default;
+const parse = require('../../../built/text/parse').default;
+
+const migrate = async (following) => {
+ const followingCount = await Following.count({
+ followerId: following.followerId,
+ _id: { $lt: following._id },
+ $or: [
+ { deletedAt: { $exists: false } },
+ { deletedAt: { $gt: following.createdAt } }
+ ]
+ });
+ await FollowingLog.insert({
+ createdAt: following.createdAt,
+ userId: following.followerId,
+ count: followingCount + 1
+ });
+
+ const followersCount = await Following.count({
+ followeeId: following.followeeId,
+ _id: { $lt: following._id },
+ $or: [
+ { deletedAt: { $exists: false } },
+ { deletedAt: { $gt: following.createdAt } }
+ ]
+ });
+ await FollowedLog.insert({
+ createdAt: following.createdAt,
+ userId: following.followeeId,
+ count: followersCount + 1
+ });
+
+ if (following.deletedAt) {
+ await FollowingLog.insert({
+ createdAt: following.deletedAt,
+ userId: following.followerId,
+ count: followingCount - 1
+ });
+
+ await FollowedLog.insert({
+ createdAt: following.deletedAt,
+ userId: following.followeeId,
+ count: followersCount - 1
+ });
+ }
+
+ return true;
+}
+
+async function main() {
+ const count = await Following.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 Following.find({}, {
+ limit: dop, skip: time * dop, sort: { _id: 1 }
+ })
+ 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)