这是indexloc提供的服务,不要输入任何密码
Skip to content
3 changes: 3 additions & 0 deletions cli/commands/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
v2yaml "gopkg.in/yaml.v2"
)

// NewMetadataCmd returns the metadata command
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
metadataCmd := &cobra.Command{
Use: "metadata",
Aliases: []string{"md"},
Short: "Manage Hasura GraphQL Engine metadata saved in the database",
SilenceUsage: true,
}
Expand All @@ -24,6 +26,7 @@ func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
newMetadataClearCmd(ec),
newMetadataReloadCmd(ec),
newMetadataApplyCmd(ec),
newMetadataInconsistencyCmd(ec),
)
return metadataCmd
}
Expand Down
22 changes: 22 additions & 0 deletions cli/commands/metadata_inconsistency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package commands

import (
"github.com/hasura/graphql-engine/cli"
"github.com/spf13/cobra"
)

func newMetadataInconsistencyCmd(ec *cli.ExecutionContext) *cobra.Command {
metadataInconsistencyCmd := &cobra.Command{
Use: "inconsistency",
Short: "Manage inconsistent objects in Hasura Metadata",
Aliases: []string{"inconsistencies", "ic"},
SilenceUsage: true,
}

metadataInconsistencyCmd.AddCommand(
newMetadataInconsistencyListCmd(ec),
newMetadataInconsistencyDropCmd(ec),
newMetadataInconsistencyStatusCmd(ec),
)
return metadataInconsistencyCmd
}
60 changes: 60 additions & 0 deletions cli/commands/metadata_inconsistency_drop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package commands

import (
"github.com/hasura/graphql-engine/cli"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func newMetadataInconsistencyDropCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &metadataInconsistencyDropOptions{
EC: ec,
}

metadataInconsistencyDropCmd := &cobra.Command{
Use: "drop",
Short: "Drop inconsistent objects from the metadata",
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("Dropping inconsistent metadata...")
err := opts.run()
opts.EC.Spinner.Stop()
if err != nil {
return errors.Wrap(err, "failed to drop inconsistent metadata")
}
opts.EC.Logger.Info("all inconsistent objects removed from metadata")
return nil
},
}

f := metadataInconsistencyDropCmd.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")

// 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 metadataInconsistencyDropCmd
}

type metadataInconsistencyDropOptions struct {
EC *cli.ExecutionContext
}

func (o *metadataInconsistencyDropOptions) run() error {
d, 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 d.DropInconsistentMetadata()
}
45 changes: 45 additions & 0 deletions cli/commands/metadata_inconsistency_drop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package commands

import (
"net/url"
"os"
"testing"
"time"

"github.com/briandowns/spinner"
"github.com/sirupsen/logrus/hooks/test"

"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/version"
)

func testMetadataInconsistencyDropCmd(t *testing.T, migrationsDir string, metadataFile string, endpoint *url.URL) {
logger, _ := test.NewNullLogger()
opts := &metadataInconsistencyDropOptions{
EC: &cli.ExecutionContext{
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
MetadataFile: []string{metadataFile},
ServerConfig: &cli.ServerConfig{
Endpoint: endpoint.String(),
AdminSecret: os.Getenv("HASURA_GRAPHQL_TEST_ADMIN_SECRET"),
ParsedEndpoint: endpoint,
},
MigrationDir: migrationsDir,
},
}

opts.EC.Version = version.New()
v, err := version.FetchServerVersion(opts.EC.ServerConfig.Endpoint)
if err != nil {
t.Fatalf("getting server version failed: %v", err)
}
opts.EC.Version.SetServerVersion(v)

err = opts.run()
if err != nil {
t.Fatalf("failed dropping the inconsistency: %v", err)
}

os.RemoveAll(opts.EC.MigrationDir)
}
105 changes: 105 additions & 0 deletions cli/commands/metadata_inconsistency_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package commands

import (
"bytes"
"fmt"
"text/tabwriter"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/migrate/database"
"github.com/hasura/graphql-engine/cli/util"
)

func newMetadataInconsistencyListCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &metadataInconsistencyListOptions{
EC: ec,
}

metadataInconsistencyListCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List all inconsistent objects from the metadata",
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
err := opts.run()
opts.EC.Spinner.Stop()
if err != nil {
return errors.Wrap(err, "failed to list inconsistent metadata")
}
if opts.isConsistent {
opts.EC.Logger.Println("metadata is consistent")
}
return nil
},
}

f := metadataInconsistencyListCmd.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")

// 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 metadataInconsistencyListCmd
}

type metadataInconsistencyListOptions struct {
EC *cli.ExecutionContext

isConsistent bool
inconsistentObjects []database.InconsistentMetadataInterface
}

func (o *metadataInconsistencyListOptions) read() error {
d, 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
}
o.isConsistent, o.inconsistentObjects, err = d.GetInconsistentMetadata()
if err != nil {
return err
}
return nil
}

func (o *metadataInconsistencyListOptions) run() error {
o.EC.Spin("Getting inconsistent metadata...")

err := o.read()
if err != nil {
return err
}
if o.isConsistent {
return nil
}
out := new(tabwriter.Writer)
buf := &bytes.Buffer{}
out.Init(buf, 0, 8, 2, ' ', 0)
w := util.NewPrefixWriter(out)
w.Write(util.LEVEL_0, "NAME\tTYPE\tDESCRIPTION\tREASON\n")
for _, obj := range o.inconsistentObjects {
w.Write(util.LEVEL_0, "%s\t%s\t%s\t%s\n",
obj.GetName(),
obj.GetType(),
obj.GetDescription(),
obj.GetReason(),
)
}
out.Flush()
o.EC.Spinner.Stop()
fmt.Println(buf.String())
return nil
}
52 changes: 52 additions & 0 deletions cli/commands/metadata_inconsistency_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package commands

import (
"github.com/hasura/graphql-engine/cli"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func newMetadataInconsistencyStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
opts := &metadataInconsistencyListOptions{
EC: ec,
}

metadataInconsistencyStatusCmd := &cobra.Command{
Use: "status",
Short: "Check if the metadata is inconsistent or not",
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("reading metadata status...")
err := opts.read()
opts.EC.Spinner.Stop()
if err != nil {
return errors.Wrap(err, "failed to read metadata status")
}
if opts.isConsistent {
opts.EC.Logger.Println("metadata is consistent")
} else {
return errors.New("metadata is inconsistent, use list command to see the objects")
}
return nil
},
}

f := metadataInconsistencyStatusCmd.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")

// 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 metadataInconsistencyStatusCmd
}
4 changes: 3 additions & 1 deletion cli/commands/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
sqldriver "database/sql/driver"
"fmt"
"github.com/hasura/graphql-engine/cli/migrate"
"io"
"io/ioutil"
"net/url"
Expand All @@ -14,7 +15,6 @@ import (

"github.com/Masterminds/semver"

"github.com/hasura/graphql-engine/cli/migrate"
mt "github.com/hasura/graphql-engine/cli/migrate/testing"
"github.com/hasura/graphql-engine/cli/version"
_ "github.com/lib/pq"
Expand Down Expand Up @@ -262,6 +262,8 @@ func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir string) {
testMetadataReset(t, metadataFile, endpoint)
testMetadataExport(t, metadataFile, endpoint)
compareMetadata(t, metadataFile, "empty-metadata", versionCtx.ServerSemver)

testMetadataInconsistencyDropCmd(t, migrationsDir, metadataFile, endpoint)
}

func mustWriteFile(t testing.TB, dir, file string, body string) {
Expand Down
Loading