150 lines
4.1 KiB
Go
150 lines
4.1 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: audit.sql
|
|
|
|
package postgresdb
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
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,
|
|
$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),
|
|
convert_from($10::bytea, 'UTF8')::jsonb
|
|
) d
|
|
ORDER BY d.field
|
|
`
|
|
|
|
type InsertResourceAuditParams struct {
|
|
At time.Time
|
|
RecordKey string
|
|
Version int64
|
|
Actor string
|
|
Source string
|
|
RequestID string
|
|
OrgID string
|
|
Kind string
|
|
PreviousVersion int64
|
|
Value []byte
|
|
}
|
|
|
|
func (q *Queries) InsertResourceAudit(ctx context.Context, arg InsertResourceAuditParams) error {
|
|
_, err := q.db.Exec(ctx, insertResourceAudit,
|
|
arg.At,
|
|
arg.RecordKey,
|
|
arg.Version,
|
|
arg.Actor,
|
|
arg.Source,
|
|
arg.RequestID,
|
|
arg.OrgID,
|
|
arg.Kind,
|
|
arg.PreviousVersion,
|
|
arg.Value,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const lockAuditRecord = `-- name: LockAuditRecord :exec
|
|
SELECT pg_advisory_xact_lock(hashtextextended($1::text || ':' || $2::text, 0))
|
|
`
|
|
|
|
type LockAuditRecordParams struct {
|
|
TableName string
|
|
RecordKey string
|
|
}
|
|
|
|
// Serialises a read-modify-write on one audited record so the old side the
|
|
// audit row reports cannot change between the read and the write. Row locks
|
|
// cannot hold a key that does not exist yet; this can.
|
|
func (q *Queries) LockAuditRecord(ctx context.Context, arg LockAuditRecordParams) error {
|
|
_, err := q.db.Exec(ctx, lockAuditRecord, arg.TableName, arg.RecordKey)
|
|
return err
|
|
}
|
|
|
|
const pruneAuditEventBatch = `-- name: PruneAuditEventBatch :execrows
|
|
DELETE FROM events
|
|
WHERE id IN (
|
|
SELECT id FROM events
|
|
WHERE kind = ANY($1::text[])
|
|
AND at < $2::timestamptz
|
|
ORDER BY at
|
|
LIMIT $3::bigint
|
|
)
|
|
`
|
|
|
|
type PruneAuditEventBatchParams struct {
|
|
Kinds []string
|
|
Cutoff time.Time
|
|
Batch int64
|
|
}
|
|
|
|
// One bounded delete of the observability kinds the sweep allows, oldest
|
|
// first. The kind list is code, so no stored setting can widen it.
|
|
func (q *Queries) PruneAuditEventBatch(ctx context.Context, arg PruneAuditEventBatchParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, pruneAuditEventBatch, arg.Kinds, arg.Cutoff, arg.Batch)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const tryAuditPruneLock = `-- name: TryAuditPruneLock :one
|
|
SELECT pg_try_advisory_xact_lock(hashtextextended('archie.audit.prune', 0))
|
|
`
|
|
|
|
// The retention sweep's transaction-scoped lock. A second sweep skips its
|
|
// tick rather than queuing deletes behind this one.
|
|
func (q *Queries) TryAuditPruneLock(ctx context.Context) (bool, error) {
|
|
row := q.db.QueryRow(ctx, tryAuditPruneLock)
|
|
var pg_try_advisory_xact_lock bool
|
|
err := row.Scan(&pg_try_advisory_xact_lock)
|
|
return pg_try_advisory_xact_lock, err
|
|
}
|