split_tests_assigned_at_to_dates.mjs 1.1 KB

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