Make migration scripts

and use createdAt instead of _id
This commit is contained in:
syuilo 2018-04-02 23:19:07 +09:00
parent 5b80b6e901
commit 5ce6fa67d2
7 changed files with 90 additions and 4 deletions

View file

@ -6,6 +6,7 @@ export default FollowedLog;
export type IFollowedLog = {
_id: ObjectID;
createdAt: Date;
userId: ObjectID;
count: number;
};

View file

@ -6,6 +6,7 @@ export default FollowingLog;
export type IFollowingLog = {
_id: ObjectID;
createdAt: Date;
userId: ObjectID;
count: number;
};

View file

@ -24,6 +24,7 @@ export default ({ data }, done) => Following.findOne({ _id: data.following }).th
}),
promisedFollower.then(({ followingCount }) => FollowingLog.insert({
createdAt: data.following.createdAt,
userId: followerId,
count: followingCount + 1
})),
@ -36,6 +37,7 @@ export default ({ data }, done) => Following.findOne({ _id: data.following }).th
}),
promisedFollowee.then(({ followersCount }) => FollowedLog.insert({
createdAt: data.following.createdAt,
userId: followerId,
count: followersCount + 1
})),

View file

@ -42,10 +42,10 @@ module.exports = (params) => new Promise(async (res, rej) => {
for (let i = 0; i < 30; i++) {
graph.push(FollowedLog.findOne({
_id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
createdAt: { $lt: new Date(cursorTime / 1000) },
userId: user._id
}, {
sort: { _id: -1 },
sort: { createdAt: -1 },
}).then(log => {
cursorDate = new Date(today.getTime());
cursorTime = cursorDate.setDate(today.getDate() - i);

View file

@ -42,10 +42,10 @@ module.exports = (params) => new Promise(async (res, rej) => {
for (let i = 0; i < 30; i++) {
graph.push(FollowingLog.findOne({
_id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
createdAt: { $lt: new Date(cursorTime / 1000) },
userId: user._id
}, {
sort: { _id: -1 },
sort: { createdAt: -1 },
}).then(log => {
cursorDate = new Date(today.getTime());
cursorTime = cursorDate.setDate(today.getDate() - i);

View file

@ -0,0 +1,3 @@
db.following.remove({
deletedAt: { $exists: true }
});

View file

@ -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)