Address Henry's comments about robustness

This commit is contained in:
James Allen 2017-03-23 11:34:45 +00:00
parent 73443f5a65
commit e1af76f652

View file

@ -24,7 +24,7 @@ module.exports = HoldingAccountMigration =
jobs = projects.map (project) -> jobs = projects.map (project) ->
(cb) -> (cb) ->
console.log "[Removing user from project]", user_id, JSON.stringify(project) console.log "[Removing user from project]", user_id, JSON.stringify(project)
if !project._id? if !project?._id?
throw new Error("no project id") throw new Error("no project id")
if !HoldingAccountMigration.DRY_RUN if !HoldingAccountMigration.DRY_RUN
@ -48,31 +48,31 @@ module.exports = HoldingAccountMigration =
deleteUser: (user_id, callback = (error) ->) -> deleteUser: (user_id, callback = (error) ->) ->
if !user_id? if !user_id?
throw new Error("must have user_id") throw new Error("must have user_id")
db.users.find {_id: user_id}, (error, user) -> if !HoldingAccountMigration.DRY_RUN
return callback(error) if error? db.users.remove {_id: user_id, holdingAccount: true}, (error, result) ->
if !user? return callback(error) if error?
throw new Error("expected user") console.log "[Removed user]", user_id, result
console.log "[Removing user]", user_id, JSON.stringify(user) if result.n != 1
if !HoldingAccountMigration.DRY_RUN return callback(new Error("failed to remove user as expected"))
db.users.remove {_id: user_id}, (error, result) ->
console.log "[Removed user]", user_id, result
callback(error)
else
console.log "[Would have removed user]", user_id
callback() callback()
else
console.log "[Would have removed user]", user_id
callback()
run: (done) -> run: (done = () ->) ->
HoldingAccountMigration.findHoldingAccounts (error, users) -> HoldingAccountMigration.findHoldingAccounts (error, users) ->
throw error if error? throw error if error?
console.log "[Got list of holding accounts]", users.map (u) -> u._id console.log "[Got list of holding accounts]", users.map (u) -> u._id
jobs = users.map (u) -> jobs = users.map (u) ->
(cb) -> (cb) ->
HoldingAccountMigration.deleteUserProjects u._id, (error) -> HoldingAccountMigration.deleteUser u._id, (error) ->
return cb(error) if error? return cb(error) if error?
HoldingAccountMigration.deleteUser u._id, cb HoldingAccountMigration.deleteUserProjects u._id, (error) ->
return cb(error) if error?
setTimeout cb, 200 # Small delay to not hammer DB
async.series jobs, (error) -> async.series jobs, (error) ->
throw error if error? throw error if error?
console.log "[Removed holding accounts]" console.log "[FINISHED]"
done() done()
migrate: (client, done=()->) -> migrate: (client, done=()->) ->