starting big migration

This commit is contained in:
Tyler Renelle
2013-02-04 17:27:27 -05:00
parent 57a7edc5d3
commit 927a3d0aa7
3 changed files with 87 additions and 28 deletions

View File

@@ -0,0 +1,56 @@
db.users.find({}).forEach(function(user){
// New user schema has public and private paths, so we can setup proper access control with racer
// Note 'public' and 'private' are reserved words
var newUser = {
pub:{},
priv: {}
};
newUser.priv.lastCron = user.lastCron;
user.priv.balance = user.balance;
user.priv.tasks = user.tasks;
// ------------ Stats
// rename money to gp
user.stats.gp = user.stats.money;
delete user.stats.money;
newUser.pub.stats = user.stats;
// ------------ Party
newUser.pub.party = null;
// ------------ ID lists
newUser.priv.idLists = {habit:user.habitList, daily:user.dailyList, todo:user.todoList, reward:user.rewardList};
// ------------ Flags
newUser.priv.flags = user.flags || {};
user.priv.partyEnabled = false;
user.priv.flags.itemsEnabled = user.items.itemsEnabled;
delete user.items.itemsEnabled;
// Items
user.pub.items = user.items;
// kickstarter
if (!user.notifications || !user.notifications.kickstarter) user.notifications = {kickstarter:'show'}
newUser.priv.flags.kickstarter = user.notifications.kickstarter;
// Ads
newUser.priv.flags.ads = user.flags.ads;
// ------------ API Token
newUser.priv.apiToken = user.preferences.api_token;
delete user.preferences.api_token;
// ------------ Preferences
newUser.pub.preferences = user.preferences;
db.users.update({_id:user.id}, newUser, {multi:true});
})

View File

@@ -4,7 +4,7 @@ process.on('uncaughtException', function (exception) {
}); });
require('coffee-script') // remove intermediate compilation requirement require('coffee-script') // remove intermediate compilation requirement
require('./src/server').listen(process.env.PORT || 3000); require('./lib/server').listen(process.env.PORT || 3000);
// Note: removed "up" module, which is default for development (but interferes with and production + PaaS) // Note: removed "up" module, which is default for development (but interferes with and production + PaaS)
// Restore to 5310bb0 if I want it back (see https://github.com/codeparty/derby/issues/165#issuecomment-10405693) // Restore to 5310bb0 if I want it back (see https://github.com/codeparty/derby/issues/165#issuecomment-10405693)

View File

@@ -5,48 +5,51 @@ lodash = require 'lodash'
derby = require 'derby' derby = require 'derby'
userSchema = userSchema =
lastCron: 'new' #this will be replaced with `+new Date` on first run # _id
balance: 2 pub:
stats: { money: 0, exp: 0, lvl: 1, hp: 50 } stats: { gp: 0, exp: 0, lvl: 1, hp: 50 }
items: { itemsEnabled: false, armor: 0, weapon: 0 } # party: null
notifications: { kickstarter: 'show' } # TODO invitations
preferences: { gender: 'm', armorSet: 'v1' } items: { armor: 0, weapon: 0 }
flags: { partyEnabled: false } preferences: { gender: 'm', armorSet: 'v1' }
party: [] priv:
tasks: {} idLists:
habitIds: [] habit: []
dailyIds: [] daily: []
todoIds: [] todo: []
rewardIds: [] reward: []
apiToken: null # set in newUserObject below
lastCron: 'new' #this will be replaced with `+new Date` on first run
balance: 2
tasks: {}
flags:
partyEnabled: false
itemsEnabled: false
kickstarter: 'show'
# ads: 'show' # added on registration
module.exports.newUserObject = -> module.exports.newUserObject = ->
# deep clone, else further new users get duplicate objects # deep clone, else further new users get duplicate objects
newUser = require('lodash').cloneDeep userSchema newUser = require('lodash').cloneDeep userSchema
newUser.priv.apiToken = derby.uuid()
for task in content.defaultTasks for task in content.defaultTasks
guid = task.id = require('racer').uuid() guid = task.id = derby.uuid()
newUser.tasks[guid] = task newUser.tasks[guid] = task
switch task.type switch task.type
when 'habit' then newUser.habitIds.push guid when 'habit' then newUser.priv.idLists.habit.push guid
when 'daily' then newUser.dailyIds.push guid when 'daily' then newUser.priv.idLists.daily.push guid
when 'todo' then newUser.todoIds.push guid when 'todo' then newUser.priv.idLists.todo.push guid
when 'reward' then newUser.rewardIds.push guid when 'reward' then newUser.priv.idLists.reward.push guid
return newUser return newUser
module.exports.updateUser = (batch) -> module.exports.updateUser = (batch) ->
user = batch.user user = batch.user
obj = user.batch.obj()
batch.set('notifications.kickstarter', 'show') unless user.get('notifications.kickstarter') batch.set('priv.apiToken', derby.uuid()) unless obj.priv.apiToken
batch.set('party', []) unless !_.isEmpty(user.get('party'))
# Preferences, including API key
# Some side-stepping to avoid unecessary set (one day, model.update... one day..)
currentPrefs = _.clone user.get('preferences')
mergedPrefs = _.defaults currentPrefs, { gender: 'm', armorSet: 'v1', api_token: derby.uuid() }
batch.set('preferences', mergedPrefs)
## Task List Cleanup ## Task List Cleanup
# FIXME temporary hack to fix lists (Need to figure out why these are happening) # FIXME temporary hack to fix lists (Need to figure out why these are happening)
# FIXME consolidate these all under user.listIds so we can set them en-masse
tasks = user.get('tasks') tasks = user.get('tasks')
_.each ['habit','daily','todo','reward'], (type) -> _.each ['habit','daily','todo','reward'], (type) ->
path = "#{type}Ids" path = "#{type}Ids"