2024-10-18 07:04:57 -04:00
|
|
|
import { db } from '../app/src/infrastructure/mongodb.js'
|
|
|
|
import BatchedUpdateModule from './helpers/batchedUpdate.mjs'
|
2024-11-04 04:09:11 -05:00
|
|
|
import { fileURLToPath } from 'node:url'
|
2024-10-18 07:04:57 -04:00
|
|
|
|
|
|
|
const { batchedUpdate } = BatchedUpdateModule
|
2023-03-16 06:33:09 -04:00
|
|
|
|
|
|
|
const DRY_RUN = !process.argv.includes('--dry-run=false')
|
|
|
|
const LOG_EVERY_IN_S = parseInt(process.env.LOG_EVERY_IN_S, 10) || 5
|
|
|
|
|
|
|
|
async function main(DRY_RUN) {
|
|
|
|
let processed = 0
|
|
|
|
let deleted = 0
|
|
|
|
let lastLog = 0
|
|
|
|
|
|
|
|
function logProgress() {
|
|
|
|
console.log(`rev missing ${processed} | deleted=true ${deleted}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
await batchedUpdate(
|
|
|
|
'docs',
|
|
|
|
{ rev: { $exists: false } },
|
2023-03-22 07:21:47 -04:00
|
|
|
async docs => {
|
2023-03-16 06:33:09 -04:00
|
|
|
if (!DRY_RUN) {
|
2023-03-22 07:21:47 -04:00
|
|
|
await db.docs.updateMany(
|
2023-03-16 06:33:09 -04:00
|
|
|
{
|
|
|
|
_id: { $in: docs.map(doc => doc._id) },
|
|
|
|
rev: { $exists: false },
|
|
|
|
},
|
|
|
|
{ $set: { rev: 1 } }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
processed += docs.length
|
|
|
|
deleted += docs.filter(doc => doc.deleted).length
|
|
|
|
|
|
|
|
if (Date.now() - lastLog >= LOG_EVERY_IN_S * 1000) {
|
|
|
|
logProgress()
|
|
|
|
lastLog = Date.now()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
_id: 1,
|
|
|
|
deleted: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
logProgress()
|
|
|
|
}
|
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
export default main
|
|
|
|
|
|
|
|
if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
|
|
|
try {
|
|
|
|
await main(DRY_RUN)
|
|
|
|
console.log('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2023-03-16 06:33:09 -04:00
|
|
|
}
|