mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Added migration script for User.emails (#639)
This commit is contained in:
parent
bff8cf75e5
commit
0da28dd924
1 changed files with 49 additions and 0 deletions
49
server-ce/migrations/9_create_user_emails_array.js
Normal file
49
server-ce/migrations/9_create_user_emails_array.js
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
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) => {
|
||||||
|
console.log(`updating ${user.length} users`)
|
||||||
|
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)
|
||||||
|
}
|
Loading…
Reference in a new issue