Skip to content

Commit 6c47bd2

Browse files
fix: Retention policy settings migration not running (#35101)
Co-authored-by: gabriellsh <40830821+gabriellsh@users.noreply.github.com>
1 parent 3c4a87d commit 6c47bd2

File tree

4 files changed

+95
-58
lines changed

4 files changed

+95
-58
lines changed

.changeset/sour-frogs-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rocket.chat/meteor": patch
3+
---
4+
5+
Fixes an issue with the retention policy max age settings not maintaning it's previous value when upgrading from version < 6.10

apps/meteor/server/settings/retention-policy.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ export const createRetentionSettings = () =>
7373
i18nDescription: 'RetentionPolicy_AppliesToChannels_Description',
7474
enableQuery: globalQuery,
7575
});
76+
await this.add('RetentionPolicy_MaxAge_Channels', 30, {
77+
type: 'int',
78+
public: true,
79+
hidden: true,
80+
i18nLabel: 'RetentionPolicy_MaxAge_Channels',
81+
enableQuery: [
82+
{
83+
_id: 'RetentionPolicy_AppliesToChannels',
84+
value: true,
85+
},
86+
globalQuery,
87+
],
88+
});
7689

7790
await this.add('RetentionPolicy_TTL_Channels', THIRTY_DAYS, {
7891
type: 'timespan',
@@ -94,6 +107,19 @@ export const createRetentionSettings = () =>
94107
i18nDescription: 'RetentionPolicy_AppliesToGroups_Description',
95108
enableQuery: globalQuery,
96109
});
110+
await this.add('RetentionPolicy_MaxAge_Groups', 30, {
111+
type: 'int',
112+
public: true,
113+
hidden: true,
114+
i18nLabel: 'RetentionPolicy_MaxAge_Groups',
115+
enableQuery: [
116+
{
117+
_id: 'RetentionPolicy_AppliesToGroups',
118+
value: true,
119+
},
120+
globalQuery,
121+
],
122+
});
97123

98124
await this.add('RetentionPolicy_TTL_Groups', THIRTY_DAYS, {
99125
type: 'timespan',
@@ -115,6 +141,20 @@ export const createRetentionSettings = () =>
115141
enableQuery: globalQuery,
116142
});
117143

144+
await this.add('RetentionPolicy_MaxAge_DMs', 30, {
145+
type: 'int',
146+
public: true,
147+
hidden: true,
148+
i18nLabel: 'RetentionPolicy_MaxAge_DMs',
149+
enableQuery: [
150+
{
151+
_id: 'RetentionPolicy_AppliesToDMs',
152+
value: true,
153+
},
154+
globalQuery,
155+
],
156+
});
157+
118158
await this.add('RetentionPolicy_TTL_DMs', THIRTY_DAYS, {
119159
type: 'timespan',
120160
public: true,

apps/meteor/server/startup/migrations/v319.ts

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,66 @@
1+
import { Settings } from '@rocket.chat/models';
2+
import type { UpdateResult } from 'mongodb';
3+
14
import { upsertPermissions } from '../../../app/authorization/server/functions/upsertPermissions';
5+
import { settings } from '../../../app/settings/server';
26
import { migrateDatabase, onServerVersionChange } from '../../lib/migrations';
37
import { ensureCloudWorkspaceRegistered } from '../cloudRegistration';
48

59
const { MIGRATION_VERSION = 'latest' } = process.env;
610

711
const [version, ...subcommands] = MIGRATION_VERSION.split(',');
812

13+
const maxAgeSettingMap = new Map([
14+
['RetentionPolicy_MaxAge_Channels', 'RetentionPolicy_TTL_Channels'],
15+
['RetentionPolicy_MaxAge_Groups', 'RetentionPolicy_TTL_Groups'],
16+
['RetentionPolicy_MaxAge_DMs', 'RetentionPolicy_TTL_DMs'],
17+
]);
18+
19+
const moveRetentionSetting = async () => {
20+
const convertDaysToMs = (days: number) => days * 24 * 60 * 60 * 1000;
21+
22+
const promises: Array<Promise<UpdateResult>> = [];
23+
await Settings.find(
24+
{ _id: { $in: Array.from(maxAgeSettingMap.keys()) }, value: { $ne: -1 } },
25+
{ projection: { _id: 1, value: 1 } },
26+
).forEach(({ _id, value }) => {
27+
const newSettingId = maxAgeSettingMap.get(_id);
28+
if (!newSettingId) {
29+
throw new Error(`moveRetentionSetting - Setting ${_id} equivalent does not exist`);
30+
}
31+
32+
const newValue = convertDaysToMs(Number(value));
33+
34+
promises.push(
35+
Settings.updateOne(
36+
{
37+
_id: maxAgeSettingMap.get(_id),
38+
},
39+
{
40+
$set: {
41+
value: newValue,
42+
},
43+
},
44+
),
45+
);
46+
47+
const currentCache = settings.getSetting(newSettingId);
48+
if (!currentCache) {
49+
return;
50+
}
51+
settings.set({ ...currentCache, value: newValue });
52+
});
53+
54+
await Promise.all(promises);
55+
await Settings.updateMany({ _id: { $in: Array.from(maxAgeSettingMap.keys()) } }, { $set: { value: -1 } });
56+
};
57+
958
export const performMigrationProcedure = async (): Promise<void> => {
1059
await migrateDatabase(version === 'latest' ? version : parseInt(version), subcommands);
1160
// perform operations when the server is starting with a different version
1261
await onServerVersionChange(async () => {
1362
await upsertPermissions();
1463
await ensureCloudWorkspaceRegistered();
64+
await moveRetentionSetting();
1565
});
1666
};

0 commit comments

Comments
 (0)