graphql-engine/cli/commands/seed_apply.go
Aravind K P cc3539415b cli: fix seeds incorrectly being applied to databases
GitOrigin-RevId: cce2612ddfd90d1fd10a0a3561d24c64569a6935
2021-04-01 10:39:53 +00:00

57 lines
1.3 KiB
Go

package commands
import (
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/seed"
)
type SeedApplyOptions struct {
EC *cli.ExecutionContext
Driver *seed.Driver
// seed file to apply
FileNames []string
Source cli.Source
}
func newSeedApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := SeedApplyOptions{
EC: ec,
}
cmd := &cobra.Command{
Use: "apply",
Short: "Apply seed data",
Example: ` # Apply all seeds on the database:
hasura seed apply
# Apply only a particular file:
hasura seed apply --file seeds/1234_add_some_seed_data.sql`,
SilenceUsage: false,
PreRunE: func(cmd *cobra.Command, args []string) error {
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.Driver = getSeedDriver(ec.Config.Version)
opts.EC.Spin("Applying seeds...")
opts.Source = ec.Source
err := opts.Run()
opts.EC.Spinner.Stop()
if err != nil {
return err
}
opts.EC.Logger.Info("Seeds planted")
return nil
},
}
cmd.Flags().StringArrayVarP(&opts.FileNames, "file", "f", []string{}, "seed file to apply")
return cmd
}
func (o *SeedApplyOptions) Run() error {
fs := afero.NewOsFs()
return o.Driver.ApplySeedsToDatabase(fs, o.EC.SeedsDirectory, o.FileNames, o.EC.Source)
}