Merge pull request #10442 from overleaf/jpa-convert-archived-trashed

[web] add migration for convert_archived_state script

GitOrigin-RevId: aeea3601a0c5f96e978c3f2a85458687d6d6678e
This commit is contained in:
Jakob Ackermann
2022-11-15 09:07:09 +00:00
committed by Copybot
parent 5eff354971
commit d4551dc7ce
3 changed files with 153 additions and 44 deletions
+57 -42
View File
@@ -6,62 +6,77 @@ const { batchedUpdate } = require('./helpers/batchedUpdate')
const { promiseMapWithLimit } = require('../app/src/util/promises')
// $ node scripts/convert_archived_state.js FIRST,SECOND
const STAGE = process.argv.pop()
async function main() {
if (STAGE.includes('FIRST')) {
await batchedUpdate(
'projects',
{ archived: false },
{
$set: { archived: [] },
}
)
async function main(STAGE) {
for (const FIELD of ['archived', 'trashed']) {
if (STAGE.includes('FIRST')) {
await batchedUpdate(
'projects',
{ [FIELD]: false },
{
$set: { [FIELD]: [] },
}
)
console.error('Done, with first part')
console.error('Done, with first part for field:', FIELD)
}
if (STAGE.includes('SECOND')) {
await batchedUpdate(
'projects',
{ [FIELD]: true },
async function performUpdate(collection, nextBatch) {
await promiseMapWithLimit(
WRITE_CONCURRENCY,
nextBatch,
async project => {
try {
await upgradeFieldToArray({ collection, project, FIELD })
} 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,
}
)
console.error('Done, with second part for field:', FIELD)
}
}
}
if (STAGE.includes('SECOND')) {
await batchedUpdate('projects', { archived: true }, performUpdate, {
_id: 1,
owner_ref: 1,
collaberator_refs: 1,
readOnly_refs: 1,
tokenAccessReadAndWrite_refs: 1,
tokenAccessReadOnly_refs: 1,
module.exports = main
if (require.main === module) {
main(process.argv.pop())
.then(() => {
process.exit(0)
})
.catch(error => {
console.error({ error })
process.exit(1)
})
console.error('Done, with second part')
}
}
main()
.then(() => {
process.exit(0)
})
.catch(error => {
console.error({ error })
process.exit(1)
})
async function performUpdate(collection, nextBatch) {
await promiseMapWithLimit(WRITE_CONCURRENCY, nextBatch, project =>
setArchived(collection, project)
)
}
async function setArchived(collection, project) {
const archived = calculateArchivedArray(project)
async function upgradeFieldToArray({ collection, project, FIELD }) {
return collection.updateOne(
{ _id: project._id },
{
$set: { archived },
$set: { [FIELD]: getAllUserIds(project) },
}
)
}
function calculateArchivedArray(project) {
function getAllUserIds(project) {
return _.unionWith(
[project.owner_ref],
project.collaberator_refs,