store

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: Apache-2.0 Imports: 21 Imported by: 0 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 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

View Source
const FileName = "bubbles.db"

FileName is the database file inside the data directory.

View Source
const SnapshotPrefix = "pre-migrate-"

SnapshotPrefix starts the file name of every pre-migration snapshot in the backups directory (pre-migrate-<from>-<to>.db).

Variables

View Source
var ErrClosed = errors.New("database is closed")

ErrClosed is returned by Do after Close has been called.

View Source
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.

View Source
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

func Open(ctx context.Context, path string, opts Options) (*DB, error)

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

func (db *DB) Close() error

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

func (db *DB) Do(ctx context.Context, fn func(tx *Tx) error) error

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) Path

func (db *DB) Path() string

Path returns the database file path.

func (*DB) Read

func (db *DB) Read() *sql.DB

Read returns the read-only connection pool (8 connections, query_only). Writes through it fail; use Do.

func (*DB) SetValue

func (db *DB) SetValue(key, v any)

SetValue attaches v to the database under key, like a context value: code that only holds a *Tx (event appenders, for example) finds it with Tx.Value. Keys should be unexported types owned by the package that sets them. A nil v is stored as is and reads back as nil.

func (*DB) Value

func (db *DB) Value(key any) any

Value returns the value set for key with SetValue, or nil.

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

type SchemaTooNewError struct {
	DBVersion  int
	MaxVersion int
}

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) Context

func (t *Tx) Context() context.Context

Context returns the context passed to Do.

func (*Tx) Exec

func (t *Tx) Exec(query string, args ...any) (sql.Result, error)

Exec executes a statement using the Do context.

func (*Tx) ExecContext

func (t *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext executes a statement.

func (*Tx) NewID

func (t *Tx) NewID() string

NewID returns a new ULID. IDs are strictly increasing across all transactions (commit order), even if the clock steps backwards.

func (*Tx) Now

func (t *Tx) Now() int64

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

func (t *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext prepares a statement scoped to the transaction (batch inserts and deletes).

func (*Tx) Query

func (t *Tx) Query(query string, args ...any) (*sql.Rows, error)

Query runs a query using the Do context.

func (*Tx) QueryContext

func (t *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext runs a query.

func (*Tx) QueryRow

func (t *Tx) QueryRow(query string, args ...any) *sql.Row

QueryRow runs a single-row query using the Do context.

func (*Tx) QueryRowContext

func (t *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext runs a single-row query.

func (*Tx) Value

func (t *Tx) Value(key any) any

Value returns the database value set for key with DB.SetValue, or nil.

Jump to

Keyboard shortcuts

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