mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-16 09:51:59 +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.
84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package commands
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func newMetadataApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
v := viper.New()
|
|
opts := &metadataApplyOptions{
|
|
EC: ec,
|
|
actionType: "apply",
|
|
}
|
|
|
|
metadataApplyCmd := &cobra.Command{
|
|
Use: "apply",
|
|
Short: "Apply Hasura metadata on a database",
|
|
Example: ` # Apply Hasura GraphQL Engine metadata present in metadata.[yaml|json] file:
|
|
hasura metadata apply`,
|
|
SilenceUsage: true,
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
ec.Viper = v
|
|
return ec.Validate()
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if opts.dryRun {
|
|
o := &metadataDiffOptions{
|
|
EC: ec,
|
|
output: os.Stdout,
|
|
}
|
|
filename, err := ec.GetExistingMetadataFile()
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed getting metadata file")
|
|
}
|
|
o.metadata[0] = filename
|
|
return o.run()
|
|
}
|
|
opts.EC.Spin("Applying metadata...")
|
|
err := opts.run()
|
|
opts.EC.Spinner.Stop()
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to apply metadata")
|
|
}
|
|
opts.EC.Logger.Info("Metadata applied")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
f := metadataApplyCmd.Flags()
|
|
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
|
|
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
|
|
f.String("access-key", "", "access key for Hasura GraphQL Engine")
|
|
f.MarkDeprecated("access-key", "use --admin-secret instead")
|
|
|
|
f.BoolVar(&opts.dryRun, "dry-run", false, "show a diff instead of applying the metadata")
|
|
|
|
// need to create a new viper because https://github.com/spf13/viper/issues/233
|
|
v.BindPFlag("endpoint", f.Lookup("endpoint"))
|
|
v.BindPFlag("admin_secret", f.Lookup("admin-secret"))
|
|
v.BindPFlag("access_key", f.Lookup("access-key"))
|
|
|
|
return metadataApplyCmd
|
|
}
|
|
|
|
type metadataApplyOptions struct {
|
|
EC *cli.ExecutionContext
|
|
|
|
actionType string
|
|
|
|
dryRun bool
|
|
}
|
|
|
|
func (o *metadataApplyOptions) run() error {
|
|
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.ServerConfig.ParsedEndpoint, o.EC.ServerConfig.AdminSecret, o.EC.Logger, o.EC.Version, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return executeMetadata(o.actionType, migrateDrv, o.EC)
|
|
}
|