Merge pull request #10290 from overleaf/bg-clear-feedbacks-collection

add script to clear old messages from mongo feedbacks collection

GitOrigin-RevId: a4986f432a12e801f41ff9bdf11b8771ef2b601f
This commit is contained in:
Brian Gough 2022-11-04 10:02:35 +00:00 committed by Copybot
parent b593bfa56e
commit 495dd9016d
2 changed files with 42 additions and 0 deletions

View file

@ -45,6 +45,7 @@ async function setupDb() {
db.docOps = internalDb.collection('docOps')
db.docSnapshots = internalDb.collection('docSnapshots')
db.docs = internalDb.collection('docs')
db.feedbacks = internalDb.collection('feedbacks')
db.githubSyncEntityVersions = internalDb.collection(
'githubSyncEntityVersions'
)

View file

@ -0,0 +1,41 @@
/* Clear feedback collection before a cutoff date
*
* Usage
* node scripts/clear_feedback_collection.js 2022-11-01 # dry run mode
* DRY_RUN=false node scripts/clear_feedback_collection.js 2022-11-01 # deletion mode
*/
const { db, ObjectId, waitForDb } = require('../app/src/infrastructure/mongodb')
const runScript = async (timestamp, dryRun) => {
await waitForDb()
const t = new Date(timestamp)
if (isNaN(t)) {
throw new Error('invalid date ' + timestamp)
}
const cutoffId = ObjectId.createFromTime(t / 1000)
console.log('deleting all feedback entries before', t, '=>', cutoffId)
const cursor = db.feedbacks.find({ _id: { $lt: cutoffId } })
for await (const entry of cursor) {
console.log('deleting', entry._id)
if (dryRun) {
console.log('skipping in dry run mode')
continue
}
await db.feedbacks.deleteOne({ _id: entry._id })
}
}
if (!module.parent) {
// we are in the root module, which means that we're running as a script
const timestamp = process.env.CUTOFF_TIMESTAMP || process.argv[2]
const dryRun = process.env.DRY_RUN !== 'false'
runScript(timestamp, dryRun)
.then(() => process.exit())
.catch(err => {
console.error(err)
process.exit(1)
})
}
module.exports = runScript