go_migrate

package module
v0.0.0-...-4404746 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 13 Imported by: 0

README

go-migrate

go-migrate is an simple package that provides a tool for database migrations. Still in development.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMigrator = xerrors.Message("migrator")

Functions

This section is empty.

Types

type Action

type Action struct {
	Version   int
	Migration string
	Direction Direction
}

type Database

type Database interface {
	List(ctx context.Context) ([]int, error)
	Migrate(ctx context.Context, actions []Action) error
}

Database is the interface for the database on which the migrations will be executed.

type Direction

type Direction uint8

Direction is a migration direction.

const (
	Up   Direction = 1
	Down Direction = 2
)

type FS

type FS interface {
	fs.FS
	fs.ReadDirFS
	fs.ReadFileFS
}

type FilesystemProvider

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

func NewFilesystemProvider

func NewFilesystemProvider(fs FS, path string) *FilesystemProvider

func (*FilesystemProvider) List

func (p *FilesystemProvider) List(ctx context.Context) ([]Migration, error)

type Migration

type Migration interface {
	Version(ctx context.Context) int
	Up(ctx context.Context) (string, error)
	Down(ctx context.Context) (string, error)
	Snapshot(ctx context.Context) (string, error)
}

Migration represent a single migration that can be executed.

type Migrator

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

func New

func New(prov Provider, db Database) *Migrator

func (*Migrator) CurrentVersion

func (m *Migrator) CurrentVersion(ctx context.Context) (int, error)

CurrentVersion returns latest applied migration version.

func (*Migrator) LatestVersion

func (m *Migrator) LatestVersion(ctx context.Context) (int, error)

LatestVersion returns latest available migration version.

func (*Migrator) Migrate

func (m *Migrator) Migrate(ctx context.Context, version int) error

Migrate executes up or down migrations needed to reach the required version. If version number is higher than the latest available version, then the latest version is used.

func (*Migrator) Plan

func (m *Migrator) Plan(ctx context.Context, version int) ([]Action, error)

Plan returns a list migrations needed to reach the required version. If version number is higher than the latest available version, then the latest version is used.

type Provider

type Provider interface {
	List(ctx context.Context) ([]Migration, error)
}

Provider provides list of available migrations.

type SQLDatabase

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

func NewSQLDatabase

func NewSQLDatabase(sqldb sqldb) *SQLDatabase

func (*SQLDatabase) List

func (s *SQLDatabase) List(ctx context.Context) ([]int, error)

func (*SQLDatabase) Migrate

func (s *SQLDatabase) Migrate(ctx context.Context, actions []Action) error

Jump to

Keyboard shortcuts

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