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 foreign keys on and a busy timeout. 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 ¶
- type Conn
- type Migration
- type Options
- type Store
- func (s *Store) Accounts() store.AccountStore
- func (s *Store) Audit() store.AuditStore
- func (s *Store) Close() error
- func (s *Store) Conn(ctx context.Context) Conn
- func (s *Store) Invites() store.InviteStore
- func (s *Store) OAuth() store.OAuthStore
- func (s *Store) Passkeys() store.PasskeyStore
- func (s *Store) People() store.PersonStore
- func (s *Store) Sessions() store.SessionStore
- func (s *Store) Tx(ctx context.Context, fn func(ctx context.Context) error) (err error)
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. 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 ¶
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) 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) Passkeys ¶
func (s *Store) Passkeys() store.PasskeyStore
Passkeys implements store.Store.
func (*Store) Sessions ¶
func (s *Store) Sessions() store.SessionStore
Sessions implements store.Store.
func (*Store) Tx ¶
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.