mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
a398d3b190
GITHUB_PR_NUMBER: 6111 GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/6111 Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: 1f6517acfacb58c566bb5e48f74ea0dfa5c6f063
93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package migrate
|
|
|
|
import (
|
|
"sort"
|
|
)
|
|
|
|
type MigrationStatus struct {
|
|
// Version is the version of this migration.
|
|
Version uint64 `json:"-"`
|
|
|
|
// Name helps finding this migration in the source folder
|
|
Name string `json:"-"`
|
|
|
|
// Check if the migration is applied on the cluster
|
|
IsApplied bool `json:"database_status"`
|
|
|
|
// Check if the migration is present on the local.
|
|
IsPresent bool `json:"source_status"`
|
|
|
|
IsDirty bool `json:"-"`
|
|
}
|
|
|
|
type Status struct {
|
|
Index uint64Slice `json:"migrations"`
|
|
Migrations map[uint64]*MigrationStatus `json:"status"`
|
|
}
|
|
|
|
func NewStatus() *Status {
|
|
return &Status{
|
|
Index: make(uint64Slice, 0),
|
|
Migrations: make(map[uint64]*MigrationStatus),
|
|
}
|
|
}
|
|
|
|
func (i *Status) Append(m *MigrationStatus) (ok bool) {
|
|
if m == nil {
|
|
return false
|
|
}
|
|
|
|
if i.Migrations[m.Version] == nil {
|
|
i.Migrations[m.Version] = m
|
|
} else {
|
|
// If the Version already exists
|
|
i.Migrations[m.Version].IsApplied = m.IsApplied
|
|
}
|
|
|
|
i.buildIndex()
|
|
return true
|
|
}
|
|
|
|
func (i *Status) buildIndex() {
|
|
i.Index = make(uint64Slice, 0)
|
|
for version := range i.Migrations {
|
|
i.Index = append(i.Index, version)
|
|
}
|
|
sort.Sort(i.Index)
|
|
}
|
|
|
|
func (i *Status) Read(version uint64) (m *MigrationStatus, ok bool) {
|
|
if mx, ok := i.Migrations[version]; ok {
|
|
return mx, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func (i *Status) findPos(version uint64) int {
|
|
if len(i.Index) > 0 {
|
|
ix := i.Index.Search(version)
|
|
if ix < len(i.Index) && i.Index[ix] == version {
|
|
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 })
|
|
}
|