forked from FoundKeyGang/FoundKey
Make migration scripts
and use createdAt instead of _id
This commit is contained in:
parent
5b80b6e901
commit
5ce6fa67d2
7 changed files with 90 additions and 4 deletions
|
@ -6,6 +6,7 @@ export default FollowedLog;
|
||||||
|
|
||||||
export type IFollowedLog = {
|
export type IFollowedLog = {
|
||||||
_id: ObjectID;
|
_id: ObjectID;
|
||||||
|
createdAt: Date;
|
||||||
userId: ObjectID;
|
userId: ObjectID;
|
||||||
count: number;
|
count: number;
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,6 +6,7 @@ export default FollowingLog;
|
||||||
|
|
||||||
export type IFollowingLog = {
|
export type IFollowingLog = {
|
||||||
_id: ObjectID;
|
_id: ObjectID;
|
||||||
|
createdAt: Date;
|
||||||
userId: ObjectID;
|
userId: ObjectID;
|
||||||
count: number;
|
count: number;
|
||||||
};
|
};
|
||||||
|
|
|
@ -24,6 +24,7 @@ export default ({ data }, done) => Following.findOne({ _id: data.following }).th
|
||||||
}),
|
}),
|
||||||
|
|
||||||
promisedFollower.then(({ followingCount }) => FollowingLog.insert({
|
promisedFollower.then(({ followingCount }) => FollowingLog.insert({
|
||||||
|
createdAt: data.following.createdAt,
|
||||||
userId: followerId,
|
userId: followerId,
|
||||||
count: followingCount + 1
|
count: followingCount + 1
|
||||||
})),
|
})),
|
||||||
|
@ -36,6 +37,7 @@ export default ({ data }, done) => Following.findOne({ _id: data.following }).th
|
||||||
}),
|
}),
|
||||||
|
|
||||||
promisedFollowee.then(({ followersCount }) => FollowedLog.insert({
|
promisedFollowee.then(({ followersCount }) => FollowedLog.insert({
|
||||||
|
createdAt: data.following.createdAt,
|
||||||
userId: followerId,
|
userId: followerId,
|
||||||
count: followersCount + 1
|
count: followersCount + 1
|
||||||
})),
|
})),
|
||||||
|
|
|
@ -42,10 +42,10 @@ module.exports = (params) => new Promise(async (res, rej) => {
|
||||||
|
|
||||||
for (let i = 0; i < 30; i++) {
|
for (let i = 0; i < 30; i++) {
|
||||||
graph.push(FollowedLog.findOne({
|
graph.push(FollowedLog.findOne({
|
||||||
_id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
|
createdAt: { $lt: new Date(cursorTime / 1000) },
|
||||||
userId: user._id
|
userId: user._id
|
||||||
}, {
|
}, {
|
||||||
sort: { _id: -1 },
|
sort: { createdAt: -1 },
|
||||||
}).then(log => {
|
}).then(log => {
|
||||||
cursorDate = new Date(today.getTime());
|
cursorDate = new Date(today.getTime());
|
||||||
cursorTime = cursorDate.setDate(today.getDate() - i);
|
cursorTime = cursorDate.setDate(today.getDate() - i);
|
||||||
|
|
|
@ -42,10 +42,10 @@ module.exports = (params) => new Promise(async (res, rej) => {
|
||||||
|
|
||||||
for (let i = 0; i < 30; i++) {
|
for (let i = 0; i < 30; i++) {
|
||||||
graph.push(FollowingLog.findOne({
|
graph.push(FollowingLog.findOne({
|
||||||
_id: { $lt: ObjectID.createFromTime(cursorTime / 1000) },
|
createdAt: { $lt: new Date(cursorTime / 1000) },
|
||||||
userId: user._id
|
userId: user._id
|
||||||
}, {
|
}, {
|
||||||
sort: { _id: -1 },
|
sort: { createdAt: -1 },
|
||||||
}).then(log => {
|
}).then(log => {
|
||||||
cursorDate = new Date(today.getTime());
|
cursorDate = new Date(today.getTime());
|
||||||
cursorTime = cursorDate.setDate(today.getDate() - i);
|
cursorTime = cursorDate.setDate(today.getDate() - i);
|
||||||
|
|
3
tools/migration/nighthike/10.js
Normal file
3
tools/migration/nighthike/10.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
db.following.remove({
|
||||||
|
deletedAt: { $exists: true }
|
||||||
|
});
|
79
tools/migration/nighthike/9.js
Normal file
79
tools/migration/nighthike/9.js
Normal 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)
|
Loading…
Reference in a new issue