-- name: InsertAuditEvent :exec INSERT INTO audit_events (kind, org_id, actor_user_id, actor_token_id, action, target_id, details, status, ip, user_agent, request_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11); -- name: ListAuditEvents :many -- Newest first, paged by time. kind narrows to one kind; the row policy -- already limits what the scope can see. SELECT e.id, e.time, e.kind, e.org_id, e.actor_user_id, e.actor_token_id, e.action, e.target_id, e.details, e.status, coalesce(host(e.ip), '')::text AS ip, coalesce(e.user_agent, '') AS user_agent, coalesce(e.request_id, '') AS request_id, coalesce(u.email::text, '')::text AS actor_email, coalesce(u.display_name, '') AS actor_name FROM audit_events e LEFT JOIN users u ON u.id = e.actor_user_id WHERE e.kind = sqlc.arg(kind) AND (sqlc.narg(org_id)::uuid IS NULL OR e.org_id = sqlc.narg(org_id)) AND (sqlc.narg(actor)::uuid IS NULL OR e.actor_user_id = sqlc.narg(actor)) AND (sqlc.narg(before)::timestamptz IS NULL OR e.time < sqlc.narg(before)) ORDER BY e.time DESC LIMIT sqlc.arg(max_rows); -- name: PurgeAuditEvents :execrows DELETE FROM audit_events WHERE time < $1;