mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
v3 migration: barebone migration
This commit is contained in:
@@ -7,10 +7,6 @@ website/build/
|
|||||||
website/transpiled-babel/
|
website/transpiled-babel/
|
||||||
|
|
||||||
migrations/*
|
migrations/*
|
||||||
!migrations/api_v3
|
|
||||||
!migrations/api_v3
|
|
||||||
!migrations/api_v3
|
|
||||||
migrations/api_v3/old_models
|
|
||||||
|
|
||||||
# The files in website/public/js should be moved out and browserified
|
# The files in website/public/js should be moved out and browserified
|
||||||
website/public/
|
website/public/
|
||||||
|
|||||||
@@ -1,93 +1,171 @@
|
|||||||
/* eslint-disable no-console */
|
|
||||||
|
|
||||||
// Migrate users collection to new schema
|
// Migrate users collection to new schema
|
||||||
// This should run AFTER challenges migration
|
// This should run AFTER challenges migration
|
||||||
|
|
||||||
// This code makes heavy use of ES6 / 7 features and should be compiled / run with BabelJS.
|
// The console-stamp module must be installed (not included in package.json)
|
||||||
|
|
||||||
// It requires two environment variables: MONGODB_OLD and MONGODB_NEW
|
// It requires two environment variables: MONGODB_OLD and MONGODB_NEW
|
||||||
|
|
||||||
/*
|
// Due to some big user profiles it needs more RAM than is allowed by default by v8 (arounf 1.7GB).
|
||||||
tags must have a name
|
// Run the script with --max-old-space-size=4096 to allow up to 4GB of RAM
|
||||||
*/
|
|
||||||
|
|
||||||
console.log('Starting migrations/api_v3/users.js.');
|
console.log('Starting migrations/api_v3/users.js.');
|
||||||
|
|
||||||
import Q from 'q';
|
require('babel-register');
|
||||||
import MongoDB from 'mongodb';
|
|
||||||
import nconf from 'nconf';
|
var Q = require('q');
|
||||||
import mongoose from 'mongoose';
|
var MongoDB = require('mongodb');
|
||||||
import _ from 'lodash';
|
var nconf = require('nconf');
|
||||||
|
var mongoose = require('mongoose');
|
||||||
|
var _ = require('lodash');
|
||||||
|
var uuid = require('uuid');
|
||||||
|
var consoleStamp = require('console-stamp');
|
||||||
|
|
||||||
|
// Add timestamps to console messages
|
||||||
|
consoleStamp(console);
|
||||||
|
|
||||||
// Initialize configuration
|
// Initialize configuration
|
||||||
import setupNconf from '../../website/src/libs/api-v3/setupNconf';
|
require('../../website/src/libs/api-v3/setupNconf')();
|
||||||
setupNconf();
|
|
||||||
|
|
||||||
const MONGODB_OLD = nconf.get('MONGODB_OLD');
|
var MONGODB_OLD = nconf.get('MONGODB_OLD');
|
||||||
const MONGODB_NEW = nconf.get('MONGODB_NEW');
|
var MONGODB_NEW = nconf.get('MONGODB_NEW');
|
||||||
|
|
||||||
mongoose.Promise = Q.Promise; // otherwise mongoose models won't work
|
mongoose.Promise = Q.Promise; // otherwise mongoose models won't work
|
||||||
|
|
||||||
// Load old and new models
|
// Load old and new models
|
||||||
import { model as NewUser } from '../../website/src/models/user';
|
//import { model as NewUser } from '../../website/src/models/user';
|
||||||
import * as Tasks from '../../website/src/models/task';
|
//import * as Tasks from '../../website/src/models/task';
|
||||||
|
|
||||||
// To be defined later when MongoClient connects
|
// To be defined later when MongoClient connects
|
||||||
let mongoDbOldInstance;
|
var mongoDbOldInstance;
|
||||||
let oldUserCollection;
|
var oldUserCollection;
|
||||||
|
|
||||||
let mongoDbNewInstance;
|
var mongoDbNewInstance;
|
||||||
let newUserCollection;
|
var newUserCollection;
|
||||||
let newTaskCollection;
|
var newTaskCollection;
|
||||||
|
|
||||||
async function processUser (_id) {
|
var BATCH_SIZE = 1000;
|
||||||
let [oldUser] = await oldUserCollection
|
|
||||||
.find({_id})
|
|
||||||
.limit(1)
|
|
||||||
.toArray();
|
|
||||||
|
|
||||||
let oldTasks = oldUser.habits.concat(oldUser.dailys).concat(oldUser.rewards).concat(oldUser.todos);
|
var processedUsers = 0;
|
||||||
oldUser.habits = oldUser.dailys = oldUser.rewards = oldUser.todos = undefined;
|
var totoalProcessedTasks = 0;
|
||||||
|
|
||||||
oldUser.challenges = [];
|
// Only process users that fall in a interval ie -> 0000-4000-0000-0000
|
||||||
oldUser.invitations.guilds = [];
|
var AFTER_USER_ID = nconf.get('AFTER_USER_ID');
|
||||||
oldUser.invitations.party = {};
|
var BEFORE_USER_ID = nconf.get('BEFORE_USER_ID');
|
||||||
oldUser.party = {};
|
|
||||||
oldUser.tags = oldUser.tags.map(tag => {
|
|
||||||
return {
|
|
||||||
_id: tag.id,
|
|
||||||
name: tag.name,
|
|
||||||
challenge: tag.challenge,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
let newUser = new NewUser(oldUser);
|
/* TODO
|
||||||
|
- _id 9
|
||||||
|
- challenges
|
||||||
|
- groups
|
||||||
|
- invitations
|
||||||
|
- challenges' tasks
|
||||||
|
*/
|
||||||
|
|
||||||
let batchInsertTasks = newTaskCollection.initializeUnorderedBulkOp();
|
function processUsers (afterId) {
|
||||||
oldTasks.forEach(oldTask => {
|
var processedTasks = 0;
|
||||||
let newTask = new Tasks[oldTask.type](oldTask);
|
var lastUser = null;
|
||||||
newTask.userId = newUser._id;
|
var oldUsers;
|
||||||
|
|
||||||
newTask.challenge = {};
|
var query = {};
|
||||||
if (!oldTask.text) newTask.text = 'text';
|
|
||||||
newTask.tags = _.map(oldTask.tags, (tagPresent, tagId) => {
|
if (BEFORE_USER_ID) {
|
||||||
return tagPresent && tagId;
|
query._id = {$lte: BEFORE_USER_ID};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (afterId) {
|
||||||
|
query._id = {$gt: afterId};
|
||||||
|
} else if (AFTER_USER_ID) {
|
||||||
|
query._id = {$gt: AFTER_USER_ID};
|
||||||
|
}
|
||||||
|
|
||||||
|
var batchInsertTasks = newTaskCollection.initializeUnorderedBulkOp();
|
||||||
|
var batchInsertUsers = newUserCollection.initializeUnorderedBulkOp();
|
||||||
|
|
||||||
|
console.log(`Executing users query.\nMatching users after ${afterId ? afterId : AFTER_USER_ID} and before ${BEFORE_USER_ID} (included).`);
|
||||||
|
|
||||||
|
return oldUserCollection
|
||||||
|
.find(query)
|
||||||
|
.sort({_id: 1})
|
||||||
|
.limit(BATCH_SIZE)
|
||||||
|
.toArray()
|
||||||
|
.then(function (oldUsersR) {
|
||||||
|
oldUsers = oldUsersR;
|
||||||
|
|
||||||
|
console.log(`Processing ${oldUsers.length} users. Already processed ${processedUsers} users and ${totoalProcessedTasks} tasks.`);
|
||||||
|
|
||||||
|
if (oldUsers.length === BATCH_SIZE) {
|
||||||
|
lastUser = oldUsers[oldUsers.length - 1]._id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
oldUsers.forEach(function (oldUser) {
|
||||||
|
var oldTasks = oldUser.habits.concat(oldUser.dailys).concat(oldUser.rewards).concat(oldUser.todos);
|
||||||
|
oldUser.habits = oldUser.dailys = oldUser.rewards = oldUser.todos = undefined;
|
||||||
|
|
||||||
|
oldUser.challenges = [];
|
||||||
|
if (oldUser.invitations) {
|
||||||
|
oldUser.invitations.guilds = [];
|
||||||
|
oldUser.invitations.party = {};
|
||||||
|
}
|
||||||
|
oldUser.party = {};
|
||||||
|
oldUser.tags = oldUser.tags.map(function (tag) {
|
||||||
|
return {
|
||||||
|
_id: tag.id,
|
||||||
|
name: tag.name,
|
||||||
|
challenge: tag.challenge,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
oldUser.tasksOrder = {
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
rewards: [],
|
||||||
|
todos: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
//let newUser = new NewUser(oldUser);
|
||||||
|
|
||||||
|
oldTasks.forEach(function (oldTask) {
|
||||||
|
oldTask._id = uuid.v4(); // create a new unique uuid
|
||||||
|
oldTask.userId = oldUser._id;
|
||||||
|
oldTask.legacyId = oldTask.id; // store the old task id
|
||||||
|
|
||||||
|
oldTask.challenge = {};
|
||||||
|
if (!oldTask.text) oldTask.text = 'text';
|
||||||
|
oldTask.tags = _.map(oldTask.tags, function (tagPresent, tagId) {
|
||||||
|
return tagPresent && tagId;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (oldTask.type !== 'todo' || (oldTask.type === 'todo' && !oldTask.completed)) {
|
||||||
|
oldUser.tasksOrder[`${oldTask.type}s`].push(oldTask._id);
|
||||||
|
}
|
||||||
|
|
||||||
|
//let newTask = new Tasks[oldTask.type](oldTask);
|
||||||
|
|
||||||
|
batchInsertTasks.insert(oldTask);
|
||||||
|
processedTasks++;
|
||||||
|
});
|
||||||
|
|
||||||
|
batchInsertUsers.insert(oldUser);
|
||||||
});
|
});
|
||||||
|
|
||||||
newUser.tasksOrder[`${oldTask.type}s`].push(newTask._id);
|
console.log(`Saving ${oldUsers.length} users and ${processedTasks} tasks.`);
|
||||||
|
|
||||||
let newTaskObject = newTask.toObject();
|
return Q.all([
|
||||||
newTaskObject.legacyId = oldTask.id;
|
batchInsertUsers.execute(),
|
||||||
|
batchInsertTasks.execute(),
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
.then(function () {
|
||||||
|
totoalProcessedTasks += processedTasks;
|
||||||
|
processedUsers += oldUsers.length;
|
||||||
|
|
||||||
batchInsertTasks.insert(newTaskObject);
|
console.log(`Saved ${oldUsers.length} users and their tasks.`);
|
||||||
|
|
||||||
|
if (lastUser) {
|
||||||
|
return processUsers(lastUser);
|
||||||
|
} else {
|
||||||
|
return console.log('Done!');
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
await Q.all([
|
|
||||||
newUserCollection.insertOne(newUser.toObject()),
|
|
||||||
batchInsertTasks.execute(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
console.log(`Saved user ${newUser._id} and their tasks.`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -211,13 +289,16 @@ var processUser = function(gt) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// Connect to the databases
|
// Connect to the databases
|
||||||
const MongoClient = MongoDB.MongoClient;
|
var MongoClient = MongoDB.MongoClient;
|
||||||
|
|
||||||
Q.all([
|
Q.all([
|
||||||
MongoClient.connect(MONGODB_OLD),
|
MongoClient.connect(MONGODB_OLD),
|
||||||
MongoClient.connect(MONGODB_NEW),
|
MongoClient.connect(MONGODB_NEW),
|
||||||
])
|
])
|
||||||
.then(([oldInstance, newInstance]) => {
|
.then(function (result) {
|
||||||
|
var oldInstance = result[0];
|
||||||
|
var newInstance = result[1];
|
||||||
|
|
||||||
mongoDbOldInstance = oldInstance;
|
mongoDbOldInstance = oldInstance;
|
||||||
oldUserCollection = mongoDbOldInstance.collection('users');
|
oldUserCollection = mongoDbOldInstance.collection('users');
|
||||||
|
|
||||||
@@ -227,8 +308,8 @@ Q.all([
|
|||||||
|
|
||||||
console.log(`Connected with MongoClient to ${MONGODB_OLD} and ${MONGODB_NEW}.`);
|
console.log(`Connected with MongoClient to ${MONGODB_OLD} and ${MONGODB_NEW}.`);
|
||||||
|
|
||||||
return processUser(nconf.get('USER_ID'));
|
return processUsers();
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(function (err) {
|
||||||
throw err;
|
console.error(err);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user