Merge pull request #4330 from overleaf/ns-update-confirmedAt_strings

migrate confirmedAt strings to date types

GitOrigin-RevId: 5e8bde9b52b6696ed89a34ce58f00f31a921a57a
This commit is contained in:
nate stemen 2021-07-27 14:15:29 -04:00 committed by Copybot
parent 944ec4e47b
commit 7e61fc4035
2 changed files with 63 additions and 0 deletions

View file

@ -0,0 +1,9 @@
const updateStringDates = require('../scripts/confirmed_at_to_dates.js')
exports.migrate = async client => {
await updateStringDates()
}
exports.rollback = async client => {
/* nothing to do */
}

View file

@ -0,0 +1,54 @@
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
async function updateStringDates() {
await waitForDb()
const users = await db.users.aggregate([
{ $unwind: { path: '$emails' } },
{
$match: { 'emails.confirmedAt': { $exists: true, $type: 'string' } },
},
{
$project: {
_id: 1,
'emails.email': 1,
'emails.confirmedAt': 1,
},
},
])
let user
let count = 0
while ((user = await users.next())) {
count += 1
if (count % 10000 === 0) {
console.log(`processed ${count} users`)
}
const confirmedAt = user.emails.confirmedAt
const dateConfirmedAt = new Date(confirmedAt.replace(/ UTC$/, ''))
await db.users.updateOne(
{
_id: user._id,
'emails.email': user.emails.email,
},
{
$set: {
'emails.$.confirmedAt': dateConfirmedAt,
},
}
)
}
console.log(`Updated ${count} confirmedAt strings to dates!`)
}
if (!module.parent) {
updateStringDates()
.then(() => {
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
})
}
module.exports = updateStringDates