2024-10-18 07:04:57 -04:00
|
|
|
import _ from 'lodash'
|
|
|
|
import { db } from '../app/src/infrastructure/mongodb.js'
|
2024-11-08 03:34:14 -05:00
|
|
|
import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
|
2024-10-18 07:04:57 -04:00
|
|
|
import { promiseMapWithLimit } from '@overleaf/promise-utils'
|
2024-11-04 04:09:11 -05:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2020-09-25 04:40:07 -04:00
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
|
2020-09-25 04:40:07 -04:00
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
// $ node scripts/convert_archived_state.mjs FIRST,SECOND
|
2020-11-04 04:53:26 -05:00
|
|
|
|
2022-11-14 12:27:10 -05:00
|
|
|
async function main(STAGE) {
|
|
|
|
for (const FIELD of ['archived', 'trashed']) {
|
|
|
|
if (STAGE.includes('FIRST')) {
|
|
|
|
await batchedUpdate(
|
2024-11-08 03:34:14 -05:00
|
|
|
db.projects,
|
2022-11-14 12:27:10 -05:00
|
|
|
{ [FIELD]: false },
|
|
|
|
{
|
|
|
|
$set: { [FIELD]: [] },
|
|
|
|
}
|
|
|
|
)
|
2020-09-25 04:40:07 -04:00
|
|
|
|
2022-11-14 12:27:10 -05:00
|
|
|
console.error('Done, with first part for field:', FIELD)
|
|
|
|
}
|
2020-09-25 04:40:07 -04:00
|
|
|
|
2022-11-14 12:27:10 -05:00
|
|
|
if (STAGE.includes('SECOND')) {
|
|
|
|
await batchedUpdate(
|
2024-11-08 03:34:14 -05:00
|
|
|
db.projects,
|
2022-11-14 12:27:10 -05:00
|
|
|
{ [FIELD]: true },
|
2023-03-22 07:21:47 -04:00
|
|
|
async function performUpdate(nextBatch) {
|
2022-11-14 12:27:10 -05:00
|
|
|
await promiseMapWithLimit(
|
|
|
|
WRITE_CONCURRENCY,
|
|
|
|
nextBatch,
|
|
|
|
async project => {
|
|
|
|
try {
|
2023-03-22 07:21:47 -04:00
|
|
|
await upgradeFieldToArray({ project, FIELD })
|
2022-11-14 12:27:10 -05:00
|
|
|
} catch (err) {
|
|
|
|
console.error(project._id, err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: 1,
|
|
|
|
owner_ref: 1,
|
|
|
|
collaberator_refs: 1,
|
|
|
|
readOnly_refs: 1,
|
|
|
|
tokenAccessReadAndWrite_refs: 1,
|
|
|
|
tokenAccessReadOnly_refs: 1,
|
|
|
|
}
|
|
|
|
)
|
2020-11-04 04:53:26 -05:00
|
|
|
|
2022-11-14 12:27:10 -05:00
|
|
|
console.error('Done, with second part for field:', FIELD)
|
|
|
|
}
|
2020-11-04 04:53:26 -05:00
|
|
|
}
|
2020-09-25 04:40:07 -04:00
|
|
|
}
|
|
|
|
|
2023-03-22 07:21:47 -04:00
|
|
|
async function upgradeFieldToArray({ project, FIELD }) {
|
|
|
|
return db.projects.updateOne(
|
2020-09-25 04:40:07 -04:00
|
|
|
{ _id: project._id },
|
|
|
|
{
|
2022-11-14 12:27:10 -05:00
|
|
|
$set: { [FIELD]: getAllUserIds(project) },
|
2020-09-25 04:40:07 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-11-14 12:27:10 -05:00
|
|
|
function getAllUserIds(project) {
|
2020-09-25 04:40:07 -04:00
|
|
|
return _.unionWith(
|
|
|
|
[project.owner_ref],
|
|
|
|
project.collaberator_refs,
|
|
|
|
project.readOnly_refs,
|
|
|
|
project.tokenAccessReadAndWrite_refs,
|
|
|
|
project.tokenAccessReadOnly_refs,
|
|
|
|
_objectIdEquals
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function _objectIdEquals(firstVal, secondVal) {
|
|
|
|
// For use as a comparator for unionWith
|
|
|
|
return firstVal.toString() === secondVal.toString()
|
|
|
|
}
|
2024-10-18 07:04:57 -04:00
|
|
|
|
|
|
|
if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
|
|
try {
|
|
|
|
await main(process.argv.pop())
|
|
|
|
process.exit(0)
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default main
|