Files
agentic-coding-workflow/internal/audit/queries/audit.sql
T
2026-06-22 08:55:57 +08:00

33 lines
1.8 KiB
SQL

-- name: InsertAuditLog :exec
INSERT INTO audit_logs (user_id, action, target_type, target_id, ip, metadata)
VALUES ($1, $2, $3, $4, $5, $6);
-- name: ListAuditLog :many
-- Paginated read with optional filters. NULL filter args match everything
-- (sqlc.narg renders as a nullable parameter; COALESCE/IS NULL short-circuits).
SELECT id, user_id, action, target_type, target_id, ip, metadata, created_at
FROM audit_logs
WHERE (sqlc.narg('user_id')::uuid IS NULL OR user_id = sqlc.narg('user_id'))
AND (sqlc.narg('action')::text IS NULL OR action = sqlc.narg('action'))
AND (sqlc.narg('action_prefix')::text IS NULL OR action LIKE sqlc.narg('action_prefix') || '%')
AND (sqlc.narg('target_type')::text IS NULL OR target_type = sqlc.narg('target_type'))
AND (sqlc.narg('target_id')::text IS NULL OR target_id = sqlc.narg('target_id'))
AND (sqlc.narg('from_ts')::timestamptz IS NULL OR created_at >= sqlc.narg('from_ts'))
AND (sqlc.narg('to_ts')::timestamptz IS NULL OR created_at < sqlc.narg('to_ts'))
ORDER BY created_at DESC, id DESC
LIMIT $1 OFFSET $2;
-- name: CountAuditLog :one
SELECT COUNT(*)
FROM audit_logs
WHERE (sqlc.narg('user_id')::uuid IS NULL OR user_id = sqlc.narg('user_id'))
AND (sqlc.narg('action')::text IS NULL OR action = sqlc.narg('action'))
AND (sqlc.narg('action_prefix')::text IS NULL OR action LIKE sqlc.narg('action_prefix') || '%')
AND (sqlc.narg('target_type')::text IS NULL OR target_type = sqlc.narg('target_type'))
AND (sqlc.narg('target_id')::text IS NULL OR target_id = sqlc.narg('target_id'))
AND (sqlc.narg('from_ts')::timestamptz IS NULL OR created_at >= sqlc.narg('from_ts'))
AND (sqlc.narg('to_ts')::timestamptz IS NULL OR created_at < sqlc.narg('to_ts'));
-- name: DeleteAuditLogsBefore :execrows
DELETE FROM audit_logs WHERE created_at < $1;