split_tests_assigned_at_to_dates.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
  2. async function updateStringDates() {
  3. await waitForDb()
  4. const users = await db.users.find({
  5. splitTests: { $exists: true },
  6. })
  7. let user
  8. let count = 0
  9. while ((user = await users.next())) {
  10. count += 1
  11. if (count % 10000 === 0) {
  12. console.log(`processed ${count} users...`)
  13. }
  14. const splitTests = user.splitTests
  15. for (const splitTestKey of Object.keys(splitTests)) {
  16. for (const variantIndex in splitTests[splitTestKey]) {
  17. splitTests[splitTestKey][variantIndex].assignedAt = new Date(
  18. splitTests[splitTestKey][variantIndex].assignedAt
  19. )
  20. }
  21. }
  22. await db.users.updateOne(
  23. {
  24. _id: user._id,
  25. },
  26. { $set: { splitTests } }
  27. )
  28. }
  29. console.log(`Updated ${count} assignedAt strings to dates!`)
  30. }
  31. if (!module.parent) {
  32. updateStringDates()
  33. .then(() => {
  34. process.exit(0)
  35. })
  36. .catch(error => {
  37. console.error(error)
  38. process.exit(1)
  39. })
  40. }
  41. module.exports = updateStringDates