mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import {db} from "@/db";
|
|
import {enforceRetentionCount} from "@/lib/tasks/database/retention-count";
|
|
import {enforceRetentionDays} from "@/lib/tasks/database/retention-days";
|
|
import {enforceRetentionGFS} from "@/lib/tasks/database/retention-gsf";
|
|
import {retentionPolicy} from "@/db/schema/06_database";
|
|
|
|
|
|
export const retentionCleanTask = async () => {
|
|
try {
|
|
const databases = await db.query.database.findMany({
|
|
with: {
|
|
retentionPolicy: true,
|
|
backups: true,
|
|
},
|
|
});
|
|
|
|
for (const db of databases) {
|
|
if (!db.retentionPolicy) continue; // no policy = skip
|
|
|
|
await enforceRetention(db.id, db.retentionPolicy);
|
|
}
|
|
} catch (e: any) {
|
|
console.error("Retention cleanup failed:", e);
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
export async function enforceRetention(
|
|
databaseId: string,
|
|
policy: typeof retentionPolicy.$inferSelect
|
|
) {
|
|
switch (policy.type) {
|
|
case "count":
|
|
await enforceRetentionCount(databaseId, policy.count ?? 7);
|
|
break;
|
|
|
|
case "days":
|
|
await enforceRetentionDays(databaseId, policy.days ?? 30);
|
|
break;
|
|
|
|
case "gfs":
|
|
await enforceRetentionGFS(databaseId, {
|
|
daily: policy.gfsDaily ?? 7,
|
|
weekly: policy.gfsWeekly ?? 4,
|
|
monthly: policy.gfsMonthly ?? 12,
|
|
yearly: policy.gfsYearly ?? 3,
|
|
});
|
|
break;
|
|
}
|
|
}
|