Documentation
¶
Overview ¶
Package dbopen provides a single function to open an SQLite database with the HOROS production-safe pragmas applied via EXEC (driver-agnostic).
Default pragmas:
foreign_keys = ON journal_mode = WAL busy_timeout = 10000 synchronous = NORMAL
Usage:
import _ "modernc.org/sqlite"
db, err := dbopen.Open("app.db")
With tracing driver:
import _ "github.com/hazyhaar/pkg/trace"
db, err := dbopen.Open("app.db", dbopen.WithTrace())
In tests:
db := dbopen.OpenMemory(t)
Index ¶
- func Exec(ctx context.Context, db *sql.DB, query string, args ...any) (sql.Result, error)
- func IsBusy(err error) bool
- func Open(path string, opts ...Option) (*sql.DB, error)
- func OpenMemory(t testing.TB, opts ...Option) *sql.DB
- func RunTx(ctx context.Context, db *sql.DB, fn func(*sql.Tx) error) error
- type Option
- func WithBusyTimeout(ms int) Option
- func WithCacheSize(pages int) Option
- func WithDriver(name string) Option
- func WithMkdirAll() Option
- func WithSchema(s string) Option
- func WithSchemaFile(path string) Option
- func WithSynchronous(mode string) Option
- func WithTrace() Option
- func WithoutForeignKeys() Option
- func WithoutPing() Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Exec ¶
Exec executes a statement with automatic retry on SQLITE_BUSY. It retries up to 3 times with 100/200/300 ms backoff.
func IsBusy ¶
IsBusy reports whether err indicates an SQLite BUSY condition. It checks for SQLITE_BUSY, "database is locked", and "database table is locked".
func Open ¶
Open opens an SQLite database at path with HOROS production-safe pragmas. The caller must blank-import the appropriate driver before calling Open:
import _ "modernc.org/sqlite" // default "sqlite" driver import _ "github.com/hazyhaar/pkg/trace" // "sqlite-trace" driver
func OpenMemory ¶
OpenMemory opens an in-memory SQLite database for testing. It sets MaxOpenConns(1) to ensure all queries hit the same in-memory database (each connection to ":memory:" creates a separate database). It registers t.Cleanup to close the database automatically.
Types ¶
type Option ¶
type Option func(*config)
Option customises Open behaviour.
func WithBusyTimeout ¶
WithBusyTimeout sets PRAGMA busy_timeout in milliseconds. Default: 10000.
func WithCacheSize ¶
WithCacheSize sets PRAGMA cache_size. 0 (default) keeps the SQLite default. Negative values are KiB (e.g. -64000 = 64 MB).
func WithDriver ¶
WithDriver sets the database/sql driver name. Default: "sqlite".
func WithMkdirAll ¶
func WithMkdirAll() Option
WithMkdirAll creates parent directories of the database path before opening.
func WithSchema ¶
WithSchema queues inline SQL to execute after pragmas are applied.
func WithSchemaFile ¶
WithSchemaFile queues an .sql file to read and execute after pragmas.
func WithSynchronous ¶
WithSynchronous sets PRAGMA synchronous. Default: "NORMAL".
func WithoutForeignKeys ¶
func WithoutForeignKeys() Option
WithoutForeignKeys disables PRAGMA foreign_keys (rarely needed).
func WithoutPing ¶
func WithoutPing() Option
WithoutPing skips the db.Ping() verification after opening.