2021-02-15 18:14:00 +03:00
|
|
|
package tables
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-07-23 12:49:44 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadataobject"
|
2021-06-18 20:38:29 +03:00
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2021-02-15 18:14:00 +03:00
|
|
|
|
|
|
|
"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-05-17 18:19:15 +03:00
|
|
|
*/
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-07-23 12:49:44 +03:00
|
|
|
func (t *V3MetadataTableConfig) Export(md yaml.MapSlice) (map[string][]byte, metadataobject.ErrParsingMetadataObject) {
|
2021-02-15 18:14:00 +03:00
|
|
|
metadataBytes, err := yaml.Marshal(md)
|
|
|
|
if err != nil {
|
2021-07-23 12:49:44 +03:00
|
|
|
return nil, t.error(err)
|
2021-02-15 18:14:00 +03:00
|
|
|
}
|
|
|
|
var metadata struct {
|
|
|
|
Sources []struct {
|
2021-10-13 17:38:07 +03:00
|
|
|
Name string `yaml:"name"`
|
2021-02-17 07:20:19 +03:00
|
|
|
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 {
|
2021-07-23 12:49:44 +03:00
|
|
|
return nil, t.error(err)
|
2021-02-15 18:14:00 +03:00
|
|
|
}
|
|
|
|
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 {
|
2021-07-23 12:49:44 +03:00
|
|
|
return nil, t.error(err)
|
2021-02-15 18:14:00 +03:00
|
|
|
}
|
|
|
|
return map[string][]byte{
|
2021-07-23 12:49:44 +03:00
|
|
|
filepath.ToSlash(filepath.Join(t.MetadataDir, t.Filename())): data,
|
2021-02-15 18:14:00 +03:00
|
|
|
}, nil
|
|
|
|
}
|