db

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 16 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 db opens otag's SQLite database and applies embedded migrations.

Index

Constants

View Source
const TimeFormat = "2006-01-02T15:04:05.000Z"

TimeFormat is the layout of every timestamp column.

Variables

View Source
var ErrNotMigrated = errors.New("database missing or not migrated to this binary's schema")

ErrNotMigrated is returned with Options.NoMigrate when the database is missing or has an older schema than this binary.

Functions

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation reports whether err is a UNIQUE/PRIMARY KEY constraint failure.

func Now

func Now() string

Now returns the current UTC time formatted like the DB defaults.

func OpenReadOnly added in v0.1.1

func OpenReadOnly(path string) (*sql.DB, error)

OpenReadOnly opens an existing database for a look that writes nothing at all (mode=ro, query_only, immutable): it is never created, migrated or chmod-ed, and not even a -wal or -shm file is made. immutable skips SQLite's locking and the WAL, so a concurrent writer's latest commits may be missing: callers use it for hints (the setup wizard before the user agreed to anything) and check again with Open.

Types

type DB

type DB struct {
	*sql.DB
	Path string
	// Version is the schema version found in the file; Latest is what this binary knows.
	Version, Latest int
	// contains filtered or unexported fields
}

DB is an open otag database.

func Open

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

Open opens (creating if needed) the database at path and migrates it to the latest schema. If the file's schema is newer than this binary, the DB is opened with mode=ro and every Tx call fails with SCHEMA_TOO_NEW.

func OpenWith

func OpenWith(ctx context.Context, path string, o Options) (*DB, error)

OpenWith is Open with options.

func (*DB) ReadOnly

func (d *DB) ReadOnly() bool

ReadOnly reports whether writes are refused because the schema is newer than this binary.

func (*DB) Tx

func (d *DB) Tx(ctx context.Context, fn func(tx *sql.Tx) error) error

Tx runs fn inside a write transaction (BEGIN IMMEDIATE) and commits if fn succeeds.

type Options

type Options struct {
	// BusyTimeout bounds how long a statement waits for a lock (default 10s). The hook
	// bridge uses a short one so a locked database never stalls a harness.
	BusyTimeout time.Duration
	// NoMigrate never creates or migrates the database: a missing file or an older
	// schema is ErrNotMigrated. For hot paths (the statusLine wrapper) that must never
	// take the migration's write lock.
	NoMigrate bool
}

Options tune a connection.

type Querier

type Querier 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
}

Querier is satisfied by *sql.DB, *sql.Conn and *sql.Tx.

Jump to

Keyboard shortcuts

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