db

package
v1.13.15 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMutationCardinality = errors.New("SQL mutation cardinality mismatch")

ErrMutationCardinality identifies a required SQL mutation that did not affect the expected number of direct target rows.

Functions

func ApplySQLiteSessionPragmas added in v0.10.0

func ApplySQLiteSessionPragmas(db *sql.DB) error

func BuildPostgresConnStringFromEnv added in v1.10.2

func BuildPostgresConnStringFromEnv(databaseName string) (string, error)

func ConnectDB

func ConnectDB() (*sql.DB, error)

func CurrentSchemaVersion added in v1.6.0

func CurrentSchemaVersion(dbconn *sql.DB) (int64, error)

CurrentSchemaVersion returns the highest applied schema version in schema_version.

func CurrentSchemaVersionContext added in v1.13.14

func CurrentSchemaVersionContext(ctx context.Context, dbconn *sql.DB) (int64, error)

CurrentSchemaVersionContext returns the highest applied schema version using the caller-owned context.

func DefaultOperationTimeout added in v0.10.0

func DefaultOperationTimeout() time.Duration

func DefaultStatementTimeout added in v0.10.0

func DefaultStatementTimeout() time.Duration

func EnsurePostgresSchema added in v0.10.0

func EnsurePostgresSchema(dbconn *sql.DB) error

EnsurePostgresSchema validates the runtime PostgreSQL schema. If COLDKEEP_DB_AUTO_BOOTSTRAP is enabled and schema_version is missing, it bootstraps by applying the embedded db/schema_postgres.sql.

func EnsureSchema added in v1.8.0

func EnsureSchema(dbconn *sql.DB) error

EnsureSchema applies/validates schema using the active backend. SQLite: applies embedded schema migrations. Postgres: validates or bootstraps schema and enforces minimum version.

func NewOperationContext added in v0.10.0

func NewOperationContext(parent context.Context) (context.Context, context.CancelFunc)

func QueryCurrentSchemaVersion added in v1.6.0

func QueryCurrentSchemaVersion() (int64, error)

QueryCurrentSchemaVersion connects using runtime DB settings and returns MAX(schema_version.version).

func QueryWithOptionalForUpdate added in v0.10.0

func QueryWithOptionalForUpdate(dbconn *sql.DB, query string) string

QueryWithOptionalForUpdate appends FOR UPDATE only for backends that support it.

func QueryWithOptionalForUpdateNowait added in v0.10.0

func QueryWithOptionalForUpdateNowait(dbconn *sql.DB, query string) string

QueryWithOptionalForUpdateNowait appends FOR UPDATE NOWAIT when supported.

func QueryWithOptionalForUpdateSkipLocked added in v0.10.0

func QueryWithOptionalForUpdateSkipLocked(dbconn *sql.DB, query string) string

QueryWithOptionalForUpdateSkipLocked appends FOR UPDATE SKIP LOCKED when supported.

func RequireExactlyOneRow added in v1.13.11

func RequireExactlyOneRow(result sql.Result, operation string) error

RequireExactlyOneRow verifies that a mutation affected one direct target row.

func RequireRowsAffected added in v1.13.11

func RequireRowsAffected(result sql.Result, operation string, expected int64) error

RequireRowsAffected verifies the direct row count reported by a mutation. Operation must be a bounded logical label; callers must not include SQL text, connection details, paths, hashes, or bound values.

func RunMigrations added in v0.8.0

func RunMigrations(dbconn *sql.DB) error

RunMigrations applies the embedded SQLite schema to a DB connection. It is intended for simulated/local SQLite contexts only.

func SupportsSelectForUpdate added in v0.10.0

func SupportsSelectForUpdate(dbconn *sql.DB) bool

SupportsSelectForUpdate reports whether SELECT ... FOR UPDATE is supported.

func SupportsSelectForUpdateNowait added in v0.10.0

func SupportsSelectForUpdateNowait(dbconn *sql.DB) bool

SupportsSelectForUpdateNowait reports whether FOR UPDATE NOWAIT is supported.

func SupportsSelectForUpdateSkipLocked added in v0.10.0

func SupportsSelectForUpdateSkipLocked(dbconn *sql.DB) bool

SupportsSelectForUpdateSkipLocked reports whether FOR UPDATE SKIP LOCKED is supported.

Types

type Backend added in v0.10.0

type Backend string

Backend identifies the SQL backend in use for a given *sql.DB.

const (
	BackendUnknown  Backend = "unknown"
	BackendPostgres Backend = "postgres"
	BackendSQLite   Backend = "sqlite"
)

func BackendFromDB added in v0.10.0

func BackendFromDB(dbconn *sql.DB) Backend

BackendFromDB detects the configured backend from the DB driver.

type DBTX

type DBTX interface {
	Exec(query string, args ...any) (sql.Result, error)
	QueryRow(query string, args ...any) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

DBTX is implemented by *sql.DB and *sql.Tx (so we can reuse helpers inside a tx).

Jump to

Keyboard shortcuts

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