db

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package db opens the SQLite database, applies embedded goose migrations and provides transaction helpers shared by the repositories in internal/store.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DSN

func DSN(path string, busy time.Duration) string

DSN builds the modernc.org/sqlite connection string for path with the pragmas every connection must carry.

func IsCheckViolation

func IsCheckViolation(err error) bool

IsCheckViolation reports whether err is a CHECK constraint failure.

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation reports whether err is a FOREIGN KEY constraint failure.

func IsUniqueViolation

func IsUniqueViolation(err error) bool

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

func Migrations

func Migrations() fs.FS

Migrations returns the embedded goose migration files.

Types

type DB

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

DB wraps *sql.DB for the panel database.

func Open

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

Open opens (creating if needed) the SQLite database at path and verifies the connection. It does not run migrations; call Migrate.

func (*DB) LatestSchemaVersion

func (d *DB) LatestSchemaVersion() (int64, error)

LatestSchemaVersion returns the newest embedded migration version.

func (*DB) Migrate

func (d *DB) Migrate(ctx context.Context) ([]MigrationResult, error)

Migrate applies all pending migrations and returns those applied.

func (*DB) Path

func (d *DB) Path() string

Path returns the database file path.

func (*DB) SchemaVersion

func (d *DB) SchemaVersion(ctx context.Context) (int64, error)

SchemaVersion returns the current goose schema version (0 when unmigrated).

func (*DB) WithTx

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

WithTx runs fn inside a transaction (BEGIN IMMEDIATE). It commits when fn returns nil and rolls back otherwise (also on panic, which is re-raised).

type MigrationResult

type MigrationResult struct {
	Version int64
	Source  string
}

MigrationResult describes one applied migration.

type Options

type Options struct {
	// MaxOpenConns caps the pool (default 8). WAL allows concurrent readers;
	// writers are serialized by SQLite (BEGIN IMMEDIATE + busy_timeout).
	MaxOpenConns int
	// BusyTimeout is how long a writer waits for the lock (default 5s).
	BusyTimeout time.Duration
}

Options tune Open. The zero value is suitable for production.

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 the subset of *sql.DB / *sql.Tx used by repositories, so the same repository code runs inside or outside a transaction.

Jump to

Keyboard shortcuts

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