2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2018-07-09 16:47:38 +03:00
|
|
|
"io/ioutil"
|
2019-03-20 08:10:06 +03:00
|
|
|
"os"
|
2018-07-09 16:47:38 +03:00
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli"
|
2018-07-09 16:47:38 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
|
|
"github.com/pkg/errors"
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/spf13/cobra"
|
2019-12-14 09:47:38 +03:00
|
|
|
v2yaml "gopkg.in/yaml.v2"
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2018-06-24 16:47:01 +03:00
|
|
|
metadataCmd := &cobra.Command{
|
2018-06-24 16:40:48 +03:00
|
|
|
Use: "metadata",
|
2018-10-20 06:23:20 +03:00
|
|
|
Short: "Manage Hasura GraphQL Engine metadata saved in the database",
|
2018-06-24 16:40:48 +03:00
|
|
|
SilenceUsage: true,
|
|
|
|
}
|
2018-06-24 16:47:01 +03:00
|
|
|
metadataCmd.AddCommand(
|
2019-10-30 16:54:22 +03:00
|
|
|
newMetadataDiffCmd(ec),
|
2018-06-28 11:36:25 +03:00
|
|
|
newMetadataExportCmd(ec),
|
2019-03-29 08:14:56 +03:00
|
|
|
newMetadataClearCmd(ec),
|
2018-10-16 09:25:30 +03:00
|
|
|
newMetadataReloadCmd(ec),
|
2018-06-28 11:36:25 +03:00
|
|
|
newMetadataApplyCmd(ec),
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
2018-06-24 16:47:01 +03:00
|
|
|
return metadataCmd
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
2018-07-09 16:47:38 +03:00
|
|
|
|
2019-03-20 08:10:06 +03:00
|
|
|
func executeMetadata(cmd string, t *migrate.Migrate, ec *cli.ExecutionContext) error {
|
2018-07-09 16:47:38 +03:00
|
|
|
switch cmd {
|
|
|
|
case "export":
|
|
|
|
metaData, err := t.ExportMetadata()
|
|
|
|
if err != nil {
|
2019-03-20 08:10:06 +03:00
|
|
|
return errors.Wrap(err, "cannot export metadata")
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
databyt, err := v2yaml.Marshal(metaData)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-20 08:10:06 +03:00
|
|
|
metadataPath, err := ec.GetMetadataFilePath("yaml")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot save metadata")
|
|
|
|
}
|
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
err = ioutil.WriteFile(metadataPath, databyt, 0644)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot save metadata")
|
|
|
|
}
|
2019-03-29 08:14:56 +03:00
|
|
|
case "clear":
|
2018-07-09 16:47:38 +03:00
|
|
|
err := t.ResetMetadata()
|
|
|
|
if err != nil {
|
2019-03-29 08:14:56 +03:00
|
|
|
return errors.Wrap(err, "cannot clear Metadata")
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
2018-10-16 09:25:30 +03:00
|
|
|
case "reload":
|
|
|
|
err := t.ReloadMetadata()
|
|
|
|
if err != nil {
|
2019-03-20 08:10:06 +03:00
|
|
|
return errors.Wrap(err, "cannot reload Metadata")
|
2018-10-16 09:25:30 +03:00
|
|
|
}
|
2018-07-09 16:47:38 +03:00
|
|
|
case "apply":
|
2019-03-20 08:10:06 +03:00
|
|
|
var data interface{}
|
|
|
|
var metadataContent []byte
|
|
|
|
for _, format := range []string{"yaml", "json"} {
|
|
|
|
metadataPath, err := ec.GetMetadataFilePath(format)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot apply metadata")
|
|
|
|
}
|
|
|
|
|
|
|
|
metadataContent, err = ioutil.ReadFile(metadataPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if metadataContent == nil {
|
|
|
|
return errors.New("Unable to locate metadata.[yaml|json] file under migrations directory")
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
|
|
|
|
2019-03-20 08:10:06 +03:00
|
|
|
err := yaml.Unmarshal(metadataContent, &data)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot parse metadata file")
|
|
|
|
}
|
|
|
|
|
2019-03-20 08:10:06 +03:00
|
|
|
err = t.ApplyMetadata(data)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot apply metadata on the database")
|
|
|
|
}
|
2019-03-20 08:10:06 +03:00
|
|
|
return nil
|
2018-07-09 16:47:38 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|