graphql-engine/cli/internal/metadataobject/tables/v3metadata.go
Kali Vara Purushotham Santhati b875d95a75 cli: separate the metadata operations from migrate driver
Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com>
GitOrigin-RevId: 8f6791d5fd51d011623361ac2f5994c8badb50bf
2021-04-01 05:14:17 +00:00

52 lines
1.1 KiB
Go

package tables
import (
"path/filepath"
"github.com/hasura/graphql-engine/cli"
"gopkg.in/yaml.v2"
)
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 {
Name string `yaml:"name"'`
Tables []yaml.MapSlice `yaml:"tables"`
} `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
}