Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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.
type FuncMigration ¶
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 (*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 ¶
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.