up

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package up provides a simple database migration tool.

Index

Constants

View Source
const (
	RunTargetLatest     = -1
	RevertTargetInitial = 0
)

Variables

View Source
var (
	ErrLocked          = errors.New("version store is locked for writing")
	ErrInitialVersion  = errors.New("initial version is current")
	ErrVersionNotFound = errors.New("version not found")
)

Functions

This section is empty.

Types

type FuncLoader

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

FuncLoader is a Loader that loads a slice of Migrations.

func NewFuncLoader

func NewFuncLoader(migrations ...*Migration) *FuncLoader

NewFuncLoader creates a new FuncLoader with the given migrations.

func (*FuncLoader) Load

func (l *FuncLoader) Load(ctx context.Context) ([]*Migration, error)

Load loads the migrations from the FuncLoader.

type Loader

type Loader interface {
	Load(context.Context) ([]*Migration, error)
}

A Loader provides a way to load migrations from a source.

type Migration

type Migration struct {
	Version    int64
	Name       string
	RunFunc    func(context.Context, *sql.DB) error
	RevertFunc func(context.Context, *sql.DB) error
}

A Migration represents a schema change operation. Version indicates the migration's order in the change sequence. The Run and Revert functions are used to apply and revert the migration, respectively.

func (*Migration) Revert

func (m *Migration) Revert(ctx context.Context, db *sql.DB) error

Revert reverses the database migration.

func (*Migration) Run

func (m *Migration) Run(ctx context.Context, db *sql.DB) error

Run applies the migration to the database.

type Migrator

type Migrator struct {
	Store     Store
	Sources   []*Migration
	LogFunc   func(s string)
	DebugFunc func(s string)

	HoldLockOnFailure bool
}

A Migrator stores migrations and provides methods to apply or revert them.

func (*Migrator) Revert

func (m *Migrator) Revert(ctx context.Context, to int64) (n int, err error)

Revert reverses migrations down to and excluding the provided version. The special value 0 reverts all migrations.

func (*Migrator) Run

func (m *Migrator) Run(ctx context.Context, to int64) (n int, err error)

Run applies migrations up to and including the specified version. The special value -1 applies all pending migrations.

type Store

type Store interface {
	DB() *sql.DB
	Init(context.Context) error
	Lock(context.Context) error
	Release(context.Context) error
	Version(context.Context) (int64, error)
	Insert(context.Context, int64) error
	Remove(context.Context, int64) error
}

Store is an interface for a schema version store.

Directories

Path Synopsis
stores
sqlite3store
Package sqlite3store provides a SQLite3 implementation of the up.Store interface.
Package sqlite3store provides a SQLite3 implementation of the up.Store interface.

Jump to

Keyboard shortcuts

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