migrations

package
v0.70.12 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

Package migrations implements offline, forward-only metadata upgrades.

Index

Constants

View Source
const DefaultBatchSize = 1000

Variables

View Source
var (
	ErrEmptyRegistry   = errors.New("metadata migration registry is empty")
	ErrInvalidRegistry = errors.New("invalid metadata migration registry")
	ErrChecksumDrift   = errors.New("metadata migration checksum changed")
	ErrNewerSchema     = errors.New("metadata schema is newer than this binary")
	ErrLegacySchema    = errors.New("unsupported unversioned metadata schema")
)

Functions

This section is empty.

Types

type Backfill

type Backfill func(context.Context, Batch) (BatchResult, error)

Backfill executes at most Batch.Limit rows. Runner commits its data changes and returned cursor in the same transaction.

type Batch

type Batch struct {
	Tx     *sql.Tx
	Cursor string
	Limit  int
	// Rebind converts ? placeholders to the dialect's own form. It is never
	// nil; the runner substitutes an identity function for dialects that take
	// ? directly.
	Rebind func(string) string
}

Batch is one resumable backfill transaction.

type BatchResult

type BatchResult struct {
	Cursor string
	Rows   int64
	Done   bool
}

BatchResult describes the durable checkpoint after a backfill batch.

type Locker

type Locker interface {
	Acquire(context.Context, *sql.Conn) (func() error, error)
}

Locker serializes migration runners. Advisory implementations must use the supplied connection so lock ownership lasts until release.

func NewAdvisoryLocker

func NewAdvisoryLocker(
	dialect string,
	key int64,
	timeout time.Duration,
) Locker

NewAdvisoryLocker returns a connection-owned PostgreSQL or MySQL lock.

func NewFileLocker

func NewFileLocker(path string) Locker

NewFileLocker returns the cross-process lock used by file-backed SQLite.

func NewProcessLocker

func NewProcessLocker() Locker

NewProcessLocker returns a process-wide lock suitable for isolated in-memory SQLite databases.

type Migration

type Migration struct {
	Version          int
	Name             string
	BackfillRevision string
	SQL              map[string]SQL
	Backfill         Backfill
	BatchSize        int
}

Migration is immutable after release. BackfillRevision must change whenever the Go backfill behavior changes.

func MySQLRegistry

func MySQLRegistry() ([]Migration, error)

func PostgresRegistry

func PostgresRegistry() ([]Migration, error)

PostgresRegistry and MySQLRegistry expose the same v1alpha1 schema contract with backend-native type and identity syntax. Keeping the migration registry shared prevents the three providers from drifting at the schema boundary while allowing each engine to execute its own DDL.

func SQLiteRegistry

func SQLiteRegistry() ([]Migration, error)

SQLiteRegistry returns the checked-in SQLite migration registry.

type Phase

type Phase string

Phase identifies the durable point at which an interrupted upgrade resumes.

const (
	PhaseExpand   Phase = "expand"
	PhaseBackfill Phase = "backfill"
	PhaseContract Phase = "contract"
	PhaseComplete Phase = "complete"
)

type Runner

type Runner struct {
	DB       *sql.DB
	Dialect  string
	Registry []Migration
	Locker   Locker
	Logger   *slog.Logger
	Now      func() time.Time
	// Rebind converts ? placeholders to the dialect's own form for data-driven
	// backfills. Leave nil to use the runner's own Dialect-derived rebinder;
	// an identity default would feed ? straight to a dialect that rejects it.
	Rebind func(string) string
}

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) (runErr error)

Run holds the backend migration lock for the entire compatibility check and upgrade. A failure prevents the caller from advertising store readiness.

type SQL

type SQL struct {
	Expand   []string
	Contract []string
}

SQL contains idempotent DDL statements for one migration phase.

type UpgradeError

type UpgradeError struct {
	Version int
	Name    string
	Phase   Phase
	Err     error
}

UpgradeError identifies the exact migration phase that blocked readiness.

func (*UpgradeError) Error

func (e *UpgradeError) Error() string

func (*UpgradeError) Unwrap

func (e *UpgradeError) Unwrap() error

Jump to

Keyboard shortcuts

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