graphql-engine/cli/migrate/source/migration.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

205 lines
4.4 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package source
import (
"fmt"
2018-06-24 16:40:48 +03:00
"sort"
)
// Direction is either up or down.
type Direction string
const (
Down Direction = "down"
Up Direction = "up"
MetaDown Direction = "metadown"
MetaUp Direction = "metaup"
)
// Migration is a helper struct for source drivers that need to
// build the full directory tree in memory.
// Migration is fully independent from migrate.Migration.
type Migration struct {
// Version is the version of this migration.
Version uint64
// Identifier can be any string that helps identifying
// this migration in the source.
Identifier string
// Direction is either Up or Down.
Direction Direction
// Raw holds the raw location path to this migration in source.
// ReadUp and ReadDown will use this.
Raw string
// Check if the file exists in a directory
IsDir bool
2018-06-24 16:40:48 +03:00
}
// Migrations wraps Migration and has an internal index
// to keep track of Migration order.
type Migrations struct {
Index uint64Slice
Migrations map[uint64]map[Direction]*Migration
2018-06-24 16:40:48 +03:00
}
func NewMigrations() *Migrations {
return &Migrations{
Index: make(uint64Slice, 0),
Migrations: make(map[uint64]map[Direction]*Migration),
2018-06-24 16:40:48 +03:00
}
}
func (i *Migrations) Append(m *Migration) (err error) {
2018-06-24 16:40:48 +03:00
if m == nil {
return fmt.Errorf("migration cannot be nill")
2018-06-24 16:40:48 +03:00
}
if i.Migrations[m.Version] == nil {
i.Migrations[m.Version] = make(map[Direction]*Migration)
2018-06-24 16:40:48 +03:00
}
// reject duplicate versions
if migration, dup := i.Migrations[m.Version][m.Direction]; dup {
return fmt.Errorf("found duplicate migrations for version %d\n- %s\n- %s", m.Version, m.Raw, migration.Raw)
2018-06-24 16:40:48 +03:00
}
i.Migrations[m.Version][m.Direction] = m
2018-06-24 16:40:48 +03:00
i.buildIndex()
return nil
2018-06-24 16:40:48 +03:00
}
func (i *Migrations) buildIndex() {
i.Index = make(uint64Slice, 0)
for version := range i.Migrations {
i.Index = append(i.Index, version)
2018-06-24 16:40:48 +03:00
}
sort.Sort(i.Index)
2018-06-24 16:40:48 +03:00
}
func (i *Migrations) First() (version uint64, ok bool) {
if len(i.Index) == 0 {
2018-06-24 16:40:48 +03:00
return 0, false
}
return i.Index[0], true
2018-06-24 16:40:48 +03:00
}
func (i *Migrations) GetLocalVersion() uint64 {
if len(i.Index) == 0 {
2018-06-24 16:40:48 +03:00
return 0
}
return i.Index[len(i.Index)-1]
2018-06-24 16:40:48 +03:00
}
func (i *Migrations) GetUnappliedMigrations(version uint64) (versions []uint64) {
if version == 0 {
return i.Index[0:]
2018-06-24 16:40:48 +03:00
}
pos := i.findPos(version)
if pos >= 0 && len(i.Index) > pos+1 {
return i.Index[pos+1:]
2018-06-24 16:40:48 +03:00
}
return versions
}
func (i *Migrations) Prev(version uint64) (prevVersion uint64, ok bool) {
pos := i.findPos(version)
if pos >= 1 && len(i.Index) > pos-1 {
return i.Index[pos-1], true
2018-06-24 16:40:48 +03:00
}
return 0, false
}
func (i *Migrations) Next(version uint64) (nextVersion uint64, ok bool) {
pos := i.findPos(version)
if pos >= 0 && len(i.Index) > pos+1 {
return i.Index[pos+1], true
2018-06-24 16:40:48 +03:00
}
return 0, false
}
func (i *Migrations) GetDirections(version uint64) map[Direction]bool {
var directions = map[Direction]bool{
2018-06-24 16:40:48 +03:00
Down: false,
Up: false,
MetaDown: false,
MetaUp: false,
}
for k := range i.Migrations[version] {
2018-06-24 16:40:48 +03:00
directions[k] = true
}
return directions
}
func (i *Migrations) Up(version uint64) (m *Migration, ok bool) {
if _, ok := i.Migrations[version]; ok {
if mx, ok := i.Migrations[version][Up]; ok {
2018-06-24 16:40:48 +03:00
return mx, true
}
}
return nil, false
}
func (i *Migrations) MetaUp(version uint64) (m *Migration, ok bool) {
if _, ok := i.Migrations[version]; ok {
if mx, ok := i.Migrations[version][MetaUp]; ok {
2018-06-24 16:40:48 +03:00
return mx, true
}
}
return nil, false
}
func (i *Migrations) Down(version uint64) (m *Migration, ok bool) {
if _, ok := i.Migrations[version]; ok {
if mx, ok := i.Migrations[version][Down]; ok {
2018-06-24 16:40:48 +03:00
return mx, true
}
}
return nil, false
}
func (i *Migrations) MetaDown(version uint64) (m *Migration, ok bool) {
if _, ok := i.Migrations[version]; ok {
if mx, ok := i.Migrations[version][MetaDown]; ok {
2018-06-24 16:40:48 +03:00
return mx, true
}
}
return nil, false
}
func (i *Migrations) ReadName(version uint64) (name string) {
for k := range i.Migrations[version] {
return i.Migrations[version][k].Identifier
}
return "-"
}
2018-06-24 16:40:48 +03:00
func (i *Migrations) findPos(version uint64) int {
if len(i.Index) > 0 {
ix := i.Index.Search(version)
if ix < len(i.Index) && i.Index[ix] == version {
2018-06-24 16:40:48 +03:00
return ix
}
}
return -1
}
type uint64Slice []uint64
func (s uint64Slice) Len() int {
return len(s)
}
func (s uint64Slice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s uint64Slice) Less(i, j int) bool {
return s[i] < s[j]
}
func (s uint64Slice) Search(x uint64) int {
return sort.Search(len(s), func(i int) bool { return s[i] >= x })
}