2018-06-24 16:40:48 +03:00
|
|
|
package database
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2021-02-17 07:20:19 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/internal/statestore"
|
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli/internal/hasura"
|
2021-01-18 20:11:05 +03:00
|
|
|
)
|
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 {
|
2021-01-18 20:11:05 +03:00
|
|
|
index migrationVersions
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMigrations() *Migrations {
|
|
|
|
return &Migrations{
|
2021-01-18 20:11:05 +03:00
|
|
|
index: make(migrationVersions, 0),
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
func (m *Migrations) Append(migrationVersion MigrationVersion) {
|
|
|
|
if m.findPos(migrationVersion.Version) > 0 {
|
2018-06-24 16:40:48 +03:00
|
|
|
return
|
|
|
|
}
|
2021-01-18 20:11:05 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +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
|
|
|
}
|
2021-01-18 20:11:05 +03:00
|
|
|
return &m.index[0], true
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
func (m *Migrations) Last() (*MigrationVersion, bool) {
|
|
|
|
if len(m.index) == 0 {
|
|
|
|
return nil, false
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
return &m.index[len(m.index)-1], true
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +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
|
|
|
}
|
2021-01-18 20:11:05 +03:00
|
|
|
return nil, false
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +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
|
|
|
}
|
2021-01-18 20:11:05 +03:00
|
|
|
return nil, false
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +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
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
type MigrationVersion struct {
|
|
|
|
Version uint64
|
|
|
|
Dirty bool
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
type migrationVersions []MigrationVersion
|
|
|
|
|
|
|
|
func (s migrationVersions) Len() int {
|
2018-06-24 16:40:48 +03:00
|
|
|
return len(s)
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
func (s migrationVersions) Swap(i, j int) {
|
2018-06-24 16:40:48 +03:00
|
|
|
s[i], s[j] = s[j], s[i]
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
type HasuraOpts struct {
|
|
|
|
HasMetadataV3 bool
|
2021-02-17 15:51:43 +03:00
|
|
|
Database string
|
2021-02-17 07:20:19 +03:00
|
|
|
|
|
|
|
Client *hasura.Client
|
2021-02-17 15:51:43 +03:00
|
|
|
DatabaseOps hasura.DatabaseOperations
|
2021-02-17 07:20:19 +03:00
|
|
|
MetadataOps hasura.CommonMetadataOperations
|
2021-02-18 08:36:50 +03:00
|
|
|
V2MetadataOps hasura.V2CommonMetadataOperations
|
2021-02-17 07:20:19 +03:00
|
|
|
MigrationsStateStore statestore.MigrationsStateStore
|
|
|
|
SettingsStateStore statestore.SettingsStateStore
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|