Skip to content

feat!: add migration to drop legacy unique index from read_receipts#40242

Closed
AYUSHSAHU2004 wants to merge 3 commits intoRocketChat:developfrom
AYUSHSAHU2004:fix/drop-read-receipts-index
Closed

feat!: add migration to drop legacy unique index from read_receipts#40242
AYUSHSAHU2004 wants to merge 3 commits intoRocketChat:developfrom
AYUSHSAHU2004:fix/drop-read-receipts-index

Conversation

@AYUSHSAHU2004
Copy link
Copy Markdown

@AYUSHSAHU2004 AYUSHSAHU2004 commented Apr 21, 2026

Description

Adds a database migration to remove the legacy unique index on
(roomId, userId, messageId) from the read_receipts collection.

Why

The unique index was removed from the model as part of changes where
uniqueness is now handled via the _id field. However, this index may still
exist in databases from older versions, causing conflicts and potential
duplicate key errors during operations like archiving.

Changes

  • Added migration v336 to safely drop the legacy index
  • The migration checks for the existence of the index before removing it
  • Ensures backward compatibility for users upgrading to newer versions

Notes

  • The migration is idempotent and will not fail if the index does not exist

Summary by CodeRabbit

  • Chores
    • Conditionally removed a legacy unique database index on read receipts to improve reliability and prevent potential conflicts during message read tracking.

@AYUSHSAHU2004 AYUSHSAHU2004 requested a review from a team as a code owner April 21, 2026 09:28
@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot Bot commented Apr 21, 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 targeting the wrong base branch. It should target 9.0.0, but it targets 8.4.0

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 21, 2026

⚠️ No Changeset found

Latest commit: 405488f

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

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 21, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 21, 2026

Walkthrough

Adds migration v336 that scans the raw MongoDB read_receipts collection for a legacy unique index whose key is exactly { roomId: 1, userId: 1, messageId: 1 } and drops that index if found.

Changes

Cohort / File(s) Summary
Database Migration
apps/meteor/server/startup/migrations/v336.ts
Adds migration v336: obtains read_receipts raw collection, enumerates indexes, detects a unique index with keys roomId: 1, userId: 1, messageId: 1, and calls collection.dropIndex(...) when matched.
Migration Registry
apps/meteor/server/startup/migrations/index.ts
Imports and registers the new v336 migration alongside existing migrations (one-line addition to include the new module).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
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.
Title check ✅ Passed The title accurately describes the main change: adding a migration to drop a legacy unique index from read_receipts collection, which matches the changeset content.

✏️ 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.

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.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/meteor/server/startup/migrations/v336.ts`:
- Around line 12-18: The current predicate used to find targetIndex via
indexes.find(...) is too permissive and may match compound indexes that merely
contain roomId/userId/messageId; update the predicate for targetIndex to ensure
the index key exactly equals the legacy key shape and order and that
index.unique === true (e.g., compare Object.keys(idx.key) and the values/order
or JSON.stringify on the key object against the exact {
roomId:1,userId:1,messageId:1 } shape, and check idx.unique === true) so only
the exact legacy unique index is selected for dropping.
- Around line 4-24: The migration file (v336) registers itself via addMigration
but isn't imported by the migrations loader, so add an import for the module
that defines the v336 migration (the same place you import './v335') so the
side-effect registration runs; locate the migrations loader that currently
imports ./v335 and add the equivalent import for v336 (i.e., import './v336') so
the migration is registered and executed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4d120760-ed89-4d0e-92a1-cd59f2872a07

📥 Commits

Reviewing files that changed from the base of the PR and between 6a49770 and 5e7cdc0.

📒 Files selected for processing (1)
  • apps/meteor/server/startup/migrations/v336.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:50.305Z
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:50.305Z
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:50.305Z
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

Comment thread apps/meteor/server/startup/migrations/v336.ts
Comment thread apps/meteor/server/startup/migrations/v336.ts
@coderabbitai coderabbitai Bot removed the type: bug label Apr 21, 2026
Copy link
Copy Markdown
Member

@ggazzo ggazzo left a comment

Choose a reason for hiding this comment

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

should be pointing to branch release-9.0.0

@ggazzo ggazzo added this to the 9.0.0 milestone Apr 22, 2026
@ggazzo ggazzo changed the title feat: add migration to drop legacy unique index from read_receipts feat!: add migration to drop legacy unique index from read_receipts Apr 22, 2026
@AYUSHSAHU2004
Copy link
Copy Markdown
Author

AYUSHSAHU2004 commented Apr 22, 2026

Closing this PR as I have created a new one targeting the release-9.0.0 branch.

New PR: #40292

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.

3 participants