migres

package module
v0.0.0-...-2ff5372 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: MIT Imports: 4 Imported by: 0

README

Migres

This package provides simple migration capabilities for any backend.

System requirements

Basic usage

The key type in this package is Module which allows mapping version strings to Migration interfaces. For example:

import "github.com/annybs/migres"

type MyBackend struct{}

func (mb *MyBackend) Module() migres.Module {
  return migres.Module{
    "1.0.0": migres.Func(mb.upgradeV1, mb.downgradeV1),
    "2.0.0": migres.Func(mb.upgradeV2, mb.downgradeV2),
  }
}

Call Module.Upgrade(from, to) or Module.Downgrade(from, to) in order to execute migrations. The module ensures migrations are all run in the correct order.

License

See LICENSE.md

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMigrationFailed = Error{Message: "migration failed at version %q: %s"}
)

Migration error.

Functions

This section is empty.

Types

type Error

type Error struct {
	Message       string           // Error message template.
	PreviousError error            // Original error encountered during the migration.
	Version       *version.Version // Version at which the migration error occured.
	LastVersion   *version.Version // Version
}

Error reflects an error that occurred during a migration.

func (*Error) Error

func (e *Error) Error() string

Error retrieves the message of a migration error. This does not necessarily include all information about the error, such as the last

func (*Error) Is

func (e *Error) Is(target error) bool

Is determines whether the Error is an instance of the target. https://pkg.go.dev/errors#Is

This implementation does not compare versions.

type FuncMigration

type FuncMigration struct {
	D func() error
	U func() error
}

FuncMigration enables creating a functional migration using callback functions.

import "github.com/annybs/migres"

type MyBackend struct{}

func (mb *MyBackend) Module() migres.Module {
  return migres.Module{
    "1.0.0": migres.Func(mb.upgradeV1, mb.downgradeV1),
    "2.0.0": migres.Func(mb.upgradeV2, mb.downgradeV2),
  }
}

In this example MyModule can be defined with multiple upgrade/downgrade functions. This may be simpler than defining separate migration structs in many cases.

func Func

func Func(up, down func() error) *FuncMigration

Func creates a functional migration.

func (*FuncMigration) Downgrade

func (fm *FuncMigration) Downgrade() error

func (*FuncMigration) Upgrade

func (fm *FuncMigration) Upgrade() error

type Migration

type Migration interface {
	Downgrade() error // Perform a downgrade.
	Upgrade() error   // Perform an upgrade.
}

Migration is anything that can upgrade or downgrade external state - commonly, but not limited to, database schemas.

Each migration SHOULD be able to upgrade or downgrade freely, allowing any changes to be reverted with ease.

Of course, this is not always possible. In the case of irreversible state change, the opposite function should return an error e.g. if an upgrade deletes something irrecoverably, have the corresponding downgrade function throw a descriptive error.

type Module

type Module map[string]Migration

Module provides migrations keyed by version string. This helps to organise migrations execute upgrades or downgrades in the correct version order.

For example:

mod := Module{"1": migration1, "3": migration3, "2": migration2}
mod.Upgrade("1", "3")

Here, the module's three migrations will be sorted into the proper order of 1-2-3, then their upgrades will be performed in that same order.

func (Module) Downgrade

func (mod Module) Downgrade(from, to string) error

Downgrade migrations at versions after from until (and including) to.

func (Module) Upgrade

func (mod Module) Upgrade(from, to string) error

Upgrade migrations at versions after from until (and including) to.

func (Module) Versions

func (mod Module) Versions() (version.List, error)

Versions gets a list of all versions in the module.

Jump to

Keyboard shortcuts

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