Files
version/migration.go
T

91 lines
1.5 KiB
Go
Raw Normal View History

2026-06-24 16:51:49 +01:00
package version
import (
"sort"
)
// Migration maps provide a simple way to store and run versioned 'patch' functions.
type Migration map[string]Patch
2026-06-24 16:51:49 +01:00
// Down reverts all patches in reverse version order.
func (m Migration) Down(after func(*Version)) error {
versions, err := m.Versions()
if err != nil {
return err
}
for i := len(versions); i > 0; i-- {
v := versions[i-1]
patch := m[v.Text]
if err := patch.Down(); err != nil {
return err
2026-06-24 16:51:49 +01:00
}
if after != nil {
after(v)
}
2026-06-24 16:51:49 +01:00
}
return nil
2026-06-24 16:51:49 +01:00
}
// Match tests versions against a constraint and returns a new migration map of matching versions only.
func (m Migration) Match(constraints ...*Constraint) (Migration, error) {
l, err := m.Versions()
2026-06-24 16:51:49 +01:00
if err != nil {
return nil, err
}
l = l.Match(constraints...)
m2 := Migration{}
for _, v := range l {
m2[v.Text] = m[v.Text]
2026-06-24 16:51:49 +01:00
}
return m2, nil
2026-06-24 16:51:49 +01:00
}
// Up executes all patches in version order.
func (m Migration) Up(after func(*Version)) error {
versions, err := m.Versions()
2026-06-24 16:51:49 +01:00
if err != nil {
return err
}
for _, v := range versions {
patch := m[v.Text]
if err := patch.Up(); err != nil {
2026-06-24 16:51:49 +01:00
return err
}
if after != nil {
after(v)
2026-06-24 16:51:49 +01:00
}
}
return nil
}
// Versions returns a List of all versions in the migration map.
func (m Migration) Versions() (List, error) {
l := List{}
for str := range m {
v, err := Parse(str)
if err != nil {
return nil, err
}
l = append(l, v)
}
sort.Stable(l)
return l, nil
}
type Patch struct {
Down func() error
Up func() error
}