sqlitestore

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 22 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlitestore implements store.Store on SQLite (modernc.org/sqlite, no cgo). It is the adapter of Homie, an application with one household and one file; Family Hub uses package mongostore. Both pass the one conformance suite, package storetest.

The database file is opened in WAL mode with full synchronous commits (a commit that returned survives a power cut), foreign keys on and a busy timeout. The file, its -wal and -shm are 0600 and their directory 0700; Open tightens looser modes and logs a warning when it had to. It tightens a directory only when it is the database's own (Open created it, or it is the process user's and holds nothing but the database's files) and refuses to open in any other directory that is not 0700. Every gatekeeper table is named gk_*, so an application's own tables can live in the same file without a clash; times are INTEGER Unix milliseconds in UTC (the precision every adapter keeps), secrets and tokens only as sha256 BLOBs.

Transactions

Transactions travel in the context: Tx binds one transaction to the context passed to its function, every store method called with that context joins it, and a Tx inside a Tx joins the outer one. An application puts its own tables in the same transaction through Conn:

err := st.Tx(ctx, func(ctx context.Context) error {
	if err := st.People().Create(ctx, p); err != nil {
		return err
	}
	_, err := st.Conn(ctx).ExecContext(ctx, `INSERT INTO plugin_settings (person_id) VALUES (?)`, p.ID)
	return err
})

Concurrency

SQLite has one writer. The store serializes write transactions in the process: a Tx (and every store method that writes outside a Tx) first takes a one-slot lock, then opens the transaction with BEGIN IMMEDIATE. So callers never see SQLITE_BUSY from each other and never deadlock on a read-then-write upgrade; waiting for the lock honours the context. The busy timeout only covers a second process (or a second Store) writing the same file. Readers never wait: outside a Tx they use another pooled connection, and WAL gives them the last committed state.

The lock is held for the whole Tx function, so the function must not wait for another Tx started with a context that does not carry this one (it would wait for itself): inside the function pass the context it received.

An application's own writes outside a Tx (Conn on a context without a transaction) skip the in-process lock and rely on the busy timeout; write inside Tx instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Conn is what an application needs to run its own SQL: a *sql.DB or a *sql.Tx.

type Migration

type Migration struct {
	// Version is a positive number, unique and increasing inside its list.
	Version int
	// Name describes the step ("add plugin tables"); it is recorded, not interpreted.
	Name string
	// SQL is one or more statements.
	SQL string
}

Migration is one step of a schema. Steps are applied in Version order, each in its own transaction, and a step that was applied is never run again. A step is final once released: change a schema by adding a step, never by editing one.

type Options

type Options struct {
	// Path is the database file; it is created (mode 0600, its directory 0700) when missing, and an existing file,
	// -wal or -shm with other modes is set to those (with a warning). Use a directory of its own: an existing directory
	// that is not 0700 is tightened only when it belongs to the process user and holds nothing but the database's
	// files, and Open fails otherwise (it never chmods a shared directory). Required.
	Path string
	// Cascades run inside the transaction of an account or person delete, after the store's own deletes.
	Cascades []store.Cascade
	// Migrations are the application's own schema steps, applied after the gatekeeper's and tracked apart from them
	// (see Migration).
	Migrations []Migration
	// Clock is the time source for audit ids and migration stamps; default time.Now.
	Clock core.Clock
	// Logger receives the migration log; nil discards it.
	Logger *slog.Logger
	// BusyTimeout is how long SQLite waits for a lock another process holds; default 10s.
	BusyTimeout time.Duration
	// MaxConns bounds the pooled connections (one writer, the rest readers); default 8.
	MaxConns int
}

Options configures a Store.

type Store

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

Store is store.Store on SQLite.

func Open

func Open(ctx context.Context, o Options) (*Store, error)

Open opens (creating it when missing) the database, applies the migrations and returns the store. It refuses a database whose schema is newer than this build knows.

func (*Store) Accounts

func (s *Store) Accounts() store.AccountStore

Accounts implements store.Store.

func (*Store) Audit

func (s *Store) Audit() store.AuditStore

Audit implements store.Store.

func (*Store) Close

func (s *Store) Close() error

Close closes the database. Open transactions are rolled back.

func (*Store) Conn

func (s *Store) Conn(ctx context.Context) Conn

Conn returns the transaction ctx carries (from Tx) or, without one, the database. An application runs its own SQL through it so its tables join the gatekeeper's transactions. Without a transaction use it for reads and write through Tx (see the package comment).

func (*Store) Invites

func (s *Store) Invites() store.InviteStore

Invites implements store.Store.

func (*Store) OAuth

func (s *Store) OAuth() store.OAuthStore

OAuth implements store.Store.

func (*Store) Passkeys

func (s *Store) Passkeys() store.PasskeyStore

Passkeys implements store.Store.

func (*Store) People

func (s *Store) People() store.PersonStore

People implements store.Store.

func (*Store) Purge added in v0.2.0

func (s *Store) Purge(ctx context.Context, now time.Time) (store.PurgeStats, error)

Purge implements store.Purger. In one transaction it deletes:

  • tokens and codes whose end has been reached (ExpiresAt <= now);
  • invites 7 days after their expiry;
  • audit entries older than 400 days;
  • DCR clients older than an hour that no grant points at (DeleteIdleClients).

Sessions are not here: the sign-in package sweeps them with SessionStore.DeleteExpired.

func (*Store) Sessions

func (s *Store) Sessions() store.SessionStore

Sessions implements store.Store.

func (*Store) Tx

func (s *Store) Tx(ctx context.Context, fn func(ctx context.Context) error) (err error)

Tx implements store.Store: it runs fn in one write transaction (BEGIN IMMEDIATE, serialized with the store's other write transactions) and commits when fn returns nil; any error, or a panic, rolls it back. Inside fn, pass the context fn received to every store method and to Conn. A Tx inside a Tx joins the outer one.

Jump to

Keyboard shortcuts

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