clear_feedback_collection.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* Clear feedback collection before a cutoff date
  2. *
  3. * Usage
  4. * node scripts/clear_feedback_collection.js 2022-11-01 # dry run mode
  5. * DRY_RUN=false node scripts/clear_feedback_collection.js 2022-11-01 # deletion mode
  6. */
  7. const { db, ObjectId, waitForDb } = require('../app/src/infrastructure/mongodb')
  8. const runScript = async (timestamp, dryRun) => {
  9. await waitForDb()
  10. const t = new Date(timestamp)
  11. if (isNaN(t)) {
  12. throw new Error('invalid date ' + timestamp)
  13. }
  14. const cutoffId = ObjectId.createFromTime(t / 1000)
  15. console.log('deleting all feedback entries before', t, '=>', cutoffId)
  16. const cursor = db.feedbacks.find({ _id: { $lt: cutoffId } })
  17. for await (const entry of cursor) {
  18. console.log('deleting', entry._id)
  19. if (dryRun) {
  20. console.log('skipping in dry run mode')
  21. continue
  22. }
  23. await db.feedbacks.deleteOne({ _id: entry._id })
  24. }
  25. }
  26. if (!module.parent) {
  27. // we are in the root module, which means that we're running as a script
  28. const timestamp = process.env.CUTOFF_TIMESTAMP || process.argv[2]
  29. const dryRun = process.env.DRY_RUN !== 'false'
  30. runScript(timestamp, dryRun)
  31. .then(() => process.exit())
  32. .catch(err => {
  33. console.error(err)
  34. process.exit(1)
  35. })
  36. }
  37. module.exports = runScript