2021-02-15 18:14:00 +03:00
|
|
|
package tables
|
|
|
|
|
|
|
|
import (
|
2022-03-10 11:12:55 +03:00
|
|
|
"bytes"
|
2021-02-15 18:14:00 +03:00
|
|
|
"path/filepath"
|
|
|
|
|
2023-01-16 12:40:58 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
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
|
|
|
|
2022-03-10 11:12:55 +03:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-02-15 18:14:00 +03:00
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 15:51:29 +03:00
|
|
|
func (t *V3MetadataTableConfig) Export(md map[string]yaml.Node) (map[string][]byte, error) {
|
2023-01-16 12:40:58 +03:00
|
|
|
var op errors.Op = "tables.V3MetadataTableConfig.Export"
|
2021-02-15 18:14:00 +03:00
|
|
|
metadataBytes, err := yaml.Marshal(md)
|
|
|
|
if err != nil {
|
2023-01-16 12:40:58 +03:00
|
|
|
return nil, errors.E(op, t.error(err))
|
2021-02-15 18:14:00 +03:00
|
|
|
}
|
|
|
|
var metadata struct {
|
|
|
|
Sources []struct {
|
2022-03-10 11:12:55 +03:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Tables []yaml.Node `yaml:"tables"`
|
2021-02-15 18:14:00 +03:00
|
|
|
} `yaml:"sources"`
|
|
|
|
}
|
|
|
|
var tables interface{}
|
|
|
|
if err := yaml.Unmarshal(metadataBytes, &metadata); err != nil {
|
2023-01-16 12:40:58 +03:00
|
|
|
return nil, errors.E(op, 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)
|
|
|
|
}
|
2022-03-10 11:12:55 +03:00
|
|
|
var buf bytes.Buffer
|
|
|
|
err = metadataobject.GetEncoder(&buf).Encode(tables)
|
2021-02-15 18:14:00 +03:00
|
|
|
if err != nil {
|
2023-01-16 12:40:58 +03:00
|
|
|
return nil, errors.E(op, t.error(err))
|
2021-02-15 18:14:00 +03:00
|
|
|
}
|
|
|
|
return map[string][]byte{
|
2022-03-10 11:12:55 +03:00
|
|
|
filepath.ToSlash(filepath.Join(t.MetadataDir, t.Filename())): buf.Bytes(),
|
2021-02-15 18:14:00 +03:00
|
|
|
}, nil
|
|
|
|
}
|