sqlite

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package sqlite provides a SQLite implementation of store.Store using grove ORM.

Index

Constants

View Source
const BusyTimeout = 5 * time.Second

BusyTimeout is how long a connection opened with DSN waits for another connection's write lock before it gives up with SQLITE_BUSY.

Variables

View Source
var Migrations = migrate.NewGroup("keysmith")

Migrations is the grove migration group for the Keysmith SQLite store. Each migration lives in a file of its own, named <14 digit version>_<name>.go, and registers itself with Migrations.Add.

Store.Migrate runs the group through grove's orchestrator, which takes the migration lock first. A host that runs its migrations centrally can register this group with its own orchestrator instead and turn Store.Migrate off (the Forge extension's WithDisableMigrate).

The versions and names match the ones keysmith has always recorded in grove_migrations, so a database that already applied them skips them.

Functions

func DSN

func DSN(path string) string

DSN returns the data source name to open the database at path with, for sqlitedriver.SqliteDB.Open. Open the database you hand to New with it:

sdb := sqlitedriver.New()
err := sdb.Open(ctx, sqlite.DSN("keysmith.db"))

database/sql pools connections and every pooled connection has its own settings, so the DSN is the one place that reaches all of them. Grove's driver runs PRAGMA foreign_keys=ON once at Open, on whichever connection it gets, and sets no busy timeout. DSN adds three settings, applied by modernc.org/sqlite to each connection it opens:

  • busy_timeout: a write waits up to BusyTimeout for the write lock. The engine's ValidateKey updates last_used_at in the background, so the next write regularly finds the lock taken.
  • foreign_keys: ON DELETE CASCADE holds on every connection, so deleting a key or a scope never leaves keysmith_key_scopes rows behind.
  • _txlock=immediate: transactions take the write lock at BEGIN. SQLite skips the busy timeout when a transaction that has already read tries to write, which AssignToKey does, and fails at once with SQLITE_BUSY. Read-only transactions still begin deferred.

path is a file name or a file: URI. Query parameters already on it are kept, and so is any of the three settings it already sets: the caller's _txlock, busy_timeout or foreign_keys wins, and DSN adds only the ones that are missing. If you open the database some other way, such as from a grove extension's config, put the same three parameters on that DSN.

Types

type Store

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

Store implements store.Store using SQLite via Grove ORM.

func New

func New(db *grove.DB) *Store

New creates a new SQLite store backed by Grove ORM.

func (*Store) BusyTimeout

func (s *Store) BusyTimeout(ctx context.Context) (time.Duration, error)

BusyTimeout reads PRAGMA busy_timeout on one pooled connection. Grove's driver never sets a busy timeout, so zero means the DSN didn't set one either, and then no connection in the pool has it. The extension reads it at startup to warn about a database opened without DSN.

func (*Store) Close

func (s *Store) Close() error

Close closes the database connection.

func (*Store) DB

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

DB returns the underlying grove database for direct access.

func (*Store) Keys

func (s *Store) Keys() key.Store

Keys returns the key store.

func (*Store) Migrate

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

Migrate applies the pending migrations in Migrations through grove's orchestrator, which holds grove's migration lock (a row in grove_migration_locks) while it runs.

func (*Store) Ping

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

Ping checks database connectivity.

func (*Store) Policies

func (s *Store) Policies() policy.Store

Policies returns the policy store.

func (*Store) Rotations

func (s *Store) Rotations() rotation.Store

Rotations returns the rotation store.

func (*Store) Scopes

func (s *Store) Scopes() scope.Store

Scopes returns the scope store.

func (*Store) Usages

func (s *Store) Usages() usage.Store

Usages returns the usage store.

Jump to

Keyboard shortcuts

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