store

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package store opens the SQLite databases (ADR-06), keeps their schemas migrated and knows where each file lives in the data directory (PLAN §4).

Index

Constants

View Source
const DefaultTenant = "default"

DefaultTenant is the only tenant of phase 1; every path is tenant-aware already, so more tenants (phase 5) need no migration of the layout.

View Source
const InstanceScope = "instance"

InstanceScope is the audit scope of <data>/audit.db.

Variables

View Source
var ErrWrongKind = errors.New("database file has the wrong type")

ErrWrongKind means a file is a different database kind than its place in the layout says, e.g. audit.db copied over pki.db.

View Source
var ErrWrongScope = errors.New("audit log has the wrong scope")

ErrWrongScope means an audit log belongs to another scope than its place in the layout, e.g. a tenant's audit.db copied to <data>/audit.db.

Kinds lists every database type; each has a migration set in schema/<kind>. A new type (acme in phase 2, metrics in phase 4) needs: a constant here, an entry in Kinds (and TenantKinds for per-tenant files), schema/<kind>/0001_init.sql and, once it has queries, a block in sqlc.yaml. The runner itself needs no change.

View Source
var TenantKinds = []Kind{KindPKI, KindAudit}

TenantKinds are the databases every tenant directory holds.

Functions

func Migrations

func Migrations(kind Kind) ([]migrate.Migration, error)

Migrations returns the migration set of a database kind.

func Snapshot

func Snapshot(ctx context.Context, src, dst string) error

Snapshot copies an existing database into dst (which must not exist) as one consistent snapshot with VACUUM INTO – also while a server writes to it (WAL). The copy is compact and in rollback-journal mode. It needs a connection without query_only, but changes nothing in src.

func TenantScope

func TenantScope(tenant string) string

TenantScope is the audit scope of tenants/<tenant>/audit.db.

func Tenants

func Tenants(ctx context.Context, l Layout) ([]systemdb.ListTenantsRow, error)

Tenants lists the tenants recorded in system.db without changing anything.

Types

type Applied

type Applied struct {
	Kind     Kind
	Path     string
	Versions []int
}

Applied lists the migrations Migrate applied to one file.

func Migrate

func Migrate(ctx context.Context, l Layout, tenant string, now func() time.Time) ([]Applied, error)

Migrate creates missing directories and database files and applies all pending migrations to system.db and the databases of tenant. It stops at the first file that is of the wrong kind, was changed or is newer.

type AuditLog

type AuditLog struct {
	Scope string
	Path  string
}

AuditLog is one audit database and its scope; the scope is part of the chain's starting value, so a log can't be moved to another scope.

type DB

type DB struct {
	Path  string
	Write *sql.DB // exactly one connection; transactions start IMMEDIATE
	Read  *sql.DB // read-only connections (PRAGMA query_only)
}

DB is one SQLite file with a single writer connection and a read pool.

SQLite allows one writer at a time. Routing every write through one connection serializes them inside the process instead of letting them collide on the file lock; WAL mode lets readers continue meanwhile.

func Open

func Open(ctx context.Context, path string) (*DB, error)

Open opens (and creates, if missing) the database at path. The caller decides whether a missing file is acceptable – see OpenExisting.

func OpenAuditLog

func OpenAuditLog(ctx context.Context, log AuditLog) (*DB, error)

OpenAuditLog opens an existing audit log read-only and checks that it is an audit database of the expected scope.

func OpenReadOnly

func OpenReadOnly(ctx context.Context, path string) (*DB, error)

OpenReadOnly opens an existing database for reading only: it creates no file and has no writer (Write is nil). Used by `myownpki db status`.

func (*DB) Close

func (d *DB) Close() error

Close closes both handles.

func (*DB) JournalMode

func (d *DB) JournalMode(ctx context.Context) (string, error)

JournalMode returns the journal mode of the file ("wal" when open via Open).

type FileStatus

type FileStatus struct {
	Kind        Kind
	Path        string
	Exists      bool
	JournalMode string
	Report      migrate.Report // valid when Err is nil and the file exists
	Available   int            // migrations this binary has for the kind
	Err         error          // wrong kind, modified or newer migrations, unreadable file
}

FileStatus is the state of one database file, for `myownpki db status`.

func Status

func Status(ctx context.Context, l Layout, tenant string) ([]FileStatus, error)

Status reports every database of the layout for tenant without creating or changing anything.

type Kind

type Kind string

Kind is a database type. Each kind has its own migration set (schema/<kind>).

const (
	KindSystem Kind = "system" // <data>/system.db
	KindPKI    Kind = "pki"    // <data>/tenants/<tenant>/pki.db
	KindAudit  Kind = "audit"  // <data>/tenants/<tenant>/audit.db
)

Database kinds of phase 1 (PLAN §4; acme and metrics follow later).

type Layout

type Layout struct {
	DataDir string // absolute, validated by internal/config
}

Layout maps database kinds to files below the data directory.

func (Layout) AuditLogs

func (l Layout) AuditLogs(tenant string) ([]AuditLog, error)

AuditLogs returns the instance log and the log of tenant.

func (Layout) InstanceAuditDB

func (l Layout) InstanceAuditDB() string

InstanceAuditDB returns <data>/audit.db: the audit log of instance events (logins, users, unseal). Tenant events go to the tenant's own audit.db.

func (Layout) SystemDB

func (l Layout) SystemDB() string

SystemDB returns <data>/system.db.

func (Layout) TenantDB

func (l Layout) TenantDB(tenant string, kind Kind) (string, error)

TenantDB returns the file of a tenant database kind.

func (Layout) TenantDir

func (l Layout) TenantDir(tenant string) (string, error)

TenantDir returns <data>/tenants/<tenant>.

Directories

Path Synopsis
Package migrate applies SQL schema migrations to one SQLite database (PLAN §4, decided instead of goose).
Package migrate applies SQL schema migrations to one SQLite database (PLAN §4, decided instead of goose).

Jump to

Keyboard shortcuts

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