sql

package module
v0.0.0-...-b4a2bd7 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 21 Imported by: 0

README

sql

Go Reference codecov

A database/sql connection pool to PostgreSQL, MySQL or SQLite, instrumented with OpenTelemetry, with a health probe and migrations.

go get github.com/go-oryn/oryn/sql
sql:
  driver: postgres   # postgres, mysql or sqlite
  dsn: postgres://user:password@localhost:5432/app
core.New(
	core.WithConfigFS(configs.FS),
	sql.Module,
	sql.AsMigrations(migrations.FS),
).Run(sql.Migrate(), httpserver.Serve())

Documentation

Page Source
SQL (configuration, observability) docs/sql/overview.md
Migrations docs/sql/migrations.md
Testing docs/sql/testing.md

Documentation

Overview

Package sql provides a database/sql connection pool to PostgreSQL, MySQL or SQLite, instrumented with OpenTelemetry, with a health probe and migrations.

core.New(
	core.WithConfigFS(configs.FS),
	sql.Module,
	sql.AsMigrations(migrations.FS),
).Run(sql.Migrate(), httpserver.Serve())

The pool is provided as a *database/sql.DB.

Index

Constants

View Source
const (
	DriverPostgres = "postgres"
	DriverMySQL    = "mysql"
	DriverSQLite   = "sqlite"
)

Supported values of sql.driver.

View Source
const DefaultMaxIdleConns = 2

DefaultMaxIdleConns is the default of sql.max_idle_conns, the database/sql default.

View Source
const ModuleName = "sql"

ModuleName is the name of the sql fx module.

Variables

Module provides the *database/sql.DB and its Driver, and registers its readiness and startup health.Probe.

Functions

func AsMigrations

func AsMigrations(fsys fs.FS) fx.Option

AsMigrations registers the migrations: the goose SQL files at the root of fsys (e.g. 00001_create_users.sql), applied by Migrate. Register them once.

-- +goose Up
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);

-- +goose Down
DROP TABLE users;

See https://pressly.github.io/goose/documentation/annotations/.

func Migrate

func Migrate() fx.Option

Migrate applies the pending migrations registered with AsMigrations when the application starts, before the start hooks registered after it: pass it before the servers to Run. The application fails to start if a migration fails.

func NewDB

func NewDB(params NewDBParams) (*stdsql.DB, error)

NewDB opens the connection pool configured by the sql.* keys, instrumented with OpenTelemetry. Unless sql.ping_on_start is false, the application fails to start if the database is unreachable. The pool is closed when the application stops.

Types

type Driver

type Driver struct {
	// Name is the sql.driver value: postgres, mysql or sqlite.
	Name string
	// contains filtered or unexported fields
}

Driver describes a supported database.

func DriverOf

func DriverOf(name string) (Driver, error)

DriverOf returns the Driver of a sql.driver value.

func NewDriver

func NewDriver(cfg *config.Config) (Driver, error)

NewDriver returns the Driver configured with sql.driver.

type MigrateParams

type MigrateParams struct {
	fx.In

	Lifecycle      fx.Lifecycle
	Driver         Driver
	Logger         *slog.Logger
	TracerProvider trace.TracerProvider
	DB             *stdsql.DB
	Migrations     []fs.FS `group:"oryn-sql-migrations"`
}

MigrateParams are the dependencies of Migrate.

type NewDBParams

type NewDBParams struct {
	fx.In

	Lifecycle      fx.Lifecycle
	Config         *config.Config
	Driver         Driver
	TracerProvider trace.TracerProvider
	MeterProvider  metric.MeterProvider
	Propagator     propagation.TextMapPropagator
}

NewDBParams are the dependencies of NewDB.

type Probe

type Probe struct {
	// contains filtered or unexported fields
}

Probe is a health.Probe pinging the database.

func NewProbe

func NewProbe(db *stdsql.DB) *Probe

NewProbe returns a Probe.

func (*Probe) Check

func (p *Probe) Check(ctx context.Context) error

Check pings the database.

func (*Probe) Name

func (p *Probe) Name() string

Name returns "sql".

Jump to

Keyboard shortcuts

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