From ff987aa433485e995f7f8c8364e444749dd093ac Mon Sep 17 00:00:00 2001 From: Jimmy Domagala-Tang Date: Tue, 18 Nov 2025 09:19:47 -0800 Subject: [PATCH] Migration for "notificationsPreferences" collection (#29446) * Implement notifications preferences model * NotificationPreferences class * remove index performance tests * move to unit tests * use zod * saving multiple preference values * set unique:true * throw error if preferences can't be parsed * use z.infer * strict schema when saving preferences * fix: update notifPreferenceKey type to only contain key values --------- Co-authored-by: Domagoj Kriskovic GitOrigin-RevId: 20cf3799c91e6a890ab6217667594d181237c791 --- .../web/app/src/infrastructure/mongodb.js | 1 + ...te_notificationsPreferences_collection.mjs | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tools/migrations/20251110151140_create_notificationsPreferences_collection.mjs diff --git a/services/web/app/src/infrastructure/mongodb.js b/services/web/app/src/infrastructure/mongodb.js index e258997321..611463e8cc 100644 --- a/services/web/app/src/infrastructure/mongodb.js +++ b/services/web/app/src/infrastructure/mongodb.js @@ -53,6 +53,7 @@ const db = { migrations: internalDb.collection('migrations'), notifications: internalDb.collection('notifications'), emailNotifications: internalDb.collection('emailNotifications'), + notificationsPreferences: internalDb.collection('notificationsPreferences'), oauthAccessTokens: internalDb.collection('oauthAccessTokens'), oauthApplications: internalDb.collection('oauthApplications'), oauthAuthorizationCodes: internalDb.collection('oauthAuthorizationCodes'), diff --git a/tools/migrations/20251110151140_create_notificationsPreferences_collection.mjs b/tools/migrations/20251110151140_create_notificationsPreferences_collection.mjs new file mode 100644 index 0000000000..113e3f8ffe --- /dev/null +++ b/tools/migrations/20251110151140_create_notificationsPreferences_collection.mjs @@ -0,0 +1,36 @@ +import Helpers from './lib/helpers.mjs' +import { getCollectionInternal } from './lib/mongodb.mjs' + +const tags = ['server-ce', 'server-pro', 'saas'] + +const indexes = [ + { + // compound index for querying both global (project_id: null) and project-specific notification preferences + key: { + user_id: 1, + project_id: 1, + }, + unique: true, + name: 'user_id_1_project_id_1', + }, +] + +const migrate = async () => { + const notificationsPreferences = await getCollectionInternal( + 'notificationsPreferences' + ) + await Helpers.addIndexesToCollection(notificationsPreferences, indexes) +} + +const rollback = async () => { + const notificationsPreferences = await getCollectionInternal( + 'notificationsPreferences' + ) + await Helpers.dropIndexesFromCollection(notificationsPreferences, indexes) +} + +export default { + tags, + migrate, + rollback, +}