mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
2ee7f7d76e
### Description Adds a `metadata diff` command to show comparisons between two different sets of Hasura metadata. ``` # Show changes between server metadata and the exported metadata file: hasura metadata diff # Show changes between server metadata and that in local_metadata.yaml: hasura metadata diff local_metadata.yaml # Show changes between metadata from metadata.yaml and metadata_old.yaml: hasura metadata diff metadata.yaml metadata_old.yaml ``` Also adds a `--dry-run` flag to `metadata apply` command which will print the diff and exit rather than actually applying the metadata. ### Affected components - CLI - Docs ### Related Issues Close #3126, Close #3127 ### Solution and Design - Added `metadata_diff.go` and `metadata_diff_test.go` ### Steps to test and verify ``` hasura metadata export # Make changes to migrations/metadata.yaml hasura metadata diff ``` ### Limitations, known bugs & workarounds This is just a general-purpose diff. A more contextual diff with the understanding of metadata can be added once #3072 is merged.
104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package commands
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
|
|
"github.com/ghodss/yaml"
|
|
"github.com/hasura/graphql-engine/cli"
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
metadataCmd := &cobra.Command{
|
|
Use: "metadata",
|
|
Short: "Manage Hasura GraphQL Engine metadata saved in the database",
|
|
SilenceUsage: true,
|
|
}
|
|
metadataCmd.AddCommand(
|
|
newMetadataDiffCmd(ec),
|
|
newMetadataExportCmd(ec),
|
|
newMetadataClearCmd(ec),
|
|
newMetadataReloadCmd(ec),
|
|
newMetadataApplyCmd(ec),
|
|
)
|
|
return metadataCmd
|
|
}
|
|
|
|
func executeMetadata(cmd string, t *migrate.Migrate, ec *cli.ExecutionContext) error {
|
|
switch cmd {
|
|
case "export":
|
|
metaData, err := t.ExportMetadata()
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot export metadata")
|
|
}
|
|
|
|
t, err := json.Marshal(metaData)
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot Marshal metadata")
|
|
}
|
|
|
|
data, err := yaml.JSONToYAML(t)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
metadataPath, err := ec.GetMetadataFilePath("yaml")
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot save metadata")
|
|
}
|
|
|
|
err = ioutil.WriteFile(metadataPath, data, 0644)
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot save metadata")
|
|
}
|
|
case "clear":
|
|
err := t.ResetMetadata()
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot clear Metadata")
|
|
}
|
|
case "reload":
|
|
err := t.ReloadMetadata()
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot reload Metadata")
|
|
}
|
|
case "apply":
|
|
var data interface{}
|
|
var metadataContent []byte
|
|
for _, format := range []string{"yaml", "json"} {
|
|
metadataPath, err := ec.GetMetadataFilePath(format)
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot apply metadata")
|
|
}
|
|
|
|
metadataContent, err = ioutil.ReadFile(metadataPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
break
|
|
}
|
|
|
|
if metadataContent == nil {
|
|
return errors.New("Unable to locate metadata.[yaml|json] file under migrations directory")
|
|
}
|
|
|
|
err := yaml.Unmarshal(metadataContent, &data)
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot parse metadata file")
|
|
}
|
|
|
|
err = t.ApplyMetadata(data)
|
|
if err != nil {
|
|
return errors.Wrap(err, "cannot apply metadata on the database")
|
|
}
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|