feat(state): shared audit write helper for domain tables

This commit is contained in:
sam committed 2026-10-25 20:49:47 +11:00
1 parent 9bedaaefb4
commit e3478ec400
3 files changed
+91

No files matched your search

@@ -70,6 +70,45 @@ func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams
return items, nil
}
const insertAudit = `-- name: InsertAudit :exec
INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id)
VALUES (now(), $1::text, $2::text, $3::text,
$4::jsonb, $5::jsonb, $6::bigint,
$7::text, $8::text, $9::text, $10::text)
`
type InsertAuditParams struct {
TableName string
RecordKey string
Field string
OldValue []byte
NewValue []byte
RecordVersion int64
Actor string
Source string
RequestID string
OrgID string
}
// The generic write for tables that are not control-plane resources. Callers
// pass the table's own name as table_name; old_value is nil for a create and
// new_value nil for a delete.
func (q *Queries) InsertAudit(ctx context.Context, arg InsertAuditParams) error {
_, err := q.db.Exec(ctx, insertAudit,
arg.TableName,
arg.RecordKey,
arg.Field,
arg.OldValue,
arg.NewValue,
arg.RecordVersion,
arg.Actor,
arg.Source,
arg.RequestID,
arg.OrgID,
)
return err
}
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, org_id)
SELECT $1::timestamptz, 'resources', $2::text, d.field, d.old_value, d.new_value,