migrate

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Migration

type Migration struct {
	Forward  func(ctx context.Context, tx *den.Tx) error
	Backward func(ctx context.Context, tx *den.Tx) error
}

Migration defines a forward and optional backward migration function. Both receive a transaction for atomic execution.

type Option added in v0.11.0

type Option func(*Registry)

Option configures a Registry at construction time.

func WithLogger added in v0.11.0

func WithLogger(l *slog.Logger) Option

WithLogger sets the slog.Logger that receives migration lifecycle events. When unset, the Registry uses slog.Default().

Events emitted (all with the migration's version attached as "version" and the direction as "direction" = "up" | "down"):

  • "migration_start" — a migration is about to run
  • "migration_success" — migration completed; "duration_ms" attached
  • "migration_failure" — migration failed; "duration_ms" and "error" attached (the error is NOT re-logged at the caller's log site — the error value still bubbles up through the return chain)
  • "ensure_table_failure" — the one-time migration-log setup failed; "error" attached. Subsequent calls reuse the cached failure, so this event fires at most once per Registry.

Passing a nil logger is treated as "use slog.Default()" — keeps callers honest if they accidentally thread nil through a config struct.

type Registry

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

Registry holds registered migrations and provides the runner API.

A Registry caches the result of its one-time migration-log setup: the first Up/UpOne/Down/DownOne caller runs EnsureCollection for the internal _den_migrations collection, and every subsequent call on the same Registry reuses that outcome — success OR error. This keeps concurrent in-process starters from racing CREATE INDEX CONCURRENTLY on PostgreSQL, but it also means startup errors are sticky: if EnsureCollection fails (wrong credentials, missing permissions, unreachable backend), every subsequent call returns the same cached error even after the underlying problem is fixed. To retry, construct a fresh Registry with NewRegistry and re- Register every migration; there is no Reset by design — tests that need a clean slate should rebuild the Registry.

func NewRegistry

func NewRegistry(opts ...Option) *Registry

NewRegistry creates a new migration registry. Optional arguments configure the registry; see WithLogger.

func (*Registry) Down

func (r *Registry) Down(ctx context.Context, db *den.DB) error

Down rolls back all applied migrations in reverse order, each in its own transaction.

See the Registry godoc for the one-time migration-log setup and the sticky-error caveat that affects repeated calls.

func (*Registry) DownOne

func (r *Registry) DownOne(ctx context.Context, db *den.DB) error

DownOne rolls back the last applied migration in a transaction.

See the Registry godoc for the one-time migration-log setup and the sticky-error caveat that affects repeated calls.

func (*Registry) Register

func (r *Registry) Register(version string, m Migration)

Register adds a migration with the given version identifier. Versions are sorted lexicographically for execution order.

func (*Registry) Up

func (r *Registry) Up(ctx context.Context, db *den.DB) error

Up runs all pending forward migrations, each in its own transaction. Concurrent starters serialize on an advisory lock; every registered migration runs exactly once across processes.

See the Registry godoc for the one-time migration-log setup and the sticky-error caveat that affects repeated calls.

func (*Registry) UpOne

func (r *Registry) UpOne(ctx context.Context, db *den.DB) error

UpOne runs the next pending forward migration in a transaction. Loads the current applied set to find the first pending version; the actual "apply exactly once" guarantee comes from the lock in runForward.

See the Registry godoc for the one-time migration-log setup and the sticky-error caveat that affects repeated calls.

Jump to

Keyboard shortcuts

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