migration_compile_timeout_60s_to_20s.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env node
  2. const minimist = require('minimist')
  3. const {
  4. db,
  5. READ_PREFERENCE_SECONDARY,
  6. waitForDb,
  7. } = require('../app/src/infrastructure/mongodb')
  8. const { batchedUpdate } = require('./helpers/batchedUpdate')
  9. async function logCount() {
  10. const count60s = await db.users.countDocuments(
  11. { 'features.compileTimeout': { $lte: 60, $ne: 20 } },
  12. { readPreference: READ_PREFERENCE_SECONDARY }
  13. )
  14. const count20s = await db.users.countDocuments(
  15. { 'features.compileTimeout': 20 },
  16. { readPreference: READ_PREFERENCE_SECONDARY }
  17. )
  18. console.log(`Found ${count60s} users with compileTimeout <= 60s && != 20s`)
  19. console.log(`Found ${count20s} users with compileTimeout == 20s`)
  20. }
  21. const main = async ({ COMMIT }) => {
  22. console.time('Script Duration')
  23. await waitForDb()
  24. await logCount()
  25. if (COMMIT) {
  26. const nModified = await batchedUpdate(
  27. 'users',
  28. { 'features.compileTimeout': { $lte: 60, $ne: 20 } },
  29. { $set: { 'features.compileTimeout': 20 } }
  30. )
  31. console.log(`Updated ${nModified} records`)
  32. }
  33. console.timeEnd('Script Duration')
  34. }
  35. const setup = () => {
  36. const argv = minimist(process.argv.slice(2))
  37. const COMMIT = argv.commit !== undefined
  38. if (!COMMIT) {
  39. console.warn('Doing dry run. Add --commit to commit changes')
  40. }
  41. return { COMMIT }
  42. }
  43. main(setup())
  44. .catch(err => {
  45. console.error(err)
  46. process.exit(1)
  47. })
  48. .then(() => process.exit(0))