sql

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package sql contains types and providers for connecting to and migrating SQL databases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckMigrations added in v0.7.0

func CheckMigrations(ctx context.Context, dsn *url.URL, db *sql.DB, migrations Migrations) error

CheckMigrations returns an error if there are missing migrations.

func Migrate added in v0.7.0

func Migrate(ctx context.Context, logger *slog.Logger, dsn *url.URL, db *sql.DB, migrations Migrations) error

Migrate applies all pending migrations to the database.

func New

func New(ctx context.Context, config Config, logger *slog.Logger, migrations Migrations) (db *sql.DB, err error)

New creates a new SQL database connection, applying migrations or verifying migrations have been applied.

Types

type Config

type Config struct {
	Migrate bool     `help:"Enable automatic migration of the database schema."`
	DSN     *url.URL `default:"${sqldsn=postgres://postgres:secret@localhost:5432/zero?sslmode=disable}" help:"DSN for the SQL connection."`
}

type Migrations added in v0.7.0

type Migrations []fs.FS

Migrations represents a set of SQL migrations.

Create a provider that returns an fs.FS with your .sql migrations at the root of the FS. eg.

//go:embed migrations/*.sql
var migrations embed.FS

//zero:provider multi
func Migrations() Migrations {
	sub, _ := fs.Sub(migrations, "migrations")
	return []fs.FS{sub}
}

Jump to

Keyboard shortcuts

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