feat(state): org-scoped audit trail and complete policy write audit

This commit is contained in:
sam committed 2026-10-25 20:49:20 +11:00
1 parent 166ae69d74
commit 5816c5cc53
9 files changed
+194 -37

No files matched your search

@@ -24,15 +24,29 @@ type AuditForRecordsParams struct {
EntryLimit int64
}
func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams) ([]SysAudit, error) {
type AuditForRecordsRow struct {
ID int64
At time.Time
TableName string
RecordKey string
Field string
OldValue []byte
NewValue []byte
RecordVersion int64
Actor string
Source string
RequestID string
}
func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams) ([]AuditForRecordsRow, error) {
rows, err := q.db.Query(ctx, auditForRecords, arg.TableName, arg.RecordKeys, arg.EntryLimit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []SysAudit
var items []AuditForRecordsRow
for rows.Next() {
var i SysAudit
var i AuditForRecordsRow
if err := rows.Scan(
&i.ID,
&i.At,
@@ -57,9 +71,10 @@ func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams
}
const insertResourceAudit = `-- name: InsertResourceAudit :exec
INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id)
INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id)
SELECT $1::timestamptz, 'resources', $2::text, d.field, d.old_value, d.new_value,
$3::bigint, $4::text, $5::text, $6::text
$3::bigint, $4::text, $5::text, $6::text,
$7::text
FROM audit_json_diff(
(SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h
WHERE h.org_id = $7::text AND h.kind = $8::text AND h.version = $9::bigint),