db

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package db is the Postgres implementation of auditlog.Store. It maps between the domain AuditLog and its row representation and uses the skit dbx helpers for query logging and error translation. Inline const queries run against the fixed audit_log table.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Schema

func Schema() string

Schema returns the DDL creating the audit_log table and its indexes. Apply it in a migration, or call Store.EnsureSchema in tests. The UNIQUE constraint on (model_type, model_id, version) makes the read-modify-write in Core.Create safe under concurrency: a racing insert at the same version fails instead of duplicating.

Types

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store implements auditlog.Store against Postgres.

func NewStore

func NewStore(log *logger.Logger, db *sqlx.DB) *Store

NewStore builds a Store. Pass a *sqlx.DB for pool-backed use.

func (*Store) DeleteVersions

func (s *Store) DeleteVersions(ctx context.Context, modelType, modelID string, versions []int) (int, error)

DeleteVersions removes the given versions of a model and returns the count deleted.

func (*Store) EnsureSchema

func (s *Store) EnsureSchema(ctx context.Context) error

EnsureSchema creates the audit_log table and indexes if absent.

Prefer running the DDL as a migration in production — goose already serializes migrations with its own advisory lock. EnsureSchema is for tests and simple boot-time setup; when several replicas call it concurrently, a bare CREATE TABLE IF NOT EXISTS can still race ("tuple concurrently updated"), so the DDL runs under a transaction-level advisory lock that auto-releases on commit. A tx-bound store (via WithTx) just runs the DDL — the caller owns the transaction and any locking.

func (*Store) OverThreshold

func (s *Store) OverThreshold(ctx context.Context, threshold, limit int) ([]auditlog.ModelRef, error)

OverThreshold lists models whose stored-version count exceeds threshold, most-versioned first, up to limit.

func (*Store) QueryHistoryByModelID

func (s *Store) QueryHistoryByModelID(ctx context.Context, modelType, modelID string) ([]auditlog.AuditLog, error)

QueryHistoryByModelID returns every version of a model in ascending order.

func (*Store) QueryLastByModelID

func (s *Store) QueryLastByModelID(ctx context.Context, modelType, modelID string) (auditlog.AuditLog, error)

QueryLastByModelID returns the most recent entry for a model.

func (*Store) QueryModelByVersion

func (s *Store) QueryModelByVersion(ctx context.Context, modelType, modelID string, ver int) (auditlog.AuditLog, error)

QueryModelByVersion returns a single version of a model.

func (*Store) Save

func (s *Store) Save(ctx context.Context, al auditlog.AuditLog) error

Save inserts a new audit log entry.

func (*Store) Versions

func (s *Store) Versions(ctx context.Context, modelType, modelID string) ([]int, error)

Versions returns the stored version numbers for a model, ascending.

func (*Store) WithTx

func (s *Store) WithTx(tx sqlx.ExtContext) auditlog.Store

WithTx returns a sibling Store whose queries run on tx, so an audit write can commit atomically with the caller's business change.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL