migrate

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package migrate provides a PostgreSQL migration loader and a Migrator that applies, reverts, and reports the status of versioned SQL migrations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, db DBTX, sources ...fs.FS) error

Migrate loads migrations from one or more filesystems (merged, then sorted by version) and applies all pending ones. It is intended for service startup, e.g.:

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

if err := migrate.Migrate(ctx, pool, migrationsFS); err != nil { ... }

*pgxpool.Pool satisfies DBTX. Duplicate versions across the merged sources are an error. With no sources Migrate is a no-op and returns nil.

Types

type AppliedMigration

type AppliedMigration struct {
	Version   string
	Name      string
	Checksum  string
	AppliedAt time.Time
}

AppliedMigration is a row recorded in the bookkeeping table.

type DBTX

type DBTX interface {
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	Begin(ctx context.Context) (pgx.Tx, error)
}

DBTX is the database handle the Migrator runs against. *pgxpool.Pool satisfies it, as does a *pgx.Conn wrapper that exposes Begin.

type Migration

type Migration struct {
	Version  string
	Name     string
	UpSQL    string
	DownSQL  string
	Checksum string // sha256 hex of the raw file bytes
}

Migration is a single versioned SQL migration loaded from a file.

A migration file is named "<version>_<name>.sql". Its body may contain "-- sqld:up" and "-- sqld:down" markers that split the up and down sections; when no markers are present the whole file is the up section.

func Load

func Load(dir string) ([]Migration, error)

Load reads all *.sql migrations from an OS directory and returns them sorted by version. It delegates to LoadFS over os.DirFS(dir).

Each filename is parsed as "<version>_<name>.sql": everything up to the first '_' is the version, the remainder (minus the .sql extension) is the name. The file body is split into up/down sections on the "-- sqld:up" / "-- sqld:down" markers (case-insensitive, whole-line); with no markers the whole file is up. Checksum is the sha256 hex of the raw file bytes.

func LoadFS

func LoadFS(fsys fs.FS) ([]Migration, error)

LoadFS reads all *.sql migrations from an fs.FS (e.g. an embed.FS) and returns them sorted by version.

It walks the filesystem recursively, so an `//go:embed migrations` directive (whose files live under "migrations/") works without the caller doing fs.Sub. Each base filename is parsed as "<version>_<name>.sql"; the body is split on the "-- sqld:up" / "-- sqld:down" markers (with no markers the whole file is up); Checksum is the sha256 hex of the raw file bytes. Non-.sql files are skipped.

type Migrator

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

Migrator applies, reverts, and reports the status of a set of migrations.

func New

func New(db DBTX, migrations []Migration, opts ...Option) *Migrator

New creates a Migrator over db using the given migrations. The migrations are kept in version order regardless of input order.

func (*Migrator) Applied

func (m *Migrator) Applied(ctx context.Context) ([]AppliedMigration, error)

Applied returns the recorded migrations in ascending version order.

func (*Migrator) Down

func (m *Migrator) Down(ctx context.Context, n int) error

Down reverts the last n applied migrations (descending version order), running each migration's DownSQL in its own transaction. It is an error to revert a migration with no DownSQL.

func (*Migrator) Pending

func (m *Migrator) Pending(ctx context.Context) ([]Migration, error)

Pending returns loaded migrations not yet applied, in version order.

func (*Migrator) Status

func (m *Migrator) Status(ctx context.Context) (Status, error)

Status reports applied, pending, and drifted migrations.

func (*Migrator) To

func (m *Migrator) To(ctx context.Context, version string) error

To migrates up or down so the applied set covers exactly the loaded migrations with version <= the target version. Migrations with version <= target that are not applied are applied (ascending); applied migrations with version > target are reverted (descending).

func (*Migrator) Up

func (m *Migrator) Up(ctx context.Context) error

Up applies all pending migrations in version order, each in its own transaction, under a session advisory lock.

type Option

type Option func(*Migrator)

Option configures a Migrator.

func WithTable

func WithTable(name string) Option

WithTable overrides the bookkeeping table name (default "sqld_migrations").

type Status

type Status struct {
	Applied []AppliedMigration
	Pending []Migration
	Drift   []string
}

Status summarizes the migration state of a database.

Applied lists the rows recorded in the bookkeeping table (version order). Pending lists loaded migrations not yet applied (version order). Drift lists versions whose recorded checksum differs from the loaded migration's checksum — i.e. a migration file changed after being applied.

Jump to

Keyboard shortcuts

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