[migrations] add migration for back filling db.users.analyticsId (#33115)

* [migrations] add migration for back filling db.users.analyticsId

Co-authored-by: Davinder Singh <davinder.singh@overleaf.com>

* [web] add acceptance test for backfilling db.users.analyticsId

---------

Co-authored-by: Davinder Singh <davinder.singh@overleaf.com>
GitOrigin-RevId: a0840969ac0c4c84e874c6f00cf0a78857a4bb06
This commit is contained in:
Jakob Ackermann
2026-04-30 08:31:42 +02:00
committed by Copybot
parent 4e138e6f99
commit 50abfe8f0c
2 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
import { db } from './lib/mongodb.mjs'
import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
const tags = ['saas', 'server-ce', 'server-pro']
const migrate = async () => {
await batchedUpdate(db.users, { analyticsId: { $exists: false } }, [
{ $set: { analyticsId: { $toString: '$_id' } } },
])
}
const rollback = async () => {
await batchedUpdate(
db.users,
{ $expr: { $eq: [{ $strLenCP: '$analyticsId' }, 24] } },
{ $unset: { analyticsId: 1 } }
)
}
export default {
tags,
migrate,
rollback,
}