2019-09-13 07:57:37 -04:00
|
|
|
const Settings = require('settings-sharelatex')
|
|
|
|
const mongojs = require('mongojs')
|
|
|
|
const db = mongojs(Settings.mongo.url, ['users'])
|
|
|
|
const async = require('async')
|
|
|
|
|
|
|
|
const handleExit = () => console.log('Got signal. Shutting down.')
|
|
|
|
process.on('SIGINT', handleExit)
|
|
|
|
process.on('SIGHUP', handleExit)
|
|
|
|
|
|
|
|
const initUserEmailsAttribute = (user, callback) => {
|
|
|
|
const update = {
|
|
|
|
$set: {
|
|
|
|
emails: [
|
|
|
|
{
|
|
|
|
email: user.email,
|
|
|
|
createdAt: new Date()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.users.update({ _id: user._id }, update, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateAllUsersEmailsAttribute = (users, callback) => {
|
2019-09-18 08:11:34 -04:00
|
|
|
console.log(`updating ${users.length} users`)
|
2019-09-13 07:57:37 -04:00
|
|
|
async.eachSeries(users, initUserEmailsAttribute, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.migrate = (client, done) =>
|
|
|
|
db.users.find(
|
|
|
|
{ emails: { $exists: false } },
|
|
|
|
{ email: 1 },
|
|
|
|
(error, users) => {
|
|
|
|
if (error) {
|
|
|
|
callback(error)
|
|
|
|
} else {
|
|
|
|
updateAllUsersEmailsAttribute(users, done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
exports.rollback = (client, done) => {
|
|
|
|
const update = {
|
|
|
|
$unset: {
|
|
|
|
emails: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db.users.update({ emails: { $exists: true } }, update, done)
|
|
|
|
}
|