Documentation
¶
Overview ¶
Package store owns the SQLite database file: opening it with the required pragmas, the read pool, the single writer goroutine, schema migrations and ID generation. It is infrastructure only; domain packages own their SQL.
Index ¶
- Constants
- Variables
- type DB
- func (db *DB) Close() error
- func (db *DB) Do(ctx context.Context, fn func(tx *Tx) error) error
- func (db *DB) Maintenance(ctx context.Context, fn func(ctx context.Context, conn *sql.Conn) error) error
- func (db *DB) Path() string
- func (db *DB) Read() *sql.DB
- func (db *DB) SetValue(key, v any)
- func (db *DB) Value(key any) any
- type Options
- type SchemaTooNewError
- type Tx
- func (t *Tx) AfterCommit(f func())
- func (t *Tx) Context() context.Context
- func (t *Tx) Exec(query string, args ...any) (sql.Result, error)
- func (t *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (t *Tx) NewID() string
- func (t *Tx) Now() int64
- func (t *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (t *Tx) Query(query string, args ...any) (*sql.Rows, error)
- func (t *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (t *Tx) QueryRow(query string, args ...any) *sql.Row
- func (t *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (t *Tx) Value(key any) any
Constants ¶
const FileName = "bubbles.db"
FileName is the database file inside the data directory.
const SnapshotPrefix = "pre-migrate-"
SnapshotPrefix starts the file name of every pre-migration snapshot in the backups directory (pre-migrate-<from>-<to>.db).
Variables ¶
var ErrClosed = errors.New("database is closed")
ErrClosed is returned by Do after Close has been called.
var ErrFnExited = errors.New("transaction function exited without returning")
ErrFnExited is returned by Do when the transaction function (or an AfterCommit callback) ended its goroutine with runtime.Goexit, for example through testing.T.FailNow, without returning or panicking. The transaction is rolled back and the writer keeps serving later calls.
var ErrSchemaTooNew = errors.New("database schema is newer than this binary")
ErrSchemaTooNew matches (errors.Is) the error Open returns when the database was migrated by a newer binary than this one.
Functions ¶
This section is empty.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is an open database: one writer connection owned by a single goroutine (see Do) and a pool of read-only connections (see Read).
func Open ¶
Open opens (creating if needed) the database at path, applies pending migrations, seeds the instance row and starts the writer goroutine.
It refuses with an error matching ErrSchemaTooNew when the file was written by a newer binary, and snapshots a non-empty database into <dir>/backups/pre-migrate-<from>-<to>.db before migrating it.
func (*DB) Close ¶
Close stops accepting writes, waits for the in-flight write transaction to finish, then closes the writer connection and the read pool. It is safe to call more than once; later calls return the first result.
func (*DB) Do ¶
Do runs fn inside one write transaction on the single writer goroutine. Calls are serialized in arrival order. The transaction commits when fn returns nil and rolls back otherwise; fn's error is returned unchanged.
If ctx is done before the transaction starts, Do returns ctx.Err() without running fn. Cancelling ctx while fn runs aborts the transaction. fn must not call Do (it would deadlock). After Close, Do returns ErrClosed. A panic in fn rolls back and is re-raised in the caller; an fn that ends its goroutine with runtime.Goexit rolls back and makes Do return ErrFnExited.
func (*DB) Maintenance ¶
func (db *DB) Maintenance(ctx context.Context, fn func(ctx context.Context, conn *sql.Conn) error) error
Maintenance runs fn on the writer goroutine with the bare write connection, outside any transaction, so it can run statements SQLite refuses inside one (VACUUM, PRAGMA wal_checkpoint). No write transaction runs while fn does; readers keep their own connections. fn must not call Do or Maintenance (it would deadlock) and must leave the connection outside a transaction.
The cancellation, panic and Goexit rules of Do apply. After Close it returns ErrClosed.
func (*DB) Read ¶
Read returns the read-only connection pool (8 connections, query_only). Writes through it fail; use Do.
type Options ¶
type Options struct {
// Now is the clock behind Tx.Now, ULID timestamps and migration times.
// Nil means time.Now.
Now func() time.Time
// Migrations replaces the embedded migrations (tests). Files named
// NNNN_name.sql at the root of the FS, numbered 1..N without gaps.
Migrations fs.FS
// Logger receives migration and snapshot notices. Nil discards them.
Logger *slog.Logger
}
Options configures Open. The zero value is ready to use.
type SchemaTooNewError ¶
SchemaTooNewError reports the database and binary schema versions.
func (*SchemaTooNewError) Error ¶
func (e *SchemaTooNewError) Error() string
func (*SchemaTooNewError) Is ¶
func (e *SchemaTooNewError) Is(target error) bool
Is makes errors.Is(err, ErrSchemaTooNew) true.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a write transaction handed to the function passed to Do. It exposes the query methods of *sql.Tx; commit and rollback belong to the writer. A Tx must not be used after its function returns.
func (*Tx) AfterCommit ¶
func (t *Tx) AfterCommit(f func())
AfterCommit registers f to run on the writer goroutine after a successful commit, in registration order (so callbacks observe global commit order). It never runs after a rollback. f must be quick and must not call Do.
func (*Tx) ExecContext ¶
ExecContext executes a statement.
func (*Tx) NewID ¶
NewID returns a new ULID. IDs are strictly increasing across all transactions (commit order), even if the clock steps backwards.
func (*Tx) Now ¶
Now returns the transaction's timestamp in unix milliseconds. It is read once from the clock when the transaction starts, so every row written by one transaction shares it.
func (*Tx) PrepareContext ¶
PrepareContext prepares a statement scoped to the transaction (batch inserts and deletes).
func (*Tx) QueryContext ¶
QueryContext runs a query.
func (*Tx) QueryRowContext ¶
QueryRowContext runs a single-row query.