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

+43
View File
@@ -0,0 +1,43 @@
package postgres
import (
"context"
"encoding/json"
"fmt"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
// insertAudit writes one audit row inside the caller's transaction, attributed
// to the caller the RPC interceptor authenticated. table is the audited table's
// own name; nil oldValue is a create and nil newValue a delete.
func insertAudit(ctx context.Context, q *postgresdb.Queries, table, recordKey, field string, oldValue, newValue []byte, version int64, orgID string) error {
if err := q.InsertAudit(ctx, postgresdb.InsertAuditParams{
TableName: table,
RecordKey: recordKey,
Field: field,
OldValue: oldValue,
NewValue: newValue,
RecordVersion: version,
Actor: access.ActorFromContext(ctx),
Source: access.SourceFromContext(ctx),
OrgID: orgID,
}); err != nil {
return fmt.Errorf("audit %s %q: %w", table, recordKey, err)
}
return nil
}
// auditJSON encodes one side of an audit change. A nil value stays nil, which
// the column stores as NULL: absent is not the same fact as JSON null.
func auditJSON(value any) []byte {
if value == nil {
return nil
}
out, err := json.Marshal(value)
if err != nil {
return nil
}
return out
}
@@ -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,
@@ -10,6 +10,15 @@ FROM audit_json_diff(
) d
ORDER BY d.field;
-- name: InsertAudit :exec
-- 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.
INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id)
VALUES (now(), sqlc.arg(table_name)::text, sqlc.arg(record_key)::text, sqlc.arg(field)::text,
sqlc.arg(old_value)::jsonb, sqlc.arg(new_value)::jsonb, sqlc.arg(record_version)::bigint,
sqlc.arg(actor)::text, sqlc.arg(source)::text, sqlc.arg(request_id)::text, sqlc.arg(org_id)::text);
-- name: AuditForRecords :many
SELECT id, at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id
FROM sys_audit