From 30143ead974ed7ea329cef6d58590b8f2069a309 Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Tue, 17 Jun 2025 13:47:47 +0200 Subject: [PATCH] [web] migration fixes (#26443) * [web] fix typo in ESM migration of a db migration * [web] migrate old migration to ESM * [web] use batchedUpdate for bulk updates in old migrations GitOrigin-RevId: a984f785c577c2ac4125c947b8a3efffa57e1eb7 --- ..._split_tests_assigned_at_strings_to_dates.mjs | 2 +- ...230110140452_rename_recurly_cached_status.mjs | 16 +++++++++------- ...07134844_group_invite_emails_to_lowercase.mjs | 9 ++++----- ..._subscriptions_managed_users_feature_flag.mjs | 8 ++++++-- ...31025094810_sso_config_certificates_array.mjs | 14 ++++++++++---- .../20231030160030_managed_users_enabled.mjs | 10 +++++----- ...0923131936_create_user_last_active_index.mjs} | 14 ++++++++++---- ...null_managed_users_sso_from_subscriptions.mjs | 8 ++++++-- 8 files changed, 51 insertions(+), 30 deletions(-) rename services/web/migrations/{20240923131936_create_user_last_active_index.js => 20240923131936_create_user_last_active_index.mjs} (55%) diff --git a/services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.mjs b/services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.mjs index 8dbac841c6..094f3501c3 100644 --- a/services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.mjs +++ b/services/web/migrations/20210726083523_convert_split_tests_assigned_at_strings_to_dates.mjs @@ -1,4 +1,4 @@ -import updateStringDates from '../scripts/confirmed_at_to_dates.mjs' +import updateStringDates from '../scripts/split_tests_assigned_at_to_dates.mjs' const tags = ['saas'] diff --git a/services/web/migrations/20230110140452_rename_recurly_cached_status.mjs b/services/web/migrations/20230110140452_rename_recurly_cached_status.mjs index 227555b4e3..37608bc1a1 100644 --- a/services/web/migrations/20230110140452_rename_recurly_cached_status.mjs +++ b/services/web/migrations/20230110140452_rename_recurly_cached_status.mjs @@ -1,13 +1,12 @@ -/* eslint-disable no-unused-vars */ - -import Helpers from './lib/helpers.mjs' +import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js' const tags = ['saas'] const migrate = async client => { const { db } = client // 'recurly' -> 'recurlyStatus' - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { $and: [ { recurlyStatus: { $exists: false } }, @@ -17,7 +16,8 @@ const migrate = async client => { { $rename: { recurly: 'recurlyStatus' } } ) // some records may have already recached the recurly status, discard old cache - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { $and: [ { recurlyStatus: { $exists: true } }, @@ -31,7 +31,8 @@ const migrate = async client => { const rollback = async client => { const { db } = client // 'recurlyStatus' -> 'recurly' - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { $and: [ { recurly: { $exists: false } }, @@ -41,7 +42,8 @@ const rollback = async client => { { $rename: { recurlyStatus: 'recurly' } } ) // some records may have already recached the recurly status, discard old cache - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { $and: [ { recurlyStatus: { $exists: true } }, diff --git a/services/web/migrations/20230207134844_group_invite_emails_to_lowercase.mjs b/services/web/migrations/20230207134844_group_invite_emails_to_lowercase.mjs index fdb2bc8e8d..7b8c76b9f2 100644 --- a/services/web/migrations/20230207134844_group_invite_emails_to_lowercase.mjs +++ b/services/web/migrations/20230207134844_group_invite_emails_to_lowercase.mjs @@ -1,12 +1,11 @@ -/* eslint-disable no-unused-vars */ - -import Helpers from './lib/helpers.mjs' +import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js' const tags = ['saas'] const migrate = async client => { const { db } = client - db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { 'teamInvites.0': { $exists: true, @@ -36,7 +35,7 @@ const migrate = async client => { ) } -const rollback = async client => { +const rollback = async () => { // There is no way back. } diff --git a/services/web/migrations/20230928092537_backfill_subscriptions_managed_users_feature_flag.mjs b/services/web/migrations/20230928092537_backfill_subscriptions_managed_users_feature_flag.mjs index 4d782bf0d0..285cadcd92 100644 --- a/services/web/migrations/20230928092537_backfill_subscriptions_managed_users_feature_flag.mjs +++ b/services/web/migrations/20230928092537_backfill_subscriptions_managed_users_feature_flag.mjs @@ -1,8 +1,11 @@ +import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js' + const tags = ['saas'] const migrate = async client => { const { db } = client - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { 'features.managedUsers': { $ne: true } }, { $set: { 'features.managedUsers': null } } ) @@ -10,7 +13,8 @@ const migrate = async client => { const rollback = async client => { const { db } = client - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { 'features.managedUsers': { $eq: null } }, { $set: { 'features.managedUsers': false } } ) diff --git a/services/web/migrations/20231025094810_sso_config_certificates_array.mjs b/services/web/migrations/20231025094810_sso_config_certificates_array.mjs index 1ec19f481e..f14f8b9b1c 100644 --- a/services/web/migrations/20231025094810_sso_config_certificates_array.mjs +++ b/services/web/migrations/20231025094810_sso_config_certificates_array.mjs @@ -1,8 +1,11 @@ +import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js' + const tags = ['saas'] const migrate = async client => { const { db } = client - await db.ssoConfigs.updateMany( + await batchedUpdate( + db.ssoConfigs, { certificate: { $exists: true }, certificates: { $exists: false } }, [ { $set: { certificates: ['$certificate'] } }, @@ -11,11 +14,13 @@ const migrate = async client => { }, ] ) - await db.ssoConfigs.updateMany( + await batchedUpdate( + db.ssoConfigs, { userFirstNameAttribute: null }, { $unset: { userFirstNameAttribute: true } } ) - await db.ssoConfigs.updateMany( + await batchedUpdate( + db.ssoConfigs, { userLastNameAttribute: null }, { $unset: { userLastNameAttribute: true } } ) @@ -23,7 +28,8 @@ const migrate = async client => { const rollback = async client => { const { db } = client - await db.ssoConfigs.updateMany( + await batchedUpdate( + db.ssoConfigs, { certificate: { $exists: false }, certificates: { $exists: true } }, [ { $set: { certificate: { $arrayElemAt: ['$certificates', 0] } } }, diff --git a/services/web/migrations/20231030160030_managed_users_enabled.mjs b/services/web/migrations/20231030160030_managed_users_enabled.mjs index 772c9bfc44..6255102866 100644 --- a/services/web/migrations/20231030160030_managed_users_enabled.mjs +++ b/services/web/migrations/20231030160030_managed_users_enabled.mjs @@ -1,12 +1,11 @@ -/* eslint-disable no-unused-vars */ - -import Helpers from './lib/helpers.mjs' +import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js' const tags = ['saas'] const migrate = async client => { const { db } = client - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { groupPolicy: { $exists: true } }, { $set: { managedUsersEnabled: true } } ) @@ -14,7 +13,8 @@ const migrate = async client => { const rollback = async client => { const { db } = client - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { groupPolicy: { $exists: true } }, { $unset: { managedUsersEnabled: '' } } ) diff --git a/services/web/migrations/20240923131936_create_user_last_active_index.js b/services/web/migrations/20240923131936_create_user_last_active_index.mjs similarity index 55% rename from services/web/migrations/20240923131936_create_user_last_active_index.js rename to services/web/migrations/20240923131936_create_user_last_active_index.mjs index 62beada2c0..76bdb0d941 100644 --- a/services/web/migrations/20240923131936_create_user_last_active_index.js +++ b/services/web/migrations/20240923131936_create_user_last_active_index.mjs @@ -1,6 +1,6 @@ -const Helpers = require('./lib/helpers') +import Helpers from './lib/helpers.mjs' -exports.tags = ['server-ce', 'server-pro', 'saas'] +const tags = ['server-ce', 'server-pro', 'saas'] const indexes = [ { @@ -9,12 +9,18 @@ const indexes = [ }, ] -exports.migrate = async client => { +async function migrate(client) { const { db } = client await Helpers.addIndexesToCollection(db.users, indexes) } -exports.rollback = async client => { +async function rollback(client) { const { db } = client await Helpers.dropIndexesFromCollection(db.users, indexes) } + +export default { + tags, + migrate, + rollback, +} diff --git a/services/web/migrations/20241111133330_remove_null_managed_users_sso_from_subscriptions.mjs b/services/web/migrations/20241111133330_remove_null_managed_users_sso_from_subscriptions.mjs index de6282daac..003c519abc 100644 --- a/services/web/migrations/20241111133330_remove_null_managed_users_sso_from_subscriptions.mjs +++ b/services/web/migrations/20241111133330_remove_null_managed_users_sso_from_subscriptions.mjs @@ -1,12 +1,16 @@ +import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js' + const tags = ['saas'] const migrate = async client => { const { db } = client - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { 'features.managedUsers': { $eq: null } }, { $set: { 'features.managedUsers': true } } ) - await db.subscriptions.updateMany( + await batchedUpdate( + db.subscriptions, { 'features.groupSSO': { $eq: null } }, { $set: { 'features.groupSSO': true } } )