-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "chore!: Remove old retention maxAge settings (#33797)"
This reverts commit 8188319.
- Loading branch information
1 parent
88be133
commit cd688d3
Showing
3 changed files
with
90 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,66 @@ | ||
import { Settings } from '@rocket.chat/models'; | ||
import type { UpdateResult } from 'mongodb'; | ||
|
||
import { upsertPermissions } from '../../../app/authorization/server/functions/upsertPermissions'; | ||
import { settings } from '../../../app/settings/server'; | ||
import { migrateDatabase, onServerVersionChange } from '../../lib/migrations'; | ||
import { ensureCloudWorkspaceRegistered } from '../cloudRegistration'; | ||
|
||
const { MIGRATION_VERSION = 'latest' } = process.env; | ||
|
||
const [version, ...subcommands] = MIGRATION_VERSION.split(','); | ||
|
||
const maxAgeSettingMap = new Map([ | ||
['RetentionPolicy_MaxAge_Channels', 'RetentionPolicy_TTL_Channels'], | ||
['RetentionPolicy_MaxAge_Groups', 'RetentionPolicy_TTL_Groups'], | ||
['RetentionPolicy_MaxAge_DMs', 'RetentionPolicy_TTL_DMs'], | ||
]); | ||
|
||
const moveRetentionSetting = async () => { | ||
const convertDaysToMs = (days: number) => days * 24 * 60 * 60 * 1000; | ||
|
||
const promises: Array<Promise<UpdateResult>> = []; | ||
await Settings.find( | ||
{ _id: { $in: Array.from(maxAgeSettingMap.keys()) }, value: { $ne: -1 } }, | ||
{ projection: { _id: 1, value: 1 } }, | ||
).forEach(({ _id, value }) => { | ||
const newSettingId = maxAgeSettingMap.get(_id); | ||
if (!newSettingId) { | ||
throw new Error(`moveRetentionSetting - Setting ${_id} equivalent does not exist`); | ||
} | ||
|
||
const newValue = convertDaysToMs(Number(value)); | ||
|
||
promises.push( | ||
Settings.updateOne( | ||
{ | ||
_id: maxAgeSettingMap.get(_id), | ||
}, | ||
{ | ||
$set: { | ||
value: newValue, | ||
}, | ||
}, | ||
), | ||
); | ||
|
||
const currentCache = settings.getSetting(newSettingId); | ||
if (!currentCache) { | ||
return; | ||
} | ||
settings.set({ ...currentCache, value: newValue }); | ||
}); | ||
|
||
await Promise.all(promises); | ||
await Settings.updateMany({ _id: { $in: Array.from(maxAgeSettingMap.keys()) } }, { $set: { value: -1 } }); | ||
}; | ||
|
||
export const performMigrationProcedure = async (): Promise<void> => { | ||
await migrateDatabase(version === 'latest' ? version : parseInt(version), subcommands); | ||
// perform operations when the server is starting with a different version | ||
await onServerVersionChange(async () => { | ||
await upsertPermissions(); | ||
await ensureCloudWorkspaceRegistered(); | ||
await moveRetentionSetting(); | ||
}); | ||
}; |