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 ¶
- func Schema() string
- type Store
- func (s *Store) DeleteVersions(ctx context.Context, modelType, modelID string, versions []int) (int, error)
- func (s *Store) EnsureSchema(ctx context.Context) error
- func (s *Store) OverThreshold(ctx context.Context, threshold, limit int) ([]auditlog.ModelRef, error)
- func (s *Store) QueryHistoryByModelID(ctx context.Context, modelType, modelID string) ([]auditlog.AuditLog, error)
- func (s *Store) QueryLastByModelID(ctx context.Context, modelType, modelID string) (auditlog.AuditLog, error)
- func (s *Store) QueryModelByVersion(ctx context.Context, modelType, modelID string, ver int) (auditlog.AuditLog, error)
- func (s *Store) Save(ctx context.Context, al auditlog.AuditLog) error
- func (s *Store) Versions(ctx context.Context, modelType, modelID string) ([]int, error)
- func (s *Store) WithTx(tx sqlx.ExtContext) auditlog.Store
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. Call Store.EnsureSchema at startup to provision it (advisory-lock guarded, replica- safe); Schema is exposed for embedding the DDL in your own migration tooling. 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 (*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 ¶
EnsureSchema creates the audit_log table and indexes if absent. It is safe to call at startup, including from several replicas at once: the DDL runs under a transaction-scoped advisory lock that auto-releases on commit, so concurrent boots serialize instead of racing on CREATE TABLE. 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.