migrate

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package migrate applies paired .up.sql and .down.sql files using database/sql. It intentionally does not choose a SQL driver or hide the underlying *sql.DB.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPendingMigration means that integrity verification found an unapplied
	// migration after verifying all recorded migrations. It does not apply it.
	ErrPendingMigration = errors.New("migrate: pending migration")
	// ErrMigrationDrift means a recorded migration no longer matches its source,
	// or a row has lost its checksum despite an integrity-enabled ledger. Removal
	// of an entire source pair, changed names, and changed up-script checksums are
	// drift. An incomplete or inconsistently named pair is invalid source instead.
	ErrMigrationDrift = errors.New("migrate: migration drift")
	// ErrInvalidMigrationSource means discovered migration source has an incomplete
	// pair, an empty script, or inconsistent names for one version. Source is
	// validated before ledger inspection, so this does not establish whether the
	// affected version was applied. Filesystem I/O errors are not this category.
	ErrInvalidMigrationSource = errors.New("migrate: invalid migration source")
	// ErrLedgerUpgradeRequired means the ledger is uninitialized or uses legacy
	// checksum bookkeeping. Verify never creates or upgrades the ledger.
	ErrLedgerUpgradeRequired = errors.New("migrate: ledger upgrade required")
)

Functions

This section is empty.

Types

type Dialect

type Dialect string
const (
	// Postgres migrations are serialized with a database advisory lock and each
	// migration is atomic when its SQL is transaction-safe.
	Postgres Dialect = "postgres"
	// MySQL is supported for bindings and bookkeeping, but MySQL DDL may commit
	// implicitly and therefore is not promised to be atomic.
	MySQL Dialect = "mysql"
	// SQLite relies on SQLite's transaction and locking behavior.
	SQLite Dialect = "sqlite"
)

type Migration

type Migration struct {
	// Version is ordered lexically; use consistently padded numbers or timestamps.
	Version string
	Name    string
	Up      string
	Down    string
}

type Migrator

type Migrator struct {
	DB      *sql.DB
	Files   fs.FS
	Dir     string
	Dialect Dialect
	Table   string
	// LockID identifies the PostgreSQL advisory lock used to serialize a complete
	// migration run. Zero selects GoForge's stable default lock ID.
	LockID int64
	// contains filtered or unexported fields
}

func (*Migrator) Down

func (migrator *Migrator) Down(ctx context.Context, steps int) ([]Migration, error)

Down reverses the latest applied versions. A nonpositive step count means one. On error, the result includes earlier committed reversals.

func (*Migrator) Status

func (migrator *Migrator) Status(ctx context.Context) ([]Status, error)

func (*Migrator) Up

func (migrator *Migrator) Up(ctx context.Context, limit int) ([]Migration, error)

Up applies pending migrations in version order. A nonpositive limit applies all pending migrations. On error, the result includes earlier committed work.

func (*Migrator) Verify added in v0.9.0

func (migrator *Migrator) Verify(ctx context.Context) error

Verify checks migration integrity without creating or altering database objects. It is suitable for request-path readiness checks; use Status for operational inspection that may upgrade a legacy migration ledger. Integrity failures wrap ErrLedgerUpgradeRequired, ErrMigrationDrift, or ErrPendingMigration for errors.Is classification. Invalid source pairs wrap ErrInvalidMigrationSource before the ledger is inspected. Other failures retain their underlying database or filesystem error; error details are not safe to expose to untrusted users.

type Status

type Status struct {
	Version   string
	Name      string
	Applied   bool
	AppliedAt *time.Time
}

Jump to

Keyboard shortcuts

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