mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Add username/nickname history retention periods
This commit is contained in:
parent
a6e650810c
commit
de71520747
8 changed files with 253 additions and 111 deletions
41
backend/src/data/cleanup/nicknames.ts
Normal file
41
backend/src/data/cleanup/nicknames.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { getRepository, In } from "typeorm";
|
||||
import moment from "moment-timezone";
|
||||
import { NicknameHistoryEntry } from "../entities/NicknameHistoryEntry";
|
||||
import { DAYS, DBDateFormat } from "../../utils";
|
||||
import { connection } from "../db";
|
||||
|
||||
export const NICKNAME_RETENTION_PERIOD = 30 * DAYS;
|
||||
const CLEAN_PER_LOOP = 500;
|
||||
|
||||
export async function cleanupNicknames(): Promise<number> {
|
||||
let cleaned = 0;
|
||||
|
||||
const nicknameHistoryRepository = getRepository(NicknameHistoryEntry);
|
||||
const dateThreshold = moment()
|
||||
.subtract(NICKNAME_RETENTION_PERIOD, "ms")
|
||||
.format(DBDateFormat);
|
||||
|
||||
// Clean old nicknames (NICKNAME_RETENTION_PERIOD)
|
||||
let rows;
|
||||
do {
|
||||
rows = await connection.query(
|
||||
`
|
||||
SELECT id
|
||||
FROM nickname_history
|
||||
WHERE timestamp < ?
|
||||
LIMIT ${CLEAN_PER_LOOP}
|
||||
`,
|
||||
[dateThreshold],
|
||||
);
|
||||
|
||||
if (rows.length > 0) {
|
||||
await nicknameHistoryRepository.delete({
|
||||
id: In(rows.map(r => r.id)),
|
||||
});
|
||||
}
|
||||
|
||||
cleaned += rows.length;
|
||||
} while (rows.length === CLEAN_PER_LOOP);
|
||||
|
||||
return cleaned;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue