store

package
v1.19.1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package store is the persistent-state layer for compliancekit's serve-mode daemon. Phase 1 lands the schema + migration runner + the Store abstraction that phase 2's Postgres backend will re-implement.

The schema lives under migrations/ as plain SQL files. Both the sqlite backend (phase 1) and the postgres backend (phase 2) consume the same files; portable SQL is enforced by review + integration tests against both engines.

IDs are TEXT (uuid4) for portability across SQLite + Postgres; timestamps are TEXT (RFC-3339) for the same reason. See migrations/0001_initial.sql for the full schema rationale.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsMissingMigration

func IsMissingMigration(err error) bool

Sentinel for callers that want to detect the "fresh DB" path.

Types

type Driver

type Driver string

Driver identifies the underlying engine. Lets repository code branch on dialect when SQLite + Postgres diverge (e.g. JSONB vs TEXT JSON, RETURNING, LISTEN/NOTIFY).

const (
	DriverSQLite   Driver = "sqlite"
	DriverPostgres Driver = "postgres"
)

type Store

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

Store is the abstract handle every daemon component consumes. Both the sqlite and postgres backends satisfy this interface. Phase 6/7 (REST API) and phase 11 (UI shell) layer Repositories on top of this in their respective packages.

func OpenPostgres

func OpenPostgres(ctx context.Context, dsn string) (*Store, error)

OpenPostgres returns a Store backed by jackc/pgx/v5/stdlib (the canonical Go PG driver, used via the standard database/sql interface so repository code stays driver-agnostic).

dsn accepts the libpq DSN forms pgx understands:

postgres://user:pass@host:port/db?sslmode=require
postgresql://user:pass@host:port/db
host=... user=... password=... dbname=... sslmode=...

Connection-pool sizing is set conservatively for the v1.3 workload (the daemon's reads dominate; writes happen via the worker pool from phase 8). Operators with high-throughput needs can tune via future Config knobs.

func OpenSQLite

func OpenSQLite(ctx context.Context, path string) (*Store, error)

OpenSQLite returns a Store backed by modernc.org/sqlite (pure Go, no CGO). path may be a filesystem path, ":memory:" for tests, or "file::memory:?cache=shared" for cross-connection in-memory state.

PRAGMA settings applied at open time:

journal_mode=WAL   — concurrent reads + single-writer with no
                     blocking. Required for the v1.3 worker pool
                     which writes while the UI reads.
foreign_keys=ON    — SQLite defaults to OFF; the schema relies on
                     CASCADE / SET NULL semantics so this must be
                     on for every connection.
synchronous=NORMAL — durability/perf trade matched to WAL; safe
                     against power loss (just may rewind to the
                     last fsync).
busy_timeout=5000  — five-second wait for the writer lock before
                     ERR; tests + concurrent webhook receivers
                     need this.

func (*Store) Close

func (s *Store) Close() error

Close releases the underlying connection pool.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB returns the underlying *sql.DB for code paths that need direct SQL access (migrations, raw queries, transactions). Repository packages should prefer adding methods on Store over reaching for DB() — but the escape hatch is here when needed.

func (*Store) Driver

func (s *Store) Driver() Driver

Driver reports the active backend.

func (*Store) MigrateUp

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

MigrateUp applies every pending migration in order. Idempotent — re-running after every migration has been applied is a no-op. Errors fail fast; the partial state of a failed migration is left in place (we don't roll back) so the operator can inspect.

Concurrent calls are serialized via SQLite's writer lock; on Postgres we take a session-scoped advisory lock so two daemon processes pointed at the same DB can't race the migration apply.

func (*Store) Ping

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

Ping verifies the connection is alive. Used by /health (when phase 6 makes it dependency-aware) and by tests.

func (*Store) Version

func (s *Store) Version(ctx context.Context) (int, error)

Version returns the highest migration version applied. Exposed for the /health endpoint and CLI helpers.

Jump to

Keyboard shortcuts

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