1 | const moment = require('moment');
|
2 | const mongoose = require('mongoose');
|
3 | const domain = require('../');
|
4 |
|
5 | async function main() {
|
6 | const connection = await mongoose.createConnection(process.env.MONGOLAB_URI);
|
7 |
|
8 | const ownershipInfoRepo = new domain.repository.OwnershipInfo(connection);
|
9 |
|
10 | const membershipFor4update = {
|
11 | typeOf: 'MembershipService',
|
12 | id: '5b1874be4e1537775703963e'
|
13 | };
|
14 |
|
15 | const cursor = await ownershipInfoRepo.ownershipInfoModel.find(
|
16 | {
|
17 | 'project.id': { $exists: true, $eq: '' },
|
18 | 'typeOfGood.typeOf': {
|
19 | $exists: true,
|
20 | $eq: 'ProgramMembership'
|
21 | },
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | ownedThrough: {
|
28 | $exists: true,
|
29 | $gte: moment('2020-04-22T00:00:00+09:00').toDate()
|
30 | }
|
31 | },
|
32 | { createdAt: 0, updatedAt: 0 }
|
33 | )
|
34 | .sort({ ownedFrom: 1 })
|
35 | .cursor();
|
36 | console.log('ownershipInfos found');
|
37 |
|
38 | let i = 0;
|
39 | await cursor.eachAsync(async (doc) => {
|
40 | i += 1;
|
41 | const ownershipInfo = doc.toObject();
|
42 | const identifier = ownershipInfo.identifier;
|
43 |
|
44 | const membershipFor = ownershipInfo.typeOfGood.membershipFor;
|
45 | if (membershipFor === undefined || membershipFor === null) {
|
46 | console.log('membershipFor undefined');
|
47 | console.log('migrating ownershipInfo...', ownershipInfo.identifier, ownershipInfo.ownedFrom);
|
48 |
|
49 |
|
50 | await ownershipInfoRepo.ownershipInfoModel.findOneAndUpdate(
|
51 | { _id: ownershipInfo.id },
|
52 | { 'typeOfGood.membershipFor': membershipFor4update }
|
53 | ).exec();
|
54 | } else {
|
55 | console.log('membershipFor exists');
|
56 | }
|
57 |
|
58 | console.log('added', identifier, i);
|
59 | });
|
60 |
|
61 | console.log(i, 'ownershipInfos migrated');
|
62 |
|
63 | }
|
64 |
|
65 | main().then(() => {
|
66 | console.log('success!');
|
67 | }).catch((error) => {
|
68 | console.error(error);
|
69 | process.exit(1);
|
70 | });
|