migration_compile_timeout_60s_to_20s.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. console.log(`Found ${count60s} users with compileTimeout <= 60s && != 20s`)
  15. const count20s = await db.users.countDocuments(
  16. { 'features.compileTimeout': 20 },
  17. { readPreference: READ_PREFERENCE_SECONDARY }
  18. )
  19. console.log(`Found ${count20s} users with compileTimeout == 20s`)
  20. }
  21. const main = async ({ COMMIT, SKIP_COUNT }) => {
  22. console.time('Script Duration')
  23. await waitForDb()
  24. if (!SKIP_COUNT) {
  25. await logCount()
  26. }
  27. if (COMMIT) {
  28. const nModified = await batchedUpdate(
  29. 'users',
  30. { 'features.compileTimeout': { $lte: 60, $ne: 20 } },
  31. { $set: { 'features.compileTimeout': 20 } }
  32. )
  33. console.log(`Updated ${nModified} records`)
  34. }
  35. console.timeEnd('Script Duration')
  36. }
  37. const setup = () => {
  38. const argv = minimist(process.argv.slice(2))
  39. const COMMIT = argv.commit !== undefined
  40. const SKIP_COUNT = argv['skip-count'] !== undefined
  41. if (!COMMIT) {
  42. console.warn('Doing dry run. Add --commit to commit changes')
  43. }
  44. return { COMMIT, SKIP_COUNT }
  45. }
  46. main(setup())
  47. .catch(err => {
  48. console.error(err)
  49. process.exit(1)
  50. })
  51. .then(() => process.exit(0))