1 | const moment = require('moment');
|
2 | const mongoose = require('mongoose');
|
3 | const domain = require('../');
|
4 |
|
5 | async 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 oldOwnershipInfoRepo = new domain.repository.OwnershipInfo(oldConnection);
|
10 | const ownershipInfoRepo = new domain.repository.OwnershipInfo(connection);
|
11 |
|
12 | const cursor = await oldOwnershipInfoRepo.ownershipInfoModel.find(
|
13 | {
|
14 | 'project.id': { $exists: true, $eq: 'sskts-production' },
|
15 | ownedFrom: {
|
16 | $exists: true,
|
17 | $gte: moment('2019-12-22T00:00:00+09:00').toDate(),
|
18 | $lte: moment('2020-01-22T00:00:00+09:00').toDate()
|
19 | },
|
20 | ownedThrough: {
|
21 | $exists: true,
|
22 | $lte: moment('2020-04-22T00:00:00+09:00').toDate()
|
23 |
|
24 | }
|
25 | },
|
26 | { createdAt: 0, updatedAt: 0 }
|
27 | )
|
28 | .sort({ ownedFrom: 1 })
|
29 | .cursor();
|
30 | console.log('ownershipInfos found');
|
31 |
|
32 | let i = 0;
|
33 | await cursor.eachAsync(async (doc) => {
|
34 | i += 1;
|
35 | const ownershipInfo = doc.toObject();
|
36 | const identifier = ownershipInfo.identifier;
|
37 | console.log('migrating ownershipInfo...', ownershipInfo.identifier, ownershipInfo.ownedFrom);
|
38 |
|
39 |
|
40 | delete ownershipInfo._id;
|
41 | delete ownershipInfo.id;
|
42 | await ownershipInfoRepo.saveByIdentifier(ownershipInfo);
|
43 |
|
44 | console.log('added', identifier, i);
|
45 | });
|
46 |
|
47 | console.log(i, 'ownershipInfos migrated');
|
48 |
|
49 | }
|
50 |
|
51 | main().then(() => {
|
52 | console.log('success!');
|
53 | }).catch((error) => {
|
54 | console.error(error);
|
55 | process.exit(1);
|
56 | });
|