migrator

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package migrator orchestrates database migrations for contrib-backed services.

When loading from multiple directories or embedded filesystems, duplicate migration version+direction pairs are rejected instead of silently overriding one another. If commit acknowledgement is ambiguous after the SQL has executed, the runner records the migration as uncertain and blocks later runs until an operator reconciles the database state and migration record.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChecksumMismatchError

type ChecksumMismatchError struct {
	Version int64
	Name    string
	Have    string
	Want    string
}

ChecksumMismatchError reports that a migration's recorded checksum does not match the on-disk migration contents.

func (*ChecksumMismatchError) Error

func (e *ChecksumMismatchError) Error() string

type Migration

type Migration struct {
	Version  int64
	Name     string
	Dir      string // "up" or "down"
	File     string
	SQL      string
	Checksum string
}

Migration is a versioned SQL change.

type Options

type Options struct {
	// MigrationsDir is kept for backward compatibility. If non-empty,
	// it will be appended to MigrationsDirs.
	MigrationsDir string
	// MigrationsDirs allows loading migrations from multiple directories.
	// Directories are processed in order and duplicate version+direction
	// pairs are rejected when loading.
	MigrationsDirs []string
	// EmbeddedFSs allows loading migrations from multiple embedded
	// filesystems. Each is expected to contain a "migrations" directory.
	// Embedded filesystems are appended after explicit directories.
	EmbeddedFSs []fs.FS
	TableName   string
	LockKey     int64
	// LockTimeout bounds advisory lock acquisition. Zero keeps the default
	// 10 minute timeout used by earlier versions.
	LockTimeout time.Duration
	// AllowDangerousDown enables Down(); keep disabled in API binaries.
	AllowDangerousDown bool
	// Logger outputs formatted status messages.
	Logger func(format string, args ...any)
	// UnlockFailureHandler observes advisory unlock failures without changing
	// the primary migration result.
	UnlockFailureHandler func(error)
}

Options configures the runner.

type Runner

type Runner struct {
	DB   *sql.DB
	Opts Options
	// contains filtered or unexported fields
}

Runner executes SQL migrations transactionally and records state.

func New

func New(db *sql.DB, opts Options) *Runner

New constructs a migration runner using the provided database handle.

func (*Runner) Down

func (r *Runner) Down(ctx context.Context) error

Down reverts the latest successfully applied version by 1 step.

func (*Runner) Status

func (r *Runner) Status(ctx context.Context) (string, error)

Status reports current and pending state.

func (*Runner) Up

func (r *Runner) Up(ctx context.Context) error

Up applies all pending "up" migrations.

type UncertainMigrationError added in v2.1.0

type UncertainMigrationError struct {
	Version   int64
	Name      string
	TableName string
	Err       error
}

UncertainMigrationError reports that a migration may have committed even though the runner could not confirm that outcome cleanly.

func (*UncertainMigrationError) Error added in v2.1.0

func (e *UncertainMigrationError) Error() string

func (*UncertainMigrationError) Unwrap added in v2.1.0

func (e *UncertainMigrationError) Unwrap() error

type UnresolvedMigrationStateError added in v2.1.0

type UnresolvedMigrationStateError struct {
	Version   int64
	Name      string
	State     string
	TableName string
}

UnresolvedMigrationStateError reports that a prior migration run ended in a state that must be reconciled manually before the runner can continue.

func (*UnresolvedMigrationStateError) Error added in v2.1.0

Jump to

Keyboard shortcuts

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