db

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package db is the GOWDK database plumbing addon. It enables the db feature and ships a thin, driver-agnostic helper for opening a *sql.DB on top of the standard library's database/sql. It is designed to pair with sqlc and user-authored SQL files: you own the schema, the queries, the generated models, migrations, and all domain logic. GOWDK owns none of that and adds no driver, ORM, repository, or query opinions.

The helper imports no SQL driver. Your application registers a driver with a blank import (for example _ "github.com/jackc/pgx/v5/stdlib") and passes its name to Open, so GOWDK carries no database dependency of its own.

Index

Constants

View Source
const DefaultMigrationTable = "gowdk_schema_migrations"

DefaultMigrationTable is the table used to track applied migrations when MigrationOptions.Table is empty.

View Source
const DefaultPingTimeout = 5 * time.Second

DefaultPingTimeout bounds the connectivity check performed by Open.

View Source
const ImportPath = "github.com/cssbruno/gowdk/addons/db"

ImportPath is the canonical Go import path for the db addon.

Variables

This section is empty.

Functions

func Addon

func Addon() gowdk.Addon

Addon enables the database plumbing feature.

func DollarPlaceholder added in v0.5.0

func DollarPlaceholder(position int) string

DollarPlaceholder returns PostgreSQL-style numbered placeholders.

func Open

func Open(driver, dsn string) (*sql.DB, error)

Open opens a *sql.DB for the given registered driver and DSN, applies pool defaults, and verifies the connection with a bounded ping. The driver must be registered by the caller (typically via a blank import) before Open is called.

func OpenWithOptions

func OpenWithOptions(driver, dsn string, options Options) (*sql.DB, error)

OpenWithOptions is Open with explicit pool configuration.

func Ping added in v0.5.0

func Ping(ctx context.Context, database *sql.DB) error

Ping verifies database connectivity through database/sql.

func QuestionPlaceholder added in v0.5.0

func QuestionPlaceholder(position int) string

QuestionPlaceholder returns ? placeholders used by SQLite and MySQL-style drivers.

func WithTx added in v0.5.0

func WithTx(ctx context.Context, database *sql.DB, options *sql.TxOptions, fn func(context.Context, *sql.Tx) error) (err error)

WithTx runs fn inside a database/sql transaction. The transaction commits when fn returns nil and rolls back when fn returns an error or panics.

Types

type MigrationOptions added in v0.5.0

type MigrationOptions struct {
	// Dir selects a directory inside Source. Empty means ".".
	Dir string
	// Table names the migration tracking table. Empty uses
	// DefaultMigrationTable. Only simple SQL identifiers are accepted.
	Table string
	// Placeholder adapts bind placeholders for the target driver. Empty uses
	// QuestionPlaceholder. Use DollarPlaceholder for PostgreSQL-style drivers.
	Placeholder PlaceholderFunc
	// Now overrides the applied_at timestamp source for tests.
	Now func() time.Time
	// TxOptions are passed to database/sql BeginTx.
	TxOptions *sql.TxOptions
}

MigrationOptions configures ApplyMigrations.

type MigrationRecord added in v0.5.0

type MigrationRecord struct {
	Name     string
	Checksum string
}

MigrationRecord describes one migration file and checksum.

type MigrationResult added in v0.5.0

type MigrationResult struct {
	Applied []MigrationRecord
	Skipped []MigrationRecord
}

MigrationResult records which migration files were applied or already present with the same checksum.

func ApplyMigrations added in v0.5.0

func ApplyMigrations(ctx context.Context, database *sql.DB, source fs.FS, options MigrationOptions) (MigrationResult, error)

ApplyMigrations applies .sql files from source in lexical path order. It is intentionally only a thin database/sql helper: files are user-owned SQL, and this helper tracks applied file names and checksums in one table.

type Options

type Options struct {
	// MaxOpenConns caps total open connections. Zero means unlimited.
	MaxOpenConns int
	// MaxIdleConns caps idle connections. Zero uses the database/sql default.
	MaxIdleConns int
	// ConnMaxLifetime caps connection reuse time. Zero means unlimited.
	ConnMaxLifetime time.Duration
	// PingTimeout bounds the initial connectivity check. Defaults to 5s.
	PingTimeout time.Duration
}

Options tunes the connection pool. The zero value applies sensible defaults.

type PlaceholderFunc added in v0.5.0

type PlaceholderFunc func(position int) string

PlaceholderFunc returns the SQL placeholder for a 1-based bind position.

type Readiness added in v0.5.0

type Readiness struct {
	Ready     bool          `json:"ready"`
	CheckedAt time.Time     `json:"checkedAt"`
	Duration  time.Duration `json:"duration"`
	Error     string        `json:"error,omitempty"`
}

Readiness reports the outcome of a database readiness check.

func CheckReadiness added in v0.5.0

func CheckReadiness(ctx context.Context, database *sql.DB) Readiness

CheckReadiness returns a small framework-neutral readiness snapshot for a generated app endpoint or startup check.

Jump to

Keyboard shortcuts

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