db

package
v0.10.6 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 17 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 for SQLite.

func MigrationsPG added in v0.9.0

func MigrationsPG() fs.FS

MigrationsPG returns the embedded goose migration files for PostgreSQL.

func Rebind added in v0.9.0

func Rebind(query string) string

Rebind converts '?' parameter placeholders in query into PostgreSQL '$1', '$2', ... placeholders. It preserves '?' characters inside single-quoted string literals ('...'), block comments (/* ... */), and line comments (-- ...).

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 OpenPostgres added in v0.9.0

func OpenPostgres(ctx context.Context, dsn string, opts PostgresOptions) (*DB, error)

OpenPostgres opens a connection pool to a PostgreSQL database at dsn and verifies it.

func (*DB) Dialect added in v0.9.0

func (d *DB) Dialect() Dialect

Dialect returns the database engine dialect (sqlite or postgres).

func (*DB) ExecContext added in v0.9.0

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

ExecContext executes a query using the underlying db, rebinding parameters for PostgreSQL.

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) PrepareContext added in v0.9.0

func (d *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext creates a prepared statement using the underlying db, rebinding parameters for PostgreSQL.

func (*DB) QueryContext added in v0.9.0

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

QueryContext executes a query using the underlying db, rebinding parameters for PostgreSQL.

func (*DB) QueryRowContext added in v0.9.0

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

QueryRowContext executes a query using the underlying db, rebinding parameters for PostgreSQL.

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

func (*DB) Wrap added in v0.9.0

func (d *DB) Wrap(q Querier) Querier

Wrap returns a Querier that automatically adapts queries (e.g. parameter rebinding) according to the database dialect.

type Dialect added in v0.9.0

type Dialect string

Dialect identifies the underlying SQL database engine.

const (
	DialectSQLite   Dialect = "sqlite"
	DialectPostgres Dialect = "postgres"
)

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 PostgresOptions added in v0.9.0

type PostgresOptions struct {
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

PostgresOptions configures connection pooling for PostgreSQL.

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