Skip to content

feat(audit): nightly retention purge for audit_log rows#49

Open
jusso-dev wants to merge 1 commit into
mainfrom
claude/issue-41-audit-log-purge
Open

feat(audit): nightly retention purge for audit_log rows#49
jusso-dev wants to merge 1 commit into
mainfrom
claude/issue-41-audit-log-purge

Conversation

@jusso-dev

Copy link
Copy Markdown
Owner

Summary

Closes #41. The audit_log table and per-tenant organisations.audit_retention_days column already shipped in #42; this adds the deletion job that respects them.

  • New lib/audit/purge.ts with purgeAuditLogsForOrganisation and purgeExpiredAuditLogs.
  • Deletion is batched (10k rows per statement, via WITH target_rows AS (... LIMIT ...) DELETE ...) so we never hold a long lock on audit_log.
  • Looped per org until a sub-batch returns, with a 1000-batch safety cap.
  • Per-org counts are emitted via structured logs ([audit-purge] org=… retention=…d cutoff=… deleted=… batches=…).
  • New Inngest function audit-log-retention-purge registered alongside eventRetentionSweep and scheduled at 0 3 * * * Australia/Sydney — 30 min after the existing PII scrub so the two jobs don't fight for the table.

Test plan

  • Insert audit rows with created_at straddling the cutoff, run purgeAuditLogsForOrganisation, confirm only rows older than the org's retention window are removed.
  • Confirm the Inngest dashboard shows audit-log-retention-purge registered after deploy.
  • Confirm structured logs show per-org counts on the first nightly run.

https://claude.ai/code/session_01PiiqDRQJdW1sBLvEmZ3GBC


Generated by Claude Code

Adds a per-organisation purge that respects each tenant's
audit_retention_days. Runs nightly at 03:00 Australia/Sydney as an
Inngest cron (one hour after the existing PII scrub) and deletes in
batches of 10k so it never holds a long lock on audit_log. Per-org
counts are emitted via structured logs so operators can confirm what
was removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit log retention purge job

2 participants