feat(controlplane): field-level sys_audit table, audit view on settings pages, whole-task time limit

This commit is contained in:
sam committed 2026-09-25 09:54:30 +10:00
1 parent 49d7527f1c
commit 8fea8e8686
43 files changed
+1176 -174

No files matched your search

@@ -0,0 +1,96 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: audit.sql
package postgresdb
import (
"context"
"time"
)
const auditForRecords = `-- name: AuditForRecords :many
SELECT id, at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id
FROM sys_audit
WHERE table_name = $1::text AND record_key = ANY($2::text[])
ORDER BY id DESC
LIMIT $3::bigint
`
type AuditForRecordsParams struct {
TableName string
RecordKeys []string
EntryLimit int64
}
func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams) ([]SysAudit, 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
for rows.Next() {
var i SysAudit
if err := rows.Scan(
&i.ID,
&i.At,
&i.TableName,
&i.RecordKey,
&i.Field,
&i.OldValue,
&i.NewValue,
&i.RecordVersion,
&i.Actor,
&i.Source,
&i.RequestID,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
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)
SELECT $1::timestamptz, 'resources', $2::text, d.field, d.old_value, d.new_value,
$3::bigint, $4::text, $5::text, $6::text
FROM audit_json_diff(
(SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h
WHERE h.kind = $2::text AND h.version = $7::bigint),
convert_from($8::bytea, 'UTF8')::jsonb
) d
ORDER BY d.field
`
type InsertResourceAuditParams struct {
At time.Time
Kind string
Version int64
Actor string
Source string
RequestID string
PreviousVersion int64
Value []byte
}
// Records the fields one resource write changed, against the version it
// replaced. Runs in the write's own transaction.
func (q *Queries) InsertResourceAudit(ctx context.Context, arg InsertResourceAuditParams) error {
_, err := q.db.Exec(ctx, insertResourceAudit,
arg.At,
arg.Kind,
arg.Version,
arg.Actor,
arg.Source,
arg.RequestID,
arg.PreviousVersion,
arg.Value,
)
return err
}