sqlmigrate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 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

func Down

func Down(ctx context.Context, r Migrator, migrations []Migration, n int) ([]string, 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 names of rolled-back migrations.

func Up

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

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

Types

type AppliedMigration

type AppliedMigration struct {
	ID   string
	Name string
}

AppliedMigration represents a migration recorded in the _migrations table.

type Migration

type Migration struct {
	Name string // e.g. "2026-04-05-001000_create-todos"
	ID   string // 8-char hex from INSERT INTO _migrations, parsed by Collect
	Up   string // SQL content of the .up.sql file
	Down string // SQL content of the .down.sql file
}

Migration represents a paired up/down migration.

func Collect

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

Collect reads .up.sql and .down.sql files from fsys, pairs them by basename, and returns them sorted lexicographically by name. If the up SQL contains an INSERT INTO _migrations line, the hex ID is extracted and stored in Migration.ID.

func NamesOnly

func NamesOnly(names []string) []Migration

NamesOnly builds a Migration 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 Migrator

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

	// ExecDown runs the down migration.
	ExecDown(ctx context.Context, m Migration) 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) ([]AppliedMigration, error)
}

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

type Status

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

Status represents the current migration state.

func GetStatus

func GetStatus(ctx context.Context, r Migrator, migrations []Migration) (*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