forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userMigration.js
137 lines (123 loc) · 3.59 KB
/
userMigration.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
/*eslint-disable block-scoped-var */
require('dotenv').load();
var User = require('../models/User.js'),
secrets = require('../config/secrets'),
mongoose = require('mongoose'),
R = require('ramda'),
ziplines = require('./challenges/ziplines.json'),
basejumps = require('./challenges/basejumps.json');
mongoose.connect(secrets.db);
var ziplineIds = ziplines.challenges.map(function(elem) {
return elem.id;
});
var basejumpIds = basejumps.challenges.map(function(elem) {
return elem.id;
});
var ziplineAndBaseJumpIds = R.concat(ziplineIds, basejumpIds);
function userModelAssurity(cb) {
console.log('userModelAssurity');
var i = 1;
var stream = User.find({}).skip(0).limit(0).batchSize(20000).stream();
stream.on('data', function (user) {
console.log(i++);
this.pause();
user.needsMigration = true;
user.save(function (err) {
if (err) {
console.log('woops');
}
this.resume();
}.bind(this));
})
.on('error', function (err) {
console.log(err);
}).on('close', function () {
console.log('done with set');
stream.destroy();
cb();
});
}
function migrateIt() {
console.log('migrateIt');
var dones = 0;
var done = function() {
dones++;
if (dones === 2) {
process.exit(0);
}
if (dones === 1) {
userModelMigration(done);
}
};
console.log('calling userModelAssurity');
userModelAssurity(done);
}
function userModelMigration(cb) {
var i = 1;
var stream = User.find({needsMigration: true}).skip(0).limit(0)
.batchSize(20000).stream();
stream.on('data', function (user) {
console.log(i++);
/*
if (user.challengesHash) {
this.pause();
user.needsMigration = false;
var oldChallenges = Object.keys(user.challengesHash).filter(function (key) {
if (user.challengesHash[key]) {
user.progressTimestamps.push(user.challengesHash[key] * 1000);
}
return user.challengesHash[key];
});
newChallenges.forEach(function (challenge) {
if (oldChallenges.indexOf(challenge.oldNumber) !== -1 && challenge.newId) {
user.completedCoursewares.push({
id: challenge.newId,
completedDate: user.challengesHash[challenge.oldNumber] * 1000
});
}
});
user.completedCoursewares.forEach(function (course) {
var indexOfCourse = user.uncompletedCoursewares.indexOf(course.id) !== -1;
if (indexOfCourse !== -1) {
user.uncompletedCoursewares.splice(indexOfCourse, 1);
}
});
user.completedBonfires.forEach(function (bonfire) {
bonfire.completedDate = bonfire.completedDate * 1000;
user.progressTimestamps.push(bonfire.completedDate);
});
}
*/
user.needsMigration = false;
user.completedChallenges = user.completedChallenges.map(function(elem) {
if (ziplineAndBaseJumpIds.indexOf(elem.id) > 0) {
return ({
id: elem.id,
name: elem.name,
completedWith: elem.completedWith,
completedDate: elem.completedDate,
solution: elem.solution,
githubLink: elem.githubLink,
verified: elem.verified,
challengeType: typeof elem.githubLink === 'boolean' ? 4 : 3
});
} else {
return elem;
}
});
var self = this;
user.save(function (err) {
if (err) {
console.log('woops');
}
self.resume();
});
}).on('error', function (err) {
console.log(err);
}).on('close', function () {
console.log('done with set');
stream.destroy();
cb();
});
}
migrateIt();