2024-10-18 07:04:57 -04:00
|
|
|
import BatchedUpdateModule from './helpers/batchedUpdate.mjs'
|
|
|
|
import { promiseMapWithLimit, promisify } from '@overleaf/promise-utils'
|
|
|
|
import { db, ObjectId, waitForDb } from '../app/src/infrastructure/mongodb.js'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
2022-09-29 05:47:50 -04:00
|
|
|
const sleep = promisify(setTimeout)
|
2024-10-18 07:04:57 -04:00
|
|
|
const { batchedUpdate } = BatchedUpdateModule
|
2022-09-29 05:47:50 -04:00
|
|
|
|
|
|
|
async function main(options) {
|
|
|
|
if (!options) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
_.defaults(options, {
|
|
|
|
dryRun: process.env.DRY_RUN !== 'false',
|
|
|
|
projectId: process.env.PROJECT_ID,
|
|
|
|
userId: process.env.USER_ID,
|
2022-10-13 09:41:07 -04:00
|
|
|
skipUsersMigration: process.env.SKIP_USERS_MIGRATION === 'true',
|
2022-09-29 05:47:50 -04:00
|
|
|
writeConcurrency: parseInt(process.env.WRITE_CONCURRENCY, 10) || 10,
|
|
|
|
letUserDoubleCheckInputsFor: parseInt(
|
|
|
|
process.env.LET_USER_DOUBLE_CHECK_INPUTS_FOR || 10 * 1000,
|
|
|
|
10
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
|
|
|
await letUserDoubleCheckInputs(options)
|
|
|
|
|
|
|
|
if (options.projectId) {
|
|
|
|
console.log('migrating projectId=' + options.projectId)
|
|
|
|
const project = await db.projects.findOne(
|
2023-12-18 05:54:01 -05:00
|
|
|
{ _id: new ObjectId(options.projectId) },
|
2022-09-29 05:47:50 -04:00
|
|
|
{ _id: 1, auditLog: 1 }
|
|
|
|
)
|
2022-10-04 13:48:54 -04:00
|
|
|
if (!project || !project.auditLog) {
|
|
|
|
console.error('unable to process project', project)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await processProjectsBatch([project], options)
|
2022-09-29 05:47:50 -04:00
|
|
|
} else if (options.userId) {
|
|
|
|
console.log('migrating userId=' + options.userId)
|
|
|
|
const user = await db.users.findOne(
|
2023-12-18 05:54:01 -05:00
|
|
|
{ _id: new ObjectId(options.userId) },
|
2022-09-29 05:47:50 -04:00
|
|
|
{ _id: 1, auditLog: 1 }
|
|
|
|
)
|
2022-10-04 13:48:54 -04:00
|
|
|
if (!user || !user.auditLog) {
|
|
|
|
console.error('unable to process user', user)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await processUsersBatch([user], options)
|
2022-09-29 05:47:50 -04:00
|
|
|
} else {
|
2022-10-13 09:41:07 -04:00
|
|
|
if (!options.skipUsersMigration) {
|
|
|
|
await batchedUpdate(
|
|
|
|
'users',
|
|
|
|
{ auditLog: { $exists: true } },
|
2023-03-22 07:21:47 -04:00
|
|
|
async users => {
|
2022-10-13 09:41:07 -04:00
|
|
|
await processUsersBatch(users, options)
|
|
|
|
},
|
|
|
|
{ _id: 1, auditLog: 1 }
|
|
|
|
)
|
|
|
|
}
|
2022-09-29 13:57:12 -04:00
|
|
|
|
|
|
|
// most projects are processed after its owner has been processed, but only those
|
|
|
|
// users with an existing `auditLog` have been taken into consideration, leaving
|
|
|
|
// some projects orphan. This batched update processes all remaining projects.
|
|
|
|
await batchedUpdate(
|
|
|
|
'projects',
|
|
|
|
{ auditLog: { $exists: true } },
|
2023-03-22 07:21:47 -04:00
|
|
|
async projects => {
|
2022-09-29 13:57:12 -04:00
|
|
|
await processProjectsBatch(projects, options)
|
|
|
|
},
|
|
|
|
{ _id: 1, auditLog: 1 }
|
|
|
|
)
|
2022-09-29 05:47:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processUsersBatch(users, options) {
|
2022-10-04 13:48:54 -04:00
|
|
|
if (!users || users.length <= 0) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const entries = users
|
|
|
|
.map(user => user.auditLog.map(log => ({ ...log, userId: user._id })))
|
|
|
|
.flat()
|
2022-09-29 05:47:50 -04:00
|
|
|
|
|
|
|
if (!options.dryRun && entries?.length > 0) {
|
|
|
|
await db.userAuditLogEntries.insertMany(entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.dryRun) {
|
2022-10-04 13:48:54 -04:00
|
|
|
const userIds = users.map(user => user._id)
|
|
|
|
await db.users.updateMany(
|
|
|
|
{ _id: { $in: userIds } },
|
|
|
|
{ $unset: { auditLog: 1 } }
|
|
|
|
)
|
2022-09-29 05:47:50 -04:00
|
|
|
}
|
|
|
|
|
2022-10-04 13:48:54 -04:00
|
|
|
await promiseMapWithLimit(options.writeConcurrency, users, async user => {
|
2022-10-05 09:17:52 -04:00
|
|
|
const projects = await db.projects
|
|
|
|
.find(
|
|
|
|
{ owner_ref: user._id, auditLog: { $exists: true } },
|
|
|
|
{ _id: 1, auditLog: 1 }
|
|
|
|
)
|
|
|
|
.toArray()
|
2022-10-04 13:48:54 -04:00
|
|
|
await processProjectsBatch(projects, options)
|
|
|
|
})
|
2022-09-29 05:47:50 -04:00
|
|
|
}
|
|
|
|
|
2022-09-29 13:57:12 -04:00
|
|
|
async function processProjectsBatch(projects, options) {
|
2022-10-04 13:48:54 -04:00
|
|
|
if (!projects || projects.length <= 0) {
|
|
|
|
return
|
|
|
|
}
|
2022-09-29 13:57:12 -04:00
|
|
|
|
2022-10-04 13:48:54 -04:00
|
|
|
const entries = projects
|
|
|
|
.map(project =>
|
|
|
|
project.auditLog.map(log => ({ ...log, projectId: project._id }))
|
|
|
|
)
|
|
|
|
.flat()
|
2022-09-29 05:47:50 -04:00
|
|
|
|
|
|
|
if (!options.dryRun && entries?.length > 0) {
|
|
|
|
await db.projectAuditLogEntries.insertMany(entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.dryRun) {
|
2022-10-04 13:48:54 -04:00
|
|
|
const projectIds = projects.map(project => project._id)
|
|
|
|
await db.projects.updateMany(
|
|
|
|
{ _id: { $in: projectIds } },
|
2022-09-29 05:47:50 -04:00
|
|
|
{ $unset: { auditLog: 1 } }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function letUserDoubleCheckInputs(options) {
|
|
|
|
const allOptions = {
|
|
|
|
...options,
|
|
|
|
// batchedUpdate() environment variables
|
|
|
|
BATCH_DESCENDING: process.env.BATCH_DESCENDING,
|
|
|
|
BATCH_SIZE: process.env.BATCH_SIZE,
|
|
|
|
VERBOSE_LOGGING: process.env.VERBOSE_LOGGING,
|
|
|
|
BATCH_LAST_ID: process.env.BATCH_LAST_ID,
|
|
|
|
BATCH_RANGE_END: process.env.BATCH_RANGE_END,
|
2022-10-13 09:41:07 -04:00
|
|
|
SKIP_USERS_MIGRATION: process.env.SKIP_USERS_MIGRATION,
|
2022-09-29 05:47:50 -04:00
|
|
|
}
|
|
|
|
console.error('Options:', JSON.stringify(allOptions, null, 2))
|
|
|
|
console.error(
|
|
|
|
'Waiting for you to double check inputs for',
|
|
|
|
options.letUserDoubleCheckInputsFor,
|
|
|
|
'ms'
|
|
|
|
)
|
|
|
|
await sleep(options.letUserDoubleCheckInputsFor)
|
|
|
|
}
|
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
export default main
|
|
|
|
|
|
|
|
if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
|
|
try {
|
|
|
|
await waitForDb()
|
|
|
|
await main()
|
|
|
|
console.log('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2022-09-29 05:47:50 -04:00
|
|
|
}
|