dbmigrate

package
v0.0.0-...-9358009 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package dbmigrate registers a generated project's per-domain migrations, orders them by declared dependencies, and applies pending ones using Bun's migration table for bookkeeping.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotConfirmed = errors.New("rollback not confirmed")

ErrNotConfirmed is what Migrate returns when confirm declines a non-empty rollback.

View Source
var ErrStateChanged = errors.New("migration state changed since planning; rerun")

ErrStateChanged is returned by Migrate when the applied set changed between planning and locking, so the confirmed plan no longer describes what would run; rerun to plan against the new state.

Functions

func Migrate

func Migrate(ctx context.Context, db *bun.DB, target Target, confirm func(rollback []Migration) bool) ([]Migration, []Migration, error)

Migrate brings target's domain to target.Version, rolling back before applying; confirm is asked before a non-empty rollback and must return true to proceed. It stops at the first failure.

func Register

func Register(up, down MigrationFunc, opts ...Option)

Register records the calling file as a migration. It must be called from init() in a file at backend/<domain>/migrations/<NNNN>_<name>.go; any other path panics.

Types

type Dependency

type Dependency struct {
	Domain, Version string
}

Dependency names another domain's migration that must run first.

type Migration

type Migration struct {
	Domain  string
	Version string
	Name    string
}

Migration identifies one registered migration.

func Apply

func Apply(ctx context.Context, db *bun.DB) ([]Migration, error)

Apply applies every registered migration not yet recorded in the database, in dependency order, and returns the ones it applied. It stops at the first failure, leaving earlier ones applied.

func (Migration) String

func (m Migration) String() string

String is Domain + "/" + Version — the Bun row name and the key used in every error.

type MigrationFunc

type MigrationFunc func(ctx context.Context, db *bun.DB) error

MigrationFunc is the shape of a migration's up and down functions, matching Bun's own migration signature.

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option is what After returns; Register accepts any number.

func After

func After(domain, version string) Option

After declares that this migration must run after version of domain (and so after every earlier version of that domain). Pass one per dependency; Register accepts any number.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry is the set Register adds to. The package-level default is what Register and Apply use; tests build their own.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func (*Registry) Add

func (r *Registry) Add(m Migration, up, down MigrationFunc, deps ...Dependency) error

Add records m with its up/down functions and cross-domain dependencies. A duplicate (Domain, Version) returns an error.

func (*Registry) Apply

func (r *Registry) Apply(ctx context.Context, db *bun.DB) (applied []Migration, err error)

Apply is the Registry-scoped form of the package-level Apply; see its doc comment.

func (*Registry) Migrate

func (r *Registry) Migrate(ctx context.Context, db *bun.DB, target Target, confirm func(rollback []Migration) bool) (rolledBack, applied []Migration, err error)

Migrate is the Registry-scoped form of the package-level Migrate; see its doc comment. It re-plans after locking and returns ErrStateChanged if a concurrent run changed the applied set.

func (*Registry) Order

func (r *Registry) Order() ([]Migration, error)

Order returns every registered migration in dependency order, ties broken by (Domain, Version) so runs are deterministic.

func (*Registry) Plan

func (r *Registry) Plan(target Target, applied map[string]bool) (rollback, apply []Migration, err error)

Plan computes what bringing applied to target requires: rollback to unapply (reverse dependency order) and apply to run (dependency order). Both empty means target is already met.

type Target

type Target struct{ Domain, Version string }

Target names the state one domain should be brought to. Version "zero" means none of the domain's migrations applied.

func ParseTarget

func ParseTarget(s string) (Target, error)

ParseTarget parses "<domain>/<version>" ("<domain>/zero" allowed).

Jump to

Keyboard shortcuts

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