2018-10-16 09:25:30 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2021-10-08 19:09:30 +03:00
|
|
|
"fmt"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2022-11-08 16:52:00 +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/projectmetadata"
|
|
|
|
|
2018-10-16 09:25:30 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newMetadataReloadCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2021-09-29 09:14:15 +03:00
|
|
|
opts := &MetadataReloadOptions{
|
2021-05-14 22:09:01 +03:00
|
|
|
EC: ec,
|
2018-10-16 09:25:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
metadataReloadCmd := &cobra.Command{
|
|
|
|
Use: "reload",
|
2023-02-18 09:10:15 +03:00
|
|
|
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.`,
|
2018-10-16 09:25:30 +03:00
|
|
|
Example: ` # Reload all the metadata information from database:
|
2019-12-12 08:16:36 +03:00
|
|
|
hasura metadata reload
|
|
|
|
|
|
|
|
# Use with admin secret:
|
|
|
|
hasura metadata reload --admin-secret "<admin-secret>"
|
|
|
|
|
2023-02-18 09:10:15 +03:00
|
|
|
# Use with a specific endpoint:
|
|
|
|
hasura metadata reload --endpoint "<endpoint>"`,
|
2018-10-16 09:25:30 +03:00
|
|
|
SilenceUsage: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-11-08 16:52:00 +03:00
|
|
|
op := genOpName(cmd, "RunE")
|
|
|
|
if err := opts.runWithInfo(); err != nil {
|
|
|
|
return errors.E(op, err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-16 09:25:30 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadataReloadCmd
|
|
|
|
}
|
|
|
|
|
2021-09-29 09:14:15 +03:00
|
|
|
type MetadataReloadOptions struct {
|
2018-10-16 09:25:30 +03:00
|
|
|
EC *cli.ExecutionContext
|
|
|
|
}
|
|
|
|
|
2021-09-29 09:14:15 +03:00
|
|
|
func (o *MetadataReloadOptions) runWithInfo() error {
|
2022-11-08 16:52:00 +03:00
|
|
|
var op errors.Op = "commands.MetadataReloadOptions.runWithInfo"
|
2021-09-29 09:14:15 +03:00
|
|
|
o.EC.Spin("Reloading metadata...")
|
|
|
|
err := o.run()
|
|
|
|
o.EC.Spinner.Stop()
|
|
|
|
if err != nil {
|
2022-11-08 16:52:00 +03:00
|
|
|
return errors.E(op, fmt.Errorf("failed to reload metadata: %w", err))
|
2021-09-29 09:14:15 +03:00
|
|
|
}
|
|
|
|
o.EC.Logger.Info("Metadata reloaded")
|
2021-10-08 19:09:30 +03:00
|
|
|
icListOpts := &metadataInconsistencyListOptions{
|
|
|
|
EC: o.EC,
|
|
|
|
}
|
|
|
|
err = icListOpts.read(projectmetadata.NewHandlerFromEC(icListOpts.EC))
|
|
|
|
if err != nil {
|
2022-11-08 16:52:00 +03:00
|
|
|
return errors.E(op, fmt.Errorf("failed to read metadata status: %w", err))
|
2021-10-08 19:09:30 +03:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2021-09-29 09:14:15 +03:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-14 22:09:01 +03:00
|
|
|
|
2021-09-29 09:14:15 +03:00
|
|
|
func (o *MetadataReloadOptions) run() error {
|
2022-11-08 16:52:00 +03:00
|
|
|
var op errors.Op = "commands.MetadataReloadOptions.run"
|
2021-05-14 22:09:01 +03:00
|
|
|
var err error
|
2021-09-29 09:14:15 +03:00
|
|
|
metadataHandler := projectmetadata.NewHandlerFromEC(o.EC)
|
2021-06-18 09:24:16 +03:00
|
|
|
_, err = metadataHandler.ReloadMetadata()
|
2018-10-16 09:25:30 +03:00
|
|
|
if err != nil {
|
2022-11-08 16:52:00 +03:00
|
|
|
return errors.E(op, fmt.Errorf("cannot reload metadata: %w", err))
|
2018-10-16 09:25:30 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|