-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.js
151 lines (137 loc) · 5.35 KB
/
migrate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const {MongoClient} = require('mongodb');
// Set these to the appropriate variables.
const fromDbUsername = process.env.FROM_DB_USERNAME;
const fromDbPassword = process.env.FROM_DB_PASSWORD;
const toDbUsername = process.env.TO_DB_USERNAME;
const toDbPassword = process.env.TO_DB_PASSWORD;
const FROM_DB = 'tutorQueue';
const TO_DB = 'tutorCenter';
const fromUri = `mongodb+srv://${fromDbUsername}:${fromDbPassword}@cluster0.qs95z.mongodb.net/?retryWrites=true&w=majority`;
const toUri = `mongodb+srv://${toDbUsername}:${toDbPassword}@cluster0.qs95z.mongodb.net/?retryWrites=true&w=majority`;
async function getItems(client, dbName, collectionName){
const items = await client.db(dbName).collection(collectionName).find({}).toArray();
return items;
}
async function main(){
/**
* Connection URI. Update <username>, <password>, and <your-cluster-url> to reflect your cluster.
* See https://docs.mongodb.com/ecosystem/drivers/node/ for more details
*/
const fromClient = new MongoClient(fromUri);
const toClient = new MongoClient(toUri);
try {
// Connect to the MongoDB cluster
await fromClient.connect();
await toClient.connect();
// Make the appropriate DB calls
const pre = await getItems(fromClient, FROM_DB, "tutors");
const courses = [...new Set(pre.map(e=>e.courses).flat())].map(e=>e.replace(" ", ''));
for(let course of courses){
const res = await toClient.db(TO_DB).collection('tutor_center_course').replaceOne(
{
code: course
},
{
name: course,
code: course,
created_time: new Date(),
modified_time: new Date()
},
{
upsert: true
}
);
}
const courseObjs = await toClient.db(TO_DB).collection('tutor_center_course').find(
{
code: {
$in: courses
}
}
).toArray();
let courseTutorMax = await toClient.db(TO_DB).collection('tutor_center_tutor_courses').find().sort({id:-1}).limit(1).toArray();
let courseTutorId = courseTutorMax.length ? courseTutorMax[0].id : 0
for(let tutor of pre){
const res = await toClient.db(TO_DB).collection('tutor_center_tutor').updateOne(
{
email: tutor.email
},
{
$set: {
name: tutor.name,
email: tutor.email,
created_time: new Date(),
modified_time: new Date(),
is_active: true,
last_login: null,
is_coord: tutor.isCoord,
password: ''
}
},
{
upsert: true
}
);
const tutorObj = await toClient.db(TO_DB).collection('tutor_center_tutor').findOne(
{
email: tutor.email
}
);
for(let course of tutor.courses.map(e=>e.replace(" ", ''))){
const courseRes = await toClient.db(TO_DB).collection('tutor_center_tutor_courses').updateOne(
{
tutor_id: tutorObj._id,
course_id: courseObjs.filter(e=>e.code===course)[0]._id,
},
{
$set:{
tutor_id: tutorObj._id,
course_id: courseObjs.filter(e=>e.code===course)[0]._id,
id:courseTutorId
}
},
{
upsert: true
}
);
courseTutorId += 1
}
}
const migratedTutors = await toClient.db(TO_DB).collection('tutor_center_tutor').find(
{
email: {
$in: pre.map(e=>e.email)
}
}
).toArray();
const idMap = {};
for(let tutor of pre){
idMap[tutor._id] = migratedTutors.find(e=>e.email === tutor.email)._id
}
console.log(idMap);
for(let request of await getItems(fromClient, FROM_DB, "tutorrequests")){
const course = courseObjs.find(e=>e.code === request.course.replace(" ", ''));
if(course){
const reqRes = await toClient.db(TO_DB).collection('tutor_center_tutoringrequest').insertOne(
{
name: request.name,
email: request.email,
status: request.status,
created_time: request.submitted,
modified_time: new Date(),
description: request.description,
closed_time: request.submitted,
requested_course_id: course._id,
tutor_id: idMap[request.tutor]
}
);
}
}
} catch (e) {
console.error(e);
} finally {
await fromClient.close();
await toClient.close();
}
}
main().catch(console.error);