Help us shape the future of Go! Take the 2026 Go Developer Survey

db

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package db owns the Postgres connection lifecycle. S01 ships the open/healthcheck/transaction helpers; later sprints add domain-specific wrappers but always go through this package for the pool.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoURL = errors.New("db: no DATABASE_URL configured (set SHITHUB_DATABASE_URL)")

ErrNoURL is returned by Open when neither cfg.URL nor SHITHUB_DATABASE_URL is set.

Functions

func FromContext

func FromContext(ctx context.Context) int64

FromContext reports how many tracer events have fired against ctx. Returns 0 when WithCounter wasn't called on this context.

func Healthcheck

func Healthcheck(ctx context.Context, pool *pgxpool.Pool) error

Healthcheck performs a fast SELECT 1 against the pool with a short timeout. Used by /readyz.

func Migrate

func Migrate(ctx context.Context, cfg Config, action MigrateAction) error

Migrate runs the requested goose action against the pool's underlying database. We reuse the pool's connection config rather than opening a fresh sql.DB from scratch, so the same DSN env-var-driven configuration applies to migrations.

goose requires `database/sql`. We bridge from pgx's connection config using `pgx/v5/stdlib`.

func Open

func Open(ctx context.Context, cfg Config) (*pgxpool.Pool, error)

Open creates a new pgx pool from cfg. The caller owns the pool's lifecycle and must call pool.Close() on shutdown.

func SetMigrationsFS

func SetMigrationsFS(fsys fs.FS)

SetMigrationsFS registers the migrations filesystem. The repo's `internal/migrationsfs` package embeds and registers it; tests can swap it for a fixture FS.

func WithCounter

func WithCounter(ctx context.Context) context.Context

WithCounter returns a derived context that records query counts. Pair with Read.

func WithTx

func WithTx(ctx context.Context, pool *pgxpool.Pool, fn func(pgx.Tx) error) (err error)

WithTx runs fn inside a Postgres transaction, committing on nil error and rolling back otherwise. Panics inside fn are recovered and re-raised after rollback so callers see them as panics rather than silent commits.

Types

type Config

type Config struct {
	URL             string
	MaxConns        int32
	MinConns        int32
	ConnectTimeout  time.Duration
	StatementCancel time.Duration
}

Config carries the pool's connection settings. S03 will populate this from the layered config loader; for S01 we accept an optional explicit URL or fall back to SHITHUB_DATABASE_URL.

func Defaults

func Defaults() Config

Defaults returns sensible defaults for a dev pool. Prod values land via the config loader in S03.

func (Config) Resolve

func (c Config) Resolve() Config

Resolve fills in URL from env if missing and clamps numeric defaults.

type MigrateAction

type MigrateAction string

MigrateAction is one of the migrate operations the CLI exposes.

const (
	MigrateUp      MigrateAction = "up"
	MigrateDown    MigrateAction = "down"
	MigrateStatus  MigrateAction = "status"
	MigrateVersion MigrateAction = "version"
	MigrateRedo    MigrateAction = "redo"
	MigrateReset   MigrateAction = "reset"
)

type QueryCounter

type QueryCounter struct{}

QueryCounter is a pgx QueryTracer that increments a per-context counter on every Query/QueryRow/Exec. The counter lives on the request context, so the tracer is safe to install at pool-config time even when most requests don't care.

Use case: handler integration tests assert "this route does ≤ N DB queries." The tracer + WithCounter / FromContext + a tiny middleware (web/middleware/query_count_assert.go) make that assertion a one-liner.

Production runs leave the tracer installed but never call WithCounter; the per-conn overhead is one atomic-load per query (the tracer reads the context value but the counter is nil so no Add fires).

func (QueryCounter) TraceQueryEnd

TraceQueryEnd implements pgx.QueryTracer. No-op; the start tick is the meaningful event.

func (QueryCounter) TraceQueryStart

TraceQueryStart implements pgx.QueryTracer. The counter increments at start, not end, so a slow query still counts.

Jump to

Keyboard shortcuts

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