2021-02-15 18:14:00 +03:00
|
|
|
package tables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2021-04-21 13:09:09 +03:00
|
|
|
/*
|
|
|
|
V3MetadataTableConfig is responsible for exporting and applying "tables" metadata objects
|
|
|
|
in config v2 format on a server with v3 metadata
|
|
|
|
*/
|
2021-02-15 18:14:00 +03:00
|
|
|
type V3MetadataTableConfig struct {
|
|
|
|
*TableConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewV3MetadataTableConfig(ec *cli.ExecutionContext, baseDir string) *V3MetadataTableConfig {
|
|
|
|
return &V3MetadataTableConfig{
|
|
|
|
&TableConfig{
|
|
|
|
MetadataDir: baseDir,
|
|
|
|
logger: ec.Logger,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (t *V3MetadataTableConfig) Export(md yaml.MapSlice) (map[string][]byte, error) {
|
|
|
|
metadataBytes, err := yaml.Marshal(md)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var metadata struct {
|
|
|
|
Sources []struct {
|
2021-02-17 07:20:19 +03:00
|
|
|
Name string `yaml:"name"'`
|
|
|
|
Tables []yaml.MapSlice `yaml:"tables"`
|
2021-02-15 18:14:00 +03:00
|
|
|
} `yaml:"sources"`
|
|
|
|
}
|
|
|
|
var tables interface{}
|
|
|
|
if err := yaml.Unmarshal(metadataBytes, &metadata); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(metadata.Sources) > 0 {
|
|
|
|
tables = metadata.Sources[0].Tables
|
|
|
|
}
|
|
|
|
if tables == nil {
|
|
|
|
tables = make([]interface{}, 0)
|
|
|
|
}
|
|
|
|
data, err := yaml.Marshal(tables)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return map[string][]byte{
|
|
|
|
filepath.Join(t.MetadataDir, MetadataFilename): data,
|
|
|
|
}, nil
|
|
|
|
}
|