2018-06-24 16:40:48 +03:00
package commands
import (
2022-11-08 17:24:34 +03:00
"fmt"
2021-06-16 14:44:15 +03:00
"github.com/hasura/graphql-engine/cli/v2"
2022-11-08 17:24:34 +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-06-24 16:40:48 +03:00
"github.com/spf13/cobra"
)
2019-03-29 08:14:56 +03:00
func newMetadataClearCmd ( ec * cli . ExecutionContext ) * cobra . Command {
2020-02-24 19:14:46 +03:00
opts := & MetadataClearOptions {
2021-05-14 22:09:01 +03:00
EC : ec ,
2018-06-24 16:40:48 +03:00
}
metadataResetCmd := & cobra . Command {
2019-03-29 08:14:56 +03:00
Use : "clear" ,
Aliases : [ ] string { "reset" } ,
2022-12-30 06:50:48 +03:00
Short : "Clear Hasura GraphQL Engine Metadata on the database" ,
Long : "This command allows you to clear the Hasura GraphQL Engine Metadata. Passing in the `--endpoint` flag will clear the Hasura Metadata on the HGE instance specified by the endpoint." ,
2019-03-29 08:14:56 +03:00
Example : ` # Clear all the metadata information from database :
2019-12-12 08:16:36 +03:00
hasura metadata clear
# Use with admin secret :
hasura metadata clear -- admin - secret "<admin-secret>"
# Clear metadata on a different Hasura instance :
hasura metadata clear -- endpoint "<endpoint>" ` ,
2018-06-24 16:40:48 +03:00
SilenceUsage : true ,
RunE : func ( cmd * cobra . Command , args [ ] string ) error {
2022-11-08 17:24:34 +03:00
op := genOpName ( cmd , "RunE" )
2019-03-29 08:14:56 +03:00
if cmd . CalledAs ( ) == "reset" {
opts . EC . Logger . Warn ( "metadata reset command is deprecated, use metadata clear instead" )
}
2019-04-03 08:52:03 +03:00
opts . EC . Spin ( "Clearing metadata..." )
2020-02-24 19:14:46 +03:00
err := opts . Run ( )
2019-04-03 08:52:03 +03:00
opts . EC . Spinner . Stop ( )
if err != nil {
2022-11-08 17:24:34 +03:00
return errors . E ( op , fmt . Errorf ( "failed to clear metadata: %w" , err ) )
2019-04-03 08:52:03 +03:00
}
opts . EC . Logger . Info ( "Metadata cleared" )
return nil
2018-06-24 16:40:48 +03:00
} ,
}
return metadataResetCmd
}
2020-02-24 19:14:46 +03:00
type MetadataClearOptions struct {
2018-06-24 16:40:48 +03:00
EC * cli . ExecutionContext
}
2020-02-24 19:14:46 +03:00
func ( o * MetadataClearOptions ) Run ( ) error {
2022-11-08 17:24:34 +03:00
var op errors . Op = "commands.MetadataClearOptions.Run"
2021-05-14 22:09:01 +03:00
var err error
2021-07-23 12:49:44 +03:00
metadataHandler := projectmetadata . NewHandlerFromEC ( o . EC )
2021-05-14 22:09:01 +03:00
err = metadataHandler . ResetMetadata ( )
2018-06-24 16:40:48 +03:00
if err != nil {
2022-11-08 17:24:34 +03:00
return errors . E ( op , fmt . Errorf ( "cannot clear Metadata: %w" , err ) )
2018-06-24 16:40:48 +03:00
}
return nil
}