Help us shape the future of Go! Take the 2026 Go Developer Survey

sqlite

package
v0.69.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sqlite opens the SQLite databases hive and Hive Desktop keep, with the DSN and pool settings every caller must agree on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AmbientTx added in v0.63.0

func AmbientTx(ctx context.Context, pool *sql.DB) (*sql.Tx, bool)

AmbientTx returns the transaction that WithinTx opened on pool and attached to ctx, if there is one.

func Open

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

Open opens the database at path in WAL mode with foreign keys on, applies the pool settings, and pings it. The caller owns migrations and defaults.

Write transactions begin with BEGIN IMMEDIATE (_txlock=immediate). Several writers, in one process or across the CLI and the desktop, run transactions that read before they write. With the driver default (deferred), two such transactions can both hold a read lock and then both try to upgrade to the write lock. SQLite resolves that deadlock by returning SQLITE_BUSY at once and ignoring busy_timeout, because waiting could never succeed. IMMEDIATE takes the write lock up front, so a second writer waits on busy_timeout instead of failing.

func WithinTx added in v0.63.0

func WithinTx(ctx context.Context, pool *sql.DB, fn func(context.Context, *sql.Tx) error) error

WithinTx runs fn inside a transaction on pool. When ctx already carries one for pool, fn joins it; otherwise WithinTx begins one and hands fn a context that carries it. Only the call that began the transaction commits it or, when fn fails, rolls it back.

Joining is required, not an optimization. Open sets _txlock=immediate, so a nested BEGIN IMMEDIATE waits out busy_timeout for the write lock its own caller holds and then fails with SQLITE_BUSY.

Types

type Options

type Options struct {
	MaxOpenConns int
	MaxIdleConns int
	BusyTimeout  time.Duration
}

type TestTemplate added in v0.69.0

type TestTemplate struct {
	// Filename is the database file's name in a directory.
	Filename string
	// Migrate creates the migrated database in dir and closes it, so that the
	// main file holds everything and no WAL remains.
	Migrate func(ctx context.Context, dir string) error
	// contains filtered or unexported fields
}

TestTemplate gives each test its own copy of a database that was migrated once per process. Tests spend most of their database setup in migrations, and parallel migrations contend inside modernc.org/sqlite.

func (*TestTemplate) WriteTo added in v0.69.0

func (tt *TestTemplate) WriteTo(ctx context.Context, dir string) error

WriteTo writes a copy of the template to Filename in dir.

type TestingT added in v0.69.0

type TestingT interface {
	Helper()
	Context() context.Context
	TempDir() string
	Fatalf(format string, args ...any)
	Cleanup(func())
}

TestingT is the part of testing.TB that a store's OpenForTest needs. The store packages ship in production binaries, so they do not import testing.

Jump to

Keyboard shortcuts

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