[web] Create script to update forgotten featuresUpdatedAt after the migration to 20s compile timeout (#18113)

* Copy `migration_compile_timeout_60s_to_20s.js` script

* Update `featuresUpdatedAt`

* Add a comment about `featuresUpdatedAt` in migration_compile_timeout_60s_to_20s.js

* Fix test on migration_compile_timeout_60s_to_20s.js

* Fix: Include users having `featuresUpdatedAt` undefined in the update

* Add test on `migration_compile_timeout_60s_to_20s_fixup_features_updated_at`

GitOrigin-RevId: 4b2baf955a6a9f39bf9ce00b7839af551064c6cb
This commit is contained in:
Antoine Clausse
2024-04-30 08:04:28 +00:00
committed by Copybot
parent 70c05dd5f7
commit 711d50a2f1
3 changed files with 333 additions and 98 deletions
@@ -34,7 +34,8 @@ const main = async ({ COMMIT, SKIP_COUNT }) => {
const nModified = await batchedUpdate(
'users',
{ 'features.compileTimeout': { $lte: 60, $ne: 20 } },
{ $set: { 'features.compileTimeout': 20 } }
// NOTE: Always update featuresUpdatedAt to ensure the user's features synced with BigQuery
{ $set: { 'features.compileTimeout': 20, featuresUpdatedAt: new Date() } }
)
console.log(`Updated ${nModified} records`)
}
@@ -0,0 +1,65 @@
#!/usr/bin/env node
const minimist = require('minimist')
const {
db,
READ_PREFERENCE_SECONDARY,
waitForDb,
} = require('../app/src/infrastructure/mongodb')
const { batchedUpdate } = require('./helpers/batchedUpdate')
// A few seconds after the previous migration script was run
const FEATURES_UPDATED_AT = new Date('2024-04-16T12:41:00Z')
const query = {
'features.compileTimeout': 20,
$or: [
{ featuresUpdatedAt: { $exists: false } },
{ featuresUpdatedAt: { $lt: FEATURES_UPDATED_AT } },
],
}
async function logCount() {
const usersToUpdate = await db.users.countDocuments(query, {
readPreference: READ_PREFERENCE_SECONDARY,
})
console.log(
`Found ${usersToUpdate} users needing their featuresUpdatedAt updated`
)
}
const main = async ({ COMMIT, SKIP_COUNT }) => {
console.time('Script Duration')
await waitForDb()
if (!SKIP_COUNT) {
await logCount()
}
if (COMMIT) {
const nModified = await batchedUpdate('users', query, {
$set: { featuresUpdatedAt: FEATURES_UPDATED_AT },
})
console.log(`Updated ${nModified} records`)
}
console.timeEnd('Script Duration')
}
const setup = () => {
const argv = minimist(process.argv.slice(2))
const COMMIT = argv.commit !== undefined
const SKIP_COUNT = argv['skip-count'] !== undefined
if (!COMMIT) {
console.warn('Doing dry run. Add --commit to commit changes')
}
return { COMMIT, SKIP_COUNT }
}
main(setup())
.catch(err => {
console.error(err)
process.exit(1)
})
.then(() => process.exit(0))