mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-19 15:30:00 +00:00
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
![]() |
import { getRepository, In } from "typeorm";
|
||
|
import moment from "moment-timezone";
|
||
|
import { UsernameHistoryEntry } from "../entities/UsernameHistoryEntry";
|
||
|
import { DAYS, DBDateFormat } from "../../utils";
|
||
|
import { connection } from "../db";
|
||
|
|
||
|
export const USERNAME_RETENTION_PERIOD = 30 * DAYS;
|
||
|
const CLEAN_PER_LOOP = 500;
|
||
|
|
||
|
export async function cleanupUsernames(): Promise<number> {
|
||
|
let cleaned = 0;
|
||
|
|
||
|
const usernameHistoryRepository = getRepository(UsernameHistoryEntry);
|
||
|
const dateThreshold = moment()
|
||
|
.subtract(USERNAME_RETENTION_PERIOD, "ms")
|
||
|
.format(DBDateFormat);
|
||
|
|
||
|
// Clean old usernames (USERNAME_RETENTION_PERIOD)
|
||
|
let rows;
|
||
|
do {
|
||
|
rows = await connection.query(
|
||
|
`
|
||
|
SELECT id
|
||
|
FROM username_history
|
||
|
WHERE timestamp < ?
|
||
|
LIMIT ${CLEAN_PER_LOOP}
|
||
|
`,
|
||
|
[dateThreshold],
|
||
|
);
|
||
|
|
||
|
if (rows.length > 0) {
|
||
|
console.log(
|
||
|
"ids",
|
||
|
rows.map(r => r.id),
|
||
|
);
|
||
|
await usernameHistoryRepository.delete({
|
||
|
id: In(rows.map(r => r.id)),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
cleaned += rows.length;
|
||
|
} while (rows.length === CLEAN_PER_LOOP);
|
||
|
|
||
|
return cleaned;
|
||
|
}
|