From 735f79ab6a3f96e30e975eb378daf9f2cdbc4ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Fri, 22 Oct 2021 09:58:45 +0200 Subject: [PATCH] Merge pull request #5364 from overleaf/ta-split-test-assignment-date-backfill Fix SplitTests assignedAt Type GitOrigin-RevId: 1711f8672fb8e1525780f869b847117a66ca86ed --- ...plit_tests_assigned_at_strings_to_dates.js | 11 +++++ .../split_tests_assigned_at_to_dates.js | 47 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.js create mode 100644 services/web/scripts/split_tests_assigned_at_to_dates.js diff --git a/services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.js b/services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.js new file mode 100644 index 0000000000..4d34e1c204 --- /dev/null +++ b/services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.js @@ -0,0 +1,11 @@ +const updateStringDates = require('../scripts/split_tests_assigned_at_to_dates') + +exports.tags = ['saas'] + +exports.migrate = async client => { + await updateStringDates() +} + +exports.rollback = async client => { + /* nothing to do */ +} diff --git a/services/web/scripts/split_tests_assigned_at_to_dates.js b/services/web/scripts/split_tests_assigned_at_to_dates.js new file mode 100644 index 0000000000..9df234cbb6 --- /dev/null +++ b/services/web/scripts/split_tests_assigned_at_to_dates.js @@ -0,0 +1,47 @@ +const { db, waitForDb } = require('../app/src/infrastructure/mongodb') + +async function updateStringDates() { + await waitForDb() + const users = await db.users.find({ + splitTests: { $exists: true }, + }) + + let user + let count = 0 + while ((user = await users.next())) { + count += 1 + if (count % 10000 === 0) { + console.log(`processed ${count} users...`) + } + + const splitTests = user.splitTests + for (const splitTestKey of Object.keys(splitTests)) { + for (const variantIndex in splitTests[splitTestKey]) { + splitTests[splitTestKey][variantIndex].assignedAt = new Date( + splitTests[splitTestKey][variantIndex].assignedAt + ) + } + } + + await db.users.updateOne( + { + _id: user._id, + }, + { $set: { splitTests } } + ) + } + console.log(`Updated ${count} assignedAt strings to dates!`) +} + +if (!module.parent) { + updateStringDates() + .then(() => { + process.exit(0) + }) + .catch(error => { + console.error(error) + process.exit(1) + }) +} + +module.exports = updateStringDates