UNPKG

1.57 kBJavaScriptView Raw
1const moment = require('moment');
2const mongoose = require('mongoose');
3const domain = require('../');
4
5async function main() {
6 const oldConnection = await mongoose.createConnection(process.env.MONGOLAB_URI_OLD);
7 const connection = await mongoose.createConnection(process.env.MONGOLAB_URI);
8
9 const oldTaskRepo = new domain.repository.Task(oldConnection);
10 const taskRepo = new domain.repository.Task(connection);
11
12 const cursor = await oldTaskRepo.taskModel.find(
13 {
14 'project.id': { $exists: true, $eq: 'sskts-production' },
15 // runsAt: {
16 // $exists: true,
17 // $gte: moment('2020-04-22T00:00:00+09:00').toDate(),
18 // $lte: moment('2020-04-22T00:00:00+09:00').toDate()
19 // },
20 status: {
21 $in: [domain.factory.taskStatus.Ready, domain.factory.taskStatus.Running]
22 }
23 },
24 { createdAt: 0, updatedAt: 0 }
25 )
26 .sort({ runsAt: 1 })
27 .cursor();
28 console.log('tasks found');
29
30 let i = 0;
31 await cursor.eachAsync(async (doc) => {
32 i += 1;
33 const task = doc.toObject();
34 console.log('migrating task...', task.id, task.runsAt);
35
36 delete task._id;
37 delete task.id;
38 await taskRepo.taskModel.create(task);
39
40 console.log('added', task.id, i);
41 });
42
43 console.log(i, 'tasks migrated');
44 // await mongoose.disconnect();
45}
46
47main().then(() => {
48 console.log('success!');
49}).catch((error) => {
50 console.error(error);
51 process.exit(1);
52});