Skip to content

Commit

Permalink
changing who and latestChangeBy field from ldapUid to user mongo ID, …
Browse files Browse the repository at this point in the history
…migration script included relates #429
  • Loading branch information
paulgirard committed Aug 28, 2017
1 parent a26890b commit cdd826c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
76 changes: 76 additions & 0 deletions scripts/migration/migrateEditLogWho.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const models = require('../../server/lib/model')
const {EditLog} = require('../../server/lib/edit-logs')
const async = require('async')
const _ = require('lodash')

console.log('starting migration')
models.connect()

async.waterfall([
next => {
// get mongoid ldapuid index
models.People.aggregate([{$match:{ldapUid:{$ne:null}}},{$project:{_id:1, ldapUid:1}}]).then(data => {
console.log(data.length)
return next(null, data)
})
},
(data,next) => {
// update edtilogs
async.parallelLimit(
data.map(e => (nextParallel) => EditLog.updateMany(
{who:e.ldapUid},
{who:e._id},
(error,cursor) => nextParallel(null, cursor.modifiedCount))),
100,
(err, counts) => {
console.log(`wrote ${_.sum(counts)} modifications on editLogs`)
return next(null, data)
})
},
(data,next) => {
// update edtilogs
async.parallelLimit(
data.map(e => (nextParallel) => models.People.updateMany(
{latestChangeBy:e.ldapUid},
{latestChangeBy:e._id},
(error,cursor) => nextParallel(null, cursor.modifiedCount))),
100,
(err, counts) => {
console.log(`wrote ${_.sum(counts)} modifications on People`)
return next(null, data)
})
},
(data,next) => {
// update edtilogs
async.parallelLimit(
data.map(e => (nextParallel) => models.Organization.updateMany(
{latestChangeBy:e.ldapUid},
{latestChangeBy:e._id},
(error,cursor) => nextParallel(null, cursor.modifiedCount))),
100,
(err, counts) => {
console.log(`wrote ${_.sum(counts)} modifications on People`)
return next(null, data)
})
},
(data,next) => {
// update edtilogs
async.parallelLimit(
data.map(e => (nextParallel) => models.Activity.updateMany(
{latestChangeBy:e.ldapUid},
{latestChangeBy:e._id},
(error,cursor) => nextParallel(null, cursor.modifiedCount))),
100,
(err, counts) => {
console.log(`wrote ${_.sum(counts)} modifications on Activity`)
return next(null)
})
}
],error => {
if (error){
console.log(error)
process.exit(1);
}
process.exit(0);
})

6 changes: 3 additions & 3 deletions server/lib/rest-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ const replaceModel = (Model, save, getPermissions) => {
}

// Delete
doc.latestChangeBy = req.session.login // sign for EditLogs
doc.latestChangeBy = req.userId // sign for EditLogs
debug('Save', Model.modelName, doc)
return save(doc, perms)
}))
Expand All @@ -234,7 +234,7 @@ const replaceModel = (Model, save, getPermissions) => {
const createModel = (Model, save, getPermissions) => (req, res) => Promise.resolve()
.then(() => new Model(removeEmptyFields(req.body)))
.then(doc => getPermissions(req, doc).then(perms => {
doc.latestChangeBy = req.session.login // sign for EditLogs
doc.latestChangeBy = req.userId // sign for EditLogs
return save(doc, perms)
}))
.then(saved => {
Expand Down Expand Up @@ -270,7 +270,7 @@ const deleteModel = (Model, getPermissions) => {
if (hasRelations)
return Promise.reject(ClientError({ status: 403, message: 'Target still has relations' }))

doc.latestChangeBy = req.session.login
doc.latestChangeBy = req.userId
return doc.remove()
})
.then(() => {
Expand Down

0 comments on commit cdd826c

Please sign in to comment.