diff --git a/.changeset/dry-gifts-report.md b/.changeset/dry-gifts-report.md new file mode 100644 index 0000000000000..353dfaa2c857f --- /dev/null +++ b/.changeset/dry-gifts-report.md @@ -0,0 +1,6 @@ +--- +'@rocket.chat/models': patch +'@rocket.chat/meteor': patch +--- + +Adds migration to remove redundant statistics index after TTL index consolidation. diff --git a/.changeset/young-humans-stare.md b/.changeset/young-humans-stare.md new file mode 100644 index 0000000000000..c887f9479f673 --- /dev/null +++ b/.changeset/young-humans-stare.md @@ -0,0 +1,5 @@ +--- +'@rocket.chat/models': patch +--- + +Adds automatic cleanup of statistics collection with 1-year retention via TTL index. diff --git a/apps/meteor/server/startup/migrations/index.ts b/apps/meteor/server/startup/migrations/index.ts index d10693f181110..deadd9ff88f59 100644 --- a/apps/meteor/server/startup/migrations/index.ts +++ b/apps/meteor/server/startup/migrations/index.ts @@ -41,5 +41,6 @@ import './v332'; import './v333'; import './v334'; import './v335'; +import './v336'; export * from './xrun'; diff --git a/apps/meteor/server/startup/migrations/v336.ts b/apps/meteor/server/startup/migrations/v336.ts new file mode 100644 index 0000000000000..2f0e174e900ab --- /dev/null +++ b/apps/meteor/server/startup/migrations/v336.ts @@ -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 + } + }, +}); diff --git a/packages/models/src/models/Statistics.ts b/packages/models/src/models/Statistics.ts index 8a1dae77dc76e..38f8e82c6afbf 100644 --- a/packages/models/src/models/Statistics.ts +++ b/packages/models/src/models/Statistics.ts @@ -10,7 +10,7 @@ export class StatisticsRaw extends BaseRaw implements IStatisticsModel { } protected override modelIndexes(): IndexDescription[] { - return [{ key: { createdAt: -1 } }]; + return [{ key: { createdAt: 1 }, expireAfterSeconds: 365 * 24 * 60 * 60 }]; } async findLast(): Promise {