Skip to content

chore(migration): drop unique index on Read Receipts collection to im…#40308

Open
DivyanshuVortex wants to merge 7 commits into
RocketChat:developfrom
DivyanshuVortex:chore_dropIndex
Open

chore(migration): drop unique index on Read Receipts collection to im…#40308
DivyanshuVortex wants to merge 7 commits into
RocketChat:developfrom
DivyanshuVortex:chore_dropIndex

Conversation

@DivyanshuVortex
Copy link
Copy Markdown

@DivyanshuVortex DivyanshuVortex commented Apr 25, 2026

Proposed changes

This PR introduces migration v336 to safely drop the compound index roomId_1_userId_1_messageId_1 from the read_receipts collection in existing databases.

This PR cleans up the read_receipts code to remove the obsolete TODO and introduces the necessary database migration script to actively delete the deprecated index, reclaiming storage space and avoiding unnecessary write overhead.

Issue :

#40235

Steps to test or reproduce

  1. Boot a Rocket.Chat instance on an older version or manually create a compound index on the read_receipts collection using:
    db.getCollection('read_receipts').createIndex({ roomId: 1, userId: 1, messageId: 1 }, { unique: true })
  2. Start the Rocket.Chat server on this branch.
  3. Observe the migration logs to confirm migration 336 executes successfully.
  4. Open your MongoDB shell or compass and verify that the roomId_1_userId_1_messageId_1 index no longer exists in the read_receipts collection (db.getCollection('read_receipts').getIndexes()).

Milestone : Migration of 9.0

Summary by CodeRabbit

  • Chores
    • Removed a legacy compound index reference from the ReadReceipts model.
    • Added migration v336 to remove the outdated index from the ReadReceipts collection; it safely ignores the case where the index is already absent to avoid failures.
    • Registered the new migration in the startup migration sequence.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 25, 2026

⚠️ No Changeset found

Latest commit: 8da67b9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot Bot commented Apr 25, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 25, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5d08d0f2-e937-4ec6-b39f-693907e9bfd1

📥 Commits

Reviewing files that changed from the base of the PR and between 38ea907 and d14616d.

📒 Files selected for processing (1)
  • apps/meteor/server/startup/migrations/index.ts
📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/server/startup/migrations/index.ts
🧠 Learnings (3)
📓 Common learnings
Learnt from: rodrigok
Repo: RocketChat/Rocket.Chat PR: 38623
File: apps/meteor/app/lib/server/functions/cleanRoomHistory.ts:146-149
Timestamp: 2026-04-18T12:32:53.425Z
Learning: In `apps/meteor/app/lib/server/functions/cleanRoomHistory.ts` (PR `#38623`), the read-receipt cleanup (both `ReadReceipts.removeByMessageIds` and `ReadReceiptsArchive.removeByMessageIds`) is intentionally only performed in the limited prune path (`limit && selectedMessageIds`). The unlimited/delete-all path (`limit === 0`) deliberately skips cleaning up orphaned read receipts in both hot and cold storage — this is by design. Do not flag this as a bug or missing cleanup in future reviews.
Learnt from: smirk-dev
Repo: RocketChat/Rocket.Chat PR: 39625
File: apps/meteor/app/api/server/v1/push.ts:85-97
Timestamp: 2026-03-14T14:58:58.834Z
Learning: In RocketChat/Rocket.Chat, the `push.token` POST/DELETE endpoints in `apps/meteor/app/api/server/v1/push.ts` were already migrated to the chained router API pattern on `develop` prior to PR `#39625`. `cleanTokenResult` (which strips `authToken` and returns `PushTokenResult`) and `isPushTokenPOSTProps`/`isPushTokenDELETEProps` validators already exist on `develop`. PR `#39625` only migrates `push.get` and `push.info` to the chained pattern. Do not flag `cleanTokenResult` or `PushTokenResult` as newly introduced behavior-breaking changes when reviewing this PR.
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/server/startup/migrations/index.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/server/startup/migrations/index.ts

Walkthrough

Removes a legacy commented compound index from the ReadReceipts model and adds migration v336 that attempts to drop the roomId_1_userId_1_messageId_1 index from the ReadReceipts collection, ignoring only IndexNotFound errors.

Changes

Cohort / File(s) Summary
Index Cleanup
apps/meteor/ee/server/models/raw/ReadReceipts.ts
Removes commented-out legacy unique compound index definition and associated TODO comment.
Database Migration
apps/meteor/server/startup/migrations/v336.ts, apps/meteor/server/startup/migrations/index.ts
Adds migration v336 which calls ReadReceipts.col.dropIndex('roomId_1_userId_1_messageId_1') and suppresses the error only when error.codeName === 'IndexNotFound'; updates migrations index to import ./v336.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

type: chore

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is partially related to the main change, covering the key action (drop unique index) and target collection (Read Receipts), but is truncated ('to im…') and doesn't fully convey the complete intent.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@DivyanshuVortex DivyanshuVortex marked this pull request as ready for review April 25, 2026 06:13
@DivyanshuVortex DivyanshuVortex requested a review from a team as a code owner April 25, 2026 06:13
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
apps/meteor/server/startup/migrations/v336.ts (1)

9-13: Include the underlying error in the warning log.

The current catch {} discards the error entirely, so genuine failures (connectivity, permissions, etc.) are indistinguishable from the expected "index not found" case and the migration silently reports success. Capture the error and include it in the log so operators can diagnose unexpected failures.

♻️ Proposed refactor
-		try {
-			await ReadReceipts.col.dropIndex('roomId_1_userId_1_messageId_1');
-		} catch {
-			console.warn('Error dropping index roomId_1_userId_1_messageId_1 from Read Receipts collection, it might have already been dropped.');
-		}
+		try {
+			await ReadReceipts.col.dropIndex('roomId_1_userId_1_messageId_1');
+		} catch (error) {
+			console.warn('Error dropping index roomId_1_userId_1_messageId_1 from Read Receipts collection, it might have already been dropped.', error);
+		}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/meteor/server/startup/migrations/v336.ts` around lines 9 - 13, Update
the catch block around the ReadReceipts.col.dropIndex call to capture the caught
error and include it in the warning log so unexpected failures are visible;
specifically, change the bare catch to capture the error (e.g., catch (err)) and
pass that error (or err.message) into console.warn along with the existing
descriptive text for ReadReceipts.col.dropIndex('roomId_1_userId_1_messageId_1')
so operators can distinguish "index not found" from real failures.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@apps/meteor/server/startup/migrations/v336.ts`:
- Around line 9-13: Update the catch block around the ReadReceipts.col.dropIndex
call to capture the caught error and include it in the warning log so unexpected
failures are visible; specifically, change the bare catch to capture the error
(e.g., catch (err)) and pass that error (or err.message) into console.warn along
with the existing descriptive text for
ReadReceipts.col.dropIndex('roomId_1_userId_1_messageId_1') so operators can
distinguish "index not found" from real failures.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c9d21145-a536-491e-9cf9-f2c202fab426

📥 Commits

Reviewing files that changed from the base of the PR and between ceefff9 and a89b1ad.

📒 Files selected for processing (2)
  • apps/meteor/ee/server/models/raw/ReadReceipts.ts
  • apps/meteor/server/startup/migrations/v336.ts
💤 Files with no reviewable changes (1)
  • apps/meteor/ee/server/models/raw/ReadReceipts.ts
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/server/startup/migrations/v336.ts
🧠 Learnings (6)
📓 Common learnings
Learnt from: rodrigok
Repo: RocketChat/Rocket.Chat PR: 38623
File: apps/meteor/app/lib/server/functions/cleanRoomHistory.ts:146-149
Timestamp: 2026-04-18T12:32:53.425Z
Learning: In `apps/meteor/app/lib/server/functions/cleanRoomHistory.ts` (PR `#38623`), the read-receipt cleanup (both `ReadReceipts.removeByMessageIds` and `ReadReceiptsArchive.removeByMessageIds`) is intentionally only performed in the limited prune path (`limit && selectedMessageIds`). The unlimited/delete-all path (`limit === 0`) deliberately skips cleaning up orphaned read receipts in both hot and cold storage — this is by design. Do not flag this as a bug or missing cleanup in future reviews.
Learnt from: smirk-dev
Repo: RocketChat/Rocket.Chat PR: 39625
File: apps/meteor/app/api/server/v1/push.ts:85-97
Timestamp: 2026-03-14T14:58:58.834Z
Learning: In RocketChat/Rocket.Chat, the `push.token` POST/DELETE endpoints in `apps/meteor/app/api/server/v1/push.ts` were already migrated to the chained router API pattern on `develop` prior to PR `#39625`. `cleanTokenResult` (which strips `authToken` and returns `PushTokenResult`) and `isPushTokenPOSTProps`/`isPushTokenDELETEProps` validators already exist on `develop`. PR `#39625` only migrates `push.get` and `push.info` to the chained pattern. Do not flag `cleanTokenResult` or `PushTokenResult` as newly introduced behavior-breaking changes when reviewing this PR.
📚 Learning: 2026-04-18T12:32:53.425Z
Learnt from: rodrigok
Repo: RocketChat/Rocket.Chat PR: 38623
File: apps/meteor/app/lib/server/functions/cleanRoomHistory.ts:146-149
Timestamp: 2026-04-18T12:32:53.425Z
Learning: In `apps/meteor/app/lib/server/functions/cleanRoomHistory.ts` (PR `#38623`), the read-receipt cleanup (both `ReadReceipts.removeByMessageIds` and `ReadReceiptsArchive.removeByMessageIds`) is intentionally only performed in the limited prune path (`limit && selectedMessageIds`). The unlimited/delete-all path (`limit === 0`) deliberately skips cleaning up orphaned read receipts in both hot and cold storage — this is by design. Do not flag this as a bug or missing cleanup in future reviews.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-03-11T22:04:20.529Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 39545
File: apps/meteor/client/views/room/body/hooks/useHasNewMessages.ts:59-61
Timestamp: 2026-03-11T22:04:20.529Z
Learning: In `apps/meteor/client/views/room/body/hooks/useHasNewMessages.ts`, the `msg.u._id === uid` early-return in the `streamNewMessage` handler is intentional: the "New messages" indicator is designed to notify about messages from other users only. Self-sent messages — including those sent from a different session/device — are always skipped, by design. Do not flag this as a multi-session regression.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-01-17T01:51:47.764Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 38219
File: packages/core-typings/src/cloud/Announcement.ts:5-6
Timestamp: 2026-01-17T01:51:47.764Z
Learning: In packages/core-typings/src/cloud/Announcement.ts, the AnnouncementSchema.createdBy field intentionally overrides IBannerSchema.createdBy (object with _id and optional username) with a string enum ['cloud', 'system'] to match existing runtime behavior. This is documented as technical debt with a FIXME comment at apps/meteor/app/cloud/server/functions/syncWorkspace/handleCommsSync.ts:53 and should not be flagged as an error until the runtime behavior is corrected.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts

@DivyanshuVortex
Copy link
Copy Markdown
Author

@ggazzo , please have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant