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
- Variables
- func Migrations(kind Kind) ([]migrate.Migration, error)
- func Snapshot(ctx context.Context, src, dst string) error
- func TenantScope(tenant string) string
- func Tenants(ctx context.Context, l Layout) ([]systemdb.ListTenantsRow, error)
- type Applied
- type AuditLog
- type DB
- type FileStatus
- type Kind
- type Layout
Constants ¶
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.
const InstanceScope = "instance"
InstanceScope is the audit scope of <data>/audit.db.
Variables ¶
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.
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.
var Kinds = []Kind{KindSystem, KindPKI, KindAudit}
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.
var TenantKinds = []Kind{KindPKI, KindAudit}
TenantKinds are the databases every tenant directory holds.
Functions ¶
func Migrations ¶
Migrations returns the migration set of a database kind.
func Snapshot ¶
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 ¶
TenantScope is the audit scope of tenants/<tenant>/audit.db.
Types ¶
type Applied ¶
Applied lists the migrations Migrate applied to one file.
type AuditLog ¶
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 ¶
Open opens (and creates, if missing) the database at path. The caller decides whether a missing file is acceptable – see OpenExisting.
func OpenAuditLog ¶
OpenAuditLog opens an existing audit log read-only and checks that it is an audit database of the expected scope.
func OpenReadOnly ¶
OpenReadOnly opens an existing database for reading only: it creates no file and has no writer (Write is nil). Used by `myownpki db status`.
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`.
type Kind ¶
type Kind string
Kind is a database type. Each kind has its own migration set (schema/<kind>).
type Layout ¶
type Layout struct {
DataDir string // absolute, validated by internal/config
}
Layout maps database kinds to files below the data directory.
func (Layout) InstanceAuditDB ¶
InstanceAuditDB returns <data>/audit.db: the audit log of instance events (logins, users, unseal). Tenant events go to the tenant's own audit.db.