Merge branch 'nonprofit-show' of https://github.com/FreeCodeCamp/freecodecamp into nonprofit-show
This commit is contained in:
@ -1,6 +1,5 @@
|
|||||||
require('dotenv').load();
|
require('dotenv').load();
|
||||||
var mongodb = require('mongodb'),
|
var mongodb = require('mongodb'),
|
||||||
|
|
||||||
User = require('../models/User.js'),
|
User = require('../models/User.js'),
|
||||||
newChallenges = require('./challengeMapping.json'),
|
newChallenges = require('./challengeMapping.json'),
|
||||||
secrets = require('../config/secrets');
|
secrets = require('../config/secrets');
|
||||||
@ -8,28 +7,58 @@ var mongodb = require('mongodb'),
|
|||||||
|
|
||||||
mongoose.connect(secrets.db);
|
mongoose.connect(secrets.db);
|
||||||
|
|
||||||
|
function userModelAssurity(cb) {
|
||||||
|
console.log('userModelAssurity');
|
||||||
var i = 1;
|
var i = 1;
|
||||||
var stream = User.find({}).skip(0).limit(0).stream();
|
var stream = User.find({}).skip(0).limit(0).batchSize(20000).stream();
|
||||||
|
|
||||||
stream.on('data', function (user) {
|
stream.on('data', function (user) {
|
||||||
console.log(i++);
|
console.log(i++);
|
||||||
user.save()
|
this.pause();
|
||||||
|
user.needsMigration = true;
|
||||||
|
user.save(function (err) {
|
||||||
|
if (err) {
|
||||||
|
console.log('woops');
|
||||||
|
}
|
||||||
|
this.resume();
|
||||||
|
}.bind(this));
|
||||||
})
|
})
|
||||||
.on('error', function (err) {
|
.on('error', function (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}).on('close', function () {
|
}).on('close', function () {
|
||||||
console.log('done with set');
|
console.log('done with set');
|
||||||
stream.destroy();
|
stream.destroy();
|
||||||
|
cb();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
i = 1;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
var stream = User.find({'user.needsMigration': true}).skip(0).limit(0).stream();
|
function userModelMigration(cb) {
|
||||||
|
|
||||||
|
var i = 1;
|
||||||
|
|
||||||
|
var stream = User.find({needsMigration: true}).skip(0).limit(0)
|
||||||
|
.batchSize(20000).stream();
|
||||||
|
|
||||||
stream.on('data', function (user) {
|
stream.on('data', function (user) {
|
||||||
|
console.log(i++);
|
||||||
if (user.challengesHash) {
|
if (user.challengesHash) {
|
||||||
this.pause();
|
this.pause();
|
||||||
console.log(i++);
|
|
||||||
user.needsMigration = false;
|
user.needsMigration = false;
|
||||||
var oldChallenges = Object.keys(user.challengesHash).filter(function (key) {
|
var oldChallenges = Object.keys(user.challengesHash).filter(function (key) {
|
||||||
if (user.challengesHash[key]) {
|
if (user.challengesHash[key]) {
|
||||||
@ -72,5 +101,8 @@ stream.on('data', function (user) {
|
|||||||
}).on('close', function () {
|
}).on('close', function () {
|
||||||
console.log('done with set');
|
console.log('done with set');
|
||||||
stream.destroy();
|
stream.destroy();
|
||||||
process.exit(0);
|
cb();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
migrateIt();
|
||||||
|
Reference in New Issue
Block a user