sqlmigrate

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MPL-2.0-no-copyleft-exception Imports: 7 Imported by: 7 modules, 6 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sqlmigrate

Database-agnostic SQL migration library for Go.

Go Reference

Backend packages

Each backend is a separate Go module to avoid pulling unnecessary drivers:

Package Database Driver
pgmigrate PostgreSQL pgx/v5
mymigrate MySQL / MariaDB go-sql-driver/mysql
litemigrate SQLite database/sql (caller imports driver)
msmigrate SQL Server go-mssqldb
shmigrate Shell scripts (uses native CLI)

CLI

The sql-migrate CLI uses shmigrate to generate shell scripts for managing migrations without a Go dependency at runtime.

Documentation

Overview

Package sqlmigrate provides a database-agnostic SQL migration interface.

Backend implementations (each a separate Go module):

  • pgmigrate: PostgreSQL via pgx/v5
  • mymigrate: MySQL/MariaDB via go-sql-driver/mysql
  • litemigrate: SQLite via database/sql
  • msmigrate: SQL Server via go-mssqldb
  • shmigrate: POSIX shell script generation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingUp    = errors.New("missing up migration")
	ErrMissingDown  = errors.New("missing down migration")
	ErrWalkFailed   = errors.New("walking migrations")
	ErrExecFailed   = errors.New("migration exec failed")
	ErrQueryApplied = errors.New("querying applied migrations")
	ErrInvalidN     = errors.New("n must be positive or -1 for all")
)

Sentinel errors for migration operations.

Functions

This section is empty.

Types

type Migration

type Migration struct {
	ID   string // 8-char hex from INSERT INTO _migrations, parsed by Collect
	Name string // e.g. "2026-04-05-001000_create-todos"
}

Migration identifies a migration by its name and optional hex ID.

func Down

func Down(ctx context.Context, r Migrator, ddls []Script, n int) ([]Migration, error)

Down rolls back up to n applied migrations, most recent first. If n < 0, rolls back all applied. If n == 0, returns ErrInvalidN. Returns the rolled-back migrations.

func Drop added in v1.0.1

func Drop(ctx context.Context, r Migrator, ddls []Script) ([]Migration, error)

Drop rolls back all applied migrations. Equivalent to Down(ctx, r, ddls, -1).

func Latest added in v1.0.1

func Latest(ctx context.Context, r Migrator, ddls []Script) ([]Migration, error)

Latest applies all pending migrations. Equivalent to Up(ctx, r, ddls, -1).

func Up

func Up(ctx context.Context, r Migrator, ddls []Script, n int) ([]Migration, error)

Up applies up to n pending migrations using the given Migrator. If n < 0, applies all pending. If n == 0, returns ErrInvalidN. Returns the applied migrations.

type Migrator

type Migrator interface {
	// ExecUp runs the up migration SQL. For database migrators this
	// executes the SQL in a transaction. For shell migrators this
	// outputs a command referencing the .up.sql file.
	ExecUp(ctx context.Context, m Migration, sql string) error

	// ExecDown runs the down migration SQL.
	ExecDown(ctx context.Context, m Migration, sql string) error

	// Applied returns all applied migrations from the _migrations table,
	// sorted lexicographically by name. Returns an empty slice (not an
	// error) if the migrations table or log does not exist yet.
	Applied(ctx context.Context) ([]Migration, error)
}

Migrator executes migrations. Implementations handle the database-specific or output-specific details.

type Script added in v1.0.2

type Script struct {
	Migration
	Up   string // SQL content of the .up.sql file
	Down string // SQL content of the .down.sql file
}

Script is a Migration with its up and down SQL content, as returned by Collect.

func Collect

func Collect(fsys fs.FS, subpath string) ([]Script, error)

Collect reads .up.sql and .down.sql files from fsys under subpath, pairs them by basename, and returns them sorted lexicographically by name. If subpath is "" or ".", the root of fsys is used. If the up SQL contains an INSERT INTO _migrations line, the hex ID is extracted and stored in Script.ID.

func NamesOnly

func NamesOnly(names []string) []Script

NamesOnly builds a Script slice from a list of names, with empty Up/Down content. Useful for shell-based runners that reference files on disk rather than executing SQL directly.

type Status

type Status struct {
	Applied []Migration
	Pending []Migration
}

Status represents the current migration state.

func GetStatus

func GetStatus(ctx context.Context, r Migrator, ddls []Script) (*Status, error)

GetStatus returns applied and pending migration lists.

Directories

Path Synopsis
litemigrate module
msmigrate module
mymigrate module
pgmigrate module
shmigrate module

Jump to

Keyboard shortcuts

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