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

118 lines
2.5 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package database
import (
"sort"
"github.com/hasura/graphql-engine/cli/internal/statestore"
"github.com/hasura/graphql-engine/cli/internal/hasura"
)
2018-06-24 16:40:48 +03:00
// Migrations wraps Migration and has an internal index
// to keep track of Migration order in database.
type Migrations struct {
index migrationVersions
2018-06-24 16:40:48 +03:00
}
func NewMigrations() *Migrations {
return &Migrations{
index: make(migrationVersions, 0),
2018-06-24 16:40:48 +03:00
}
}
func (m *Migrations) Append(migrationVersion MigrationVersion) {
if m.findPos(migrationVersion.Version) > 0 {
2018-06-24 16:40:48 +03:00
return
}
m.index = append(m.index, migrationVersion)
sort.Slice(m.index, func(i, j int) bool {
return m.index[i].Version < m.index[j].Version
})
2018-06-24 16:40:48 +03:00
}
func (m *Migrations) First() (migrationVersion *MigrationVersion, ok bool) {
if len(m.index) == 0 {
return nil, false
2018-06-24 16:40:48 +03:00
}
return &m.index[0], true
2018-06-24 16:40:48 +03:00
}
func (m *Migrations) Last() (*MigrationVersion, bool) {
if len(m.index) == 0 {
return nil, false
2018-06-24 16:40:48 +03:00
}
return &m.index[len(m.index)-1], true
2018-06-24 16:40:48 +03:00
}
func (m *Migrations) Prev(version uint64) (prevVersion *MigrationVersion, ok bool) {
pos := m.findPos(version)
if pos >= 1 && len(m.index) > pos-1 {
return &m.index[pos-1], true
2018-06-24 16:40:48 +03:00
}
return nil, false
2018-06-24 16:40:48 +03:00
}
func (m *Migrations) Next(version uint64) (migrationVersion *MigrationVersion, ok bool) {
pos := m.findPos(version)
if pos >= 0 && len(m.index) > pos+1 {
return &m.index[pos+1], true
2018-06-24 16:40:48 +03:00
}
return nil, false
2018-06-24 16:40:48 +03:00
}
func (m *Migrations) Read(version uint64) (ok bool) {
pos := m.findPos(version)
2018-06-24 16:40:48 +03:00
if pos >= 0 {
return true
}
return false
}
func (m *Migrations) findPos(version uint64) int {
if len(m.index) > 0 {
ix := m.index.Search(version)
if ix < len(m.index) && m.index[ix].Version == version {
2018-06-24 16:40:48 +03:00
return ix
}
}
return -1
}
type MigrationVersion struct {
Version uint64
Dirty bool
}
2018-06-24 16:40:48 +03:00
type migrationVersions []MigrationVersion
func (s migrationVersions) Len() int {
2018-06-24 16:40:48 +03:00
return len(s)
}
func (s migrationVersions) Swap(i, j int) {
2018-06-24 16:40:48 +03:00
s[i], s[j] = s[j], s[i]
}
func (s migrationVersions) Less(i, j int) bool {
return s[i].Version < s[j].Version
}
func (s migrationVersions) Search(x uint64) int {
return sort.Search(len(s), func(i int) bool { return s[i].Version >= x })
2018-06-24 16:40:48 +03:00
}
type HasuraOpts struct {
HasMetadataV3 bool
Database string
Client *hasura.Client
DatabaseOps hasura.DatabaseOperations
MetadataOps hasura.CommonMetadataOperations
V2MetadataOps hasura.V2CommonMetadataOperations
MigrationsStateStore statestore.MigrationsStateStore
SettingsStateStore statestore.SettingsStateStore
2018-06-24 16:40:48 +03:00
}