Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dry-gifts-report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rocket.chat/models': patch
'@rocket.chat/meteor': patch
---

Adds migration to remove redundant statistics index after TTL index consolidation.
5 changes: 5 additions & 0 deletions .changeset/young-humans-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/models': patch
---

Adds automatic cleanup of statistics collection with 1-year retention via TTL index.
1 change: 1 addition & 0 deletions apps/meteor/server/startup/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ import './v332';
import './v333';
import './v334';
import './v335';
import './v336';

export * from './xrun';
15 changes: 15 additions & 0 deletions apps/meteor/server/startup/migrations/v336.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Statistics } from '@rocket.chat/models';

import { addMigration } from '../../lib/migrations';

addMigration({
version: 336,
name: 'Remove redundant statistics createdAt descending index',
async up() {
try {
await Statistics.col.dropIndex('createdAt_-1');
} catch {
// Index might not exist on fresh installations
}
Comment on lines +9 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Check how other migrations in this repo handle dropIndex errors
rg -n -A5 'dropIndex' --glob '**/migrations/*.ts' | head -100

Repository: RocketChat/Rocket.Chat

Length of output: 2852


🏁 Script executed:

#!/bin/bash
# Also check the specific file and its context
cat -n apps/meteor/server/startup/migrations/v336.ts

Repository: RocketChat/Rocket.Chat

Length of output: 518


🌐 Web query:

MongoDB error code 27 IndexNotFound official documentation

💡 Result:

MongoDB error code 27 = IndexNotFound in the official MongoDB Server documentation under Reference → Error Codes. (mongodb.com)

Citations:


Catch block swallows all errors, not just "index not found".

The empty catch will silently ignore any error (e.g., authentication failures, network issues), not just the expected IndexNotFound. The codebase already establishes the proper pattern in v323.ts: check for MongoDB error code 27 and re-throw unexpected errors. Match that pattern here.

Proposed fix
 		try {
 			await Statistics.col.dropIndex('createdAt_-1');
-		} catch {
-			// Index might not exist on fresh installations
+		} catch (e: any) {
+			if (e?.code !== 27 && e?.codeName !== 'IndexNotFound') {
+				throw e;
+			}
 		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
try {
await Statistics.col.dropIndex('createdAt_-1');
} catch {
// Index might not exist on fresh installations
}
try {
await Statistics.col.dropIndex('createdAt_-1');
} catch (e: any) {
if (e?.code !== 27 && e?.codeName !== 'IndexNotFound') {
throw e;
}
}
🤖 Prompt for AI Agents
In `@apps/meteor/server/startup/migrations/v336.ts` around lines 9 - 13, Replace
the empty catch in the v336.ts migration around
Statistics.col.dropIndex('createdAt_-1') with error handling that only swallows
MongoDB "index not found" errors (code 27) and re-throws all other errors; i.e.,
catch the thrown error (e) and if e?.code !== 27 then throw e so
authentication/network or other failures surface, matching the pattern used in
v323.ts.

},
});
2 changes: 1 addition & 1 deletion packages/models/src/models/Statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class StatisticsRaw extends BaseRaw<IStats> implements IStatisticsModel {
}

protected override modelIndexes(): IndexDescription[] {
return [{ key: { createdAt: -1 } }];
return [{ key: { createdAt: 1 }, expireAfterSeconds: 365 * 24 * 60 * 60 }];
}

async findLast(): Promise<IStats> {
Expand Down
Loading