/
metadata_reload.go
82 lines (71 loc) · 2.44 KB
/
metadata_reload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package commands
import (
"fmt"
"github.com/hasura/graphql-engine/cli/v2"
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
"github.com/hasura/graphql-engine/cli/v2/internal/projectmetadata"
"github.com/spf13/cobra"
)
func newMetadataReloadCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &MetadataReloadOptions{
EC: ec,
}
metadataReloadCmd := &cobra.Command{
Use: "reload",
Short: "Reload Hasura GraphQL Engine schema to pick up changes in any underlying data sources (database or remote schema)",
Long: `hasura metadata reload should be used when there is a change in the underlying data sources (database or remote schema) that Hasura should be aware of.
Example:
A new column is added to a table and this column should now be added to the GraphQL schema.`,
Example: ` # Reload all the metadata information from database:
hasura metadata reload
# Use with admin secret:
hasura metadata reload --admin-secret "<admin-secret>"
# Use with a specific endpoint:
hasura metadata reload --endpoint "<endpoint>"`,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
op := genOpName(cmd, "RunE")
if err := opts.runWithInfo(); err != nil {
return errors.E(op, err)
}
return nil
},
}
return metadataReloadCmd
}
type MetadataReloadOptions struct {
EC *cli.ExecutionContext
}
func (o *MetadataReloadOptions) runWithInfo() error {
var op errors.Op = "commands.MetadataReloadOptions.runWithInfo"
o.EC.Spin("Reloading metadata...")
err := o.run()
o.EC.Spinner.Stop()
if err != nil {
return errors.E(op, fmt.Errorf("failed to reload metadata: %w", err))
}
o.EC.Logger.Info("Metadata reloaded")
icListOpts := &metadataInconsistencyListOptions{
EC: o.EC,
}
err = icListOpts.read(projectmetadata.NewHandlerFromEC(icListOpts.EC))
if err != nil {
return errors.E(op, fmt.Errorf("failed to read metadata status: %w", err))
}
if icListOpts.isConsistent {
icListOpts.EC.Logger.Infoln("Metadata is consistent")
} else {
icListOpts.EC.Logger.Warnln("Metadata is inconsistent, use 'hasura metadata ic list' command to see the inconsistent objects")
}
return nil
}
func (o *MetadataReloadOptions) run() error {
var op errors.Op = "commands.MetadataReloadOptions.run"
var err error
metadataHandler := projectmetadata.NewHandlerFromEC(o.EC)
_, err = metadataHandler.ReloadMetadata()
if err != nil {
return errors.E(op, fmt.Errorf("cannot reload metadata: %w", err))
}
return nil
}