overleaf/services/web/scripts/migration_compile_timeout_60s_to_20s.js
Antoine Clausse 711d50a2f1 [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
2024-04-30 08:04:28 +00:00

61 lines
1.6 KiB
JavaScript

#!/usr/bin/env node
const minimist = require('minimist')
const {
db,
READ_PREFERENCE_SECONDARY,
waitForDb,
} = require('../app/src/infrastructure/mongodb')
const { batchedUpdate } = require('./helpers/batchedUpdate')
async function logCount() {
const count60s = await db.users.countDocuments(
{ 'features.compileTimeout': { $lte: 60, $ne: 20 } },
{ readPreference: READ_PREFERENCE_SECONDARY }
)
console.log(`Found ${count60s} users with compileTimeout <= 60s && != 20s`)
const count20s = await db.users.countDocuments(
{ 'features.compileTimeout': 20 },
{ readPreference: READ_PREFERENCE_SECONDARY }
)
console.log(`Found ${count20s} users with compileTimeout == 20s`)
}
const main = async ({ COMMIT, SKIP_COUNT }) => {
console.time('Script Duration')
await waitForDb()
if (!SKIP_COUNT) {
await logCount()
}
if (COMMIT) {
const nModified = await batchedUpdate(
'users',
{ 'features.compileTimeout': { $lte: 60, $ne: 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`)
}
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))