2024-10-18 07:04:57 -04:00
|
|
|
import BatchedUpdateModule from './helpers/batchedUpdate.mjs'
|
|
|
|
import { promiseMapWithLimit, promisify } from '@overleaf/promise-utils'
|
|
|
|
import { db } from '../app/src/infrastructure/mongodb.js'
|
|
|
|
import _ from 'lodash'
|
2024-11-04 04:09:11 -05:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2024-10-18 07:04:57 -04:00
|
|
|
|
|
|
|
const { batchedUpdate } = BatchedUpdateModule
|
2021-02-17 06:41:06 -05:00
|
|
|
const sleep = promisify(setTimeout)
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
async function main(options) {
|
|
|
|
if (!options) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
_.defaults(options, {
|
|
|
|
writeConcurrency: parseInt(process.env.WRITE_CONCURRENCY, 10) || 10,
|
|
|
|
performCleanup: process.argv.includes('--perform-cleanup'),
|
|
|
|
fixPartialInserts: process.argv.includes('--fix-partial-inserts'),
|
|
|
|
letUserDoubleCheckInputsFor: parseInt(
|
|
|
|
process.env.LET_USER_DOUBLE_CHECK_INPUTS_FOR || 10 * 1000,
|
|
|
|
10
|
|
|
|
),
|
|
|
|
})
|
2021-02-17 06:41:06 -05:00
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
await letUserDoubleCheckInputs(options)
|
2021-02-17 06:41:06 -05:00
|
|
|
|
|
|
|
await batchedUpdate(
|
|
|
|
'projects',
|
|
|
|
// array is not empty ~ array has one item
|
|
|
|
{ 'deletedFiles.0': { $exists: true } },
|
2023-03-22 07:21:47 -04:00
|
|
|
async projects => {
|
|
|
|
await processBatch(projects, options)
|
2021-08-04 05:34:41 -04:00
|
|
|
},
|
2021-02-17 06:41:06 -05:00
|
|
|
{ _id: 1, deletedFiles: 1 }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-03-22 07:21:47 -04:00
|
|
|
async function processBatch(projects, options) {
|
2021-08-04 05:34:41 -04:00
|
|
|
await promiseMapWithLimit(
|
|
|
|
options.writeConcurrency,
|
|
|
|
projects,
|
|
|
|
async project => {
|
|
|
|
await processProject(project, options)
|
|
|
|
}
|
|
|
|
)
|
2021-02-17 06:41:06 -05:00
|
|
|
}
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
async function processProject(project, options) {
|
|
|
|
await backFillFiles(project, options)
|
2021-02-17 06:41:06 -05:00
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
if (options.performCleanup) {
|
2021-02-17 06:41:06 -05:00
|
|
|
await cleanupProject(project)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
async function backFillFiles(project, options) {
|
2021-02-17 06:41:06 -05:00
|
|
|
const projectId = project._id
|
2021-02-19 04:54:40 -05:00
|
|
|
filterDuplicatesInPlace(project)
|
2021-02-17 06:41:06 -05:00
|
|
|
project.deletedFiles.forEach(file => {
|
|
|
|
file.projectId = projectId
|
|
|
|
})
|
2021-04-01 04:18:28 -04:00
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
if (options.fixPartialInserts) {
|
2021-04-01 04:18:28 -04:00
|
|
|
await fixPartialInserts(project)
|
|
|
|
} else {
|
|
|
|
await db.deletedFiles.insertMany(project.deletedFiles)
|
|
|
|
}
|
2021-02-17 06:41:06 -05:00
|
|
|
}
|
|
|
|
|
2021-02-19 04:54:40 -05:00
|
|
|
function filterDuplicatesInPlace(project) {
|
|
|
|
const fileIds = new Set()
|
|
|
|
project.deletedFiles = project.deletedFiles.filter(file => {
|
|
|
|
const id = file._id.toString()
|
|
|
|
if (fileIds.has(id)) return false
|
|
|
|
fileIds.add(id)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-01 04:18:28 -04:00
|
|
|
async function fixPartialInserts(project) {
|
|
|
|
const seenFileIds = new Set(
|
|
|
|
(
|
|
|
|
await db.deletedFiles
|
|
|
|
.find(
|
|
|
|
{ _id: { $in: project.deletedFiles.map(file => file._id) } },
|
|
|
|
{ projection: { _id: 1 } }
|
|
|
|
)
|
|
|
|
.toArray()
|
|
|
|
).map(file => file._id.toString())
|
|
|
|
)
|
|
|
|
project.deletedFiles = project.deletedFiles.filter(file => {
|
|
|
|
const id = file._id.toString()
|
|
|
|
if (seenFileIds.has(id)) return false
|
|
|
|
seenFileIds.add(id)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if (project.deletedFiles.length > 0) {
|
|
|
|
await db.deletedFiles.insertMany(project.deletedFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 06:41:06 -05:00
|
|
|
async function cleanupProject(project) {
|
|
|
|
await db.projects.updateOne(
|
|
|
|
{ _id: project._id },
|
|
|
|
{ $set: { deletedFiles: [] } }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-04 05:34:41 -04:00
|
|
|
async function letUserDoubleCheckInputs(options) {
|
|
|
|
if (options.performCleanup) {
|
2021-02-17 06:41:06 -05:00
|
|
|
console.error('BACK FILLING AND PERFORMING CLEANUP')
|
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
'BACK FILLING ONLY - You will need to rerun with --perform-cleanup'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
console.error(
|
|
|
|
'Waiting for you to double check inputs for',
|
2021-08-04 05:34:41 -04:00
|
|
|
options.letUserDoubleCheckInputsFor,
|
2021-02-17 06:41:06 -05:00
|
|
|
'ms'
|
|
|
|
)
|
2021-08-04 05:34:41 -04:00
|
|
|
await sleep(options.letUserDoubleCheckInputsFor)
|
|
|
|
}
|
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
export default main
|
2021-08-04 05:34:41 -04:00
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
|
|
try {
|
|
|
|
await main()
|
|
|
|
process.exit(0)
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2021-02-17 06:41:06 -05:00
|
|
|
}
|