2021-02-17 07:20:19 +03:00
|
|
|
package statestore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/hasura"
|
2021-02-17 07:20:19 +03:00
|
|
|
)
|
|
|
|
|
2021-08-26 09:09:06 +03:00
|
|
|
type Version struct {
|
|
|
|
Version int64
|
|
|
|
Dirty bool
|
|
|
|
}
|
|
|
|
|
2021-02-17 07:20:19 +03:00
|
|
|
// Abstraction for the storage layer for migration state
|
|
|
|
type MigrationsStateStore interface {
|
2021-02-17 15:51:43 +03:00
|
|
|
InsertVersion(database string, version int64) error
|
|
|
|
RemoveVersion(database string, version int64) error
|
|
|
|
SetVersion(database string, version int64, dirty bool) error
|
|
|
|
GetVersions(database string) (map[uint64]bool, error)
|
2021-08-26 09:09:06 +03:00
|
|
|
SetVersions(database string, versions []Version) error
|
2021-02-17 07:20:19 +03:00
|
|
|
|
2021-05-28 09:04:36 +03:00
|
|
|
PrepareMigrationsStateStore(database string) error
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Abstraction for storage layer of CLI settings
|
|
|
|
type SettingsStateStore interface {
|
|
|
|
GetSetting(name string) (value string, err error)
|
|
|
|
UpdateSetting(name string, value string) error
|
|
|
|
GetAllSettings() (map[string]string, error)
|
|
|
|
PrepareSettingsDriver() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type CLICatalogState struct {
|
|
|
|
client hasura.CatalogStateOperations
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCLICatalogState(client hasura.CatalogStateOperations) *CLICatalogState {
|
|
|
|
return &CLICatalogState{client}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CLICatalogState) Get() (*CLIState, error) {
|
|
|
|
var state struct {
|
|
|
|
CLIState *CLIState `json:"cli_state"`
|
|
|
|
}
|
|
|
|
b, err := c.client.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(b).Decode(&state); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return state.CLIState, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CLICatalogState) Set(state CLIState) (io.Reader, error) {
|
|
|
|
return c.client.Set("cli", state)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// "default:
|
|
|
|
// Version Dirty
|
|
|
|
// --------------------------
|
|
|
|
// "12321312321321321": true
|
|
|
|
type MigrationsState map[string]map[string]bool
|
|
|
|
|
|
|
|
type CLIState struct {
|
2021-05-28 09:04:36 +03:00
|
|
|
Migrations MigrationsState `json:"migrations,omitempty" mapstructure:"migrations,omitempty"`
|
|
|
|
Settings map[string]string `json:"settings" mapstructure:"settings"`
|
2021-05-17 18:19:15 +03:00
|
|
|
// IsStateCopyCompleted is a utility variable
|
|
|
|
// pre config v3 state was stored in users database connected to hasura in `hdb_catalog.*` tables
|
|
|
|
// this variable is set to true when state copy happens from hdb_catalog.* tables
|
|
|
|
// this process is carried out during a scripts update-project-v3 command or an implicit state copy
|
|
|
|
// introduced in https://github.com/hasura/graphql-engine-mono/pull/1298
|
2021-05-28 09:04:36 +03:00
|
|
|
IsStateCopyCompleted bool `json:"isStateCopyCompleted" mapstructure:"isStateCopyCompleted"`
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CLIState) Init() {
|
|
|
|
if c.Migrations == nil {
|
|
|
|
c.Migrations = map[string]map[string]bool{}
|
|
|
|
}
|
|
|
|
if c.Settings == nil {
|
|
|
|
c.Settings = map[string]string{}
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 15:51:43 +03:00
|
|
|
func (c *CLIState) SetMigration(database, key string, value bool) {
|
|
|
|
if c.Migrations[database] == nil {
|
|
|
|
c.Migrations[database] = map[string]bool{}
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
2021-02-17 15:51:43 +03:00
|
|
|
c.Migrations[database][key] = value
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:51:43 +03:00
|
|
|
func (c *CLIState) UnsetMigration(database, key string) {
|
|
|
|
delete(c.Migrations[database], key)
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:51:43 +03:00
|
|
|
func (c *CLIState) GetMigrationsByDatabase(database string) map[string]bool {
|
|
|
|
return c.Migrations[database]
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CLIState) GetMigrations() *MigrationsState {
|
|
|
|
return &c.Migrations
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CLIState) SetSetting(key, value string) {
|
|
|
|
if c.Settings == nil {
|
|
|
|
c.Settings = map[string]string{}
|
|
|
|
}
|
|
|
|
c.Settings[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CLIState) GetSetting(key string) string {
|
|
|
|
v, ok := c.Settings[key]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CLIState) GetSettings() map[string]string {
|
|
|
|
return c.Settings
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:51:43 +03:00
|
|
|
func CopyMigrationState(src, dest MigrationsStateStore, srcdatabase, destdatabase string) error {
|
|
|
|
versions, err := src.GetVersions(srcdatabase)
|
2021-02-17 07:20:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-26 09:09:06 +03:00
|
|
|
var vs []Version
|
|
|
|
for v, dirty := range versions {
|
|
|
|
vs = append(vs, Version{int64(v), dirty})
|
|
|
|
}
|
|
|
|
if err := dest.SetVersions(destdatabase, vs); err != nil {
|
|
|
|
return err
|
2021-02-17 07:20:19 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CopySettingsState(src, dest SettingsStateStore) error {
|
|
|
|
settings, err := src.GetAllSettings()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for k, v := range settings {
|
|
|
|
err := dest.UpdateSetting(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|