2020-06-16 15:15:04 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2022-12-22 10:55:37 +03:00
|
|
|
stderrors "errors"
|
2021-06-21 17:34:10 +03:00
|
|
|
"fmt"
|
2023-01-09 07:25:53 +03:00
|
|
|
"io/fs"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2"
|
2022-11-07 16:11:33 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/errors"
|
2021-12-23 18:58:53 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/metadatautil"
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/seed"
|
2022-12-22 10:55:37 +03:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
2020-06-16 15:15:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type SeedApplyOptions struct {
|
2021-04-01 13:38:55 +03:00
|
|
|
EC *cli.ExecutionContext
|
|
|
|
Driver *seed.Driver
|
2020-06-16 15:15:04 +03:00
|
|
|
|
|
|
|
// seed file to apply
|
2021-02-17 15:51:43 +03:00
|
|
|
FileNames []string
|
2021-03-08 14:59:35 +03:00
|
|
|
Source cli.Source
|
2020-06-16 15:15:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSeedApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
|
|
opts := SeedApplyOptions{
|
|
|
|
EC: ec,
|
|
|
|
}
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "apply",
|
|
|
|
Short: "Apply seed data",
|
2023-01-09 07:25:53 +03:00
|
|
|
Long: `Run this command by passing the seed files you want to apply as arguments. If no arguments are passed, all the seed files in the seeds directory will be applied.
|
|
|
|
|
|
|
|
Further reading:
|
|
|
|
- https://hasura.io/docs/latest/migrations-metadata-seeds/manage-seeds/
|
|
|
|
`,
|
2020-06-16 15:15:04 +03:00
|
|
|
Example: ` # Apply all seeds on the database:
|
|
|
|
hasura seed apply
|
|
|
|
|
|
|
|
# Apply only a particular file:
|
2022-01-12 16:24:23 +03:00
|
|
|
hasura seed apply --file 1234_add_some_seed_data.sql --database-name default`,
|
2020-06-16 15:15:04 +03:00
|
|
|
SilenceUsage: false,
|
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
2022-11-07 16:11:33 +03:00
|
|
|
op := genOpName(cmd, "PreRunE")
|
2021-12-23 18:58:53 +03:00
|
|
|
if ec.Config.Version < cli.V3 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-22 10:55:37 +03:00
|
|
|
if err := validateConfigV3FlagsWithAll(cmd, ec); err != nil {
|
2022-11-07 16:11:33 +03:00
|
|
|
return errors.E(op, err)
|
2021-12-23 18:58:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ec.AllDatabases {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validateSourceInfo(ec); err != nil {
|
2022-11-07 16:11:33 +03:00
|
|
|
return errors.E(op, err)
|
2021-12-23 18:58:53 +03:00
|
|
|
}
|
|
|
|
// check if seed ops are supported for the database
|
|
|
|
if !seed.IsSeedsSupported(ec.Source.Kind) {
|
2023-01-09 11:01:49 +03:00
|
|
|
return errors.E(op, fmt.Errorf("seed operations on database '%s' of kind '%s' is not supported", ec.Source.Name, ec.Source.Kind))
|
2021-12-23 18:58:53 +03:00
|
|
|
}
|
|
|
|
return nil
|
2020-06-16 15:15:04 +03:00
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-11-07 16:11:33 +03:00
|
|
|
op := genOpName(cmd, "RunE")
|
2022-12-22 10:55:37 +03:00
|
|
|
opts.Driver = getSeedDriver(ec, ec.Config.Version)
|
2021-12-23 18:58:53 +03:00
|
|
|
if err := opts.Run(); err != nil {
|
2022-11-07 16:11:33 +03:00
|
|
|
return errors.E(op, fmt.Errorf("operation failed \n%w", err))
|
2020-06-16 15:15:04 +03:00
|
|
|
}
|
2022-12-22 10:55:37 +03:00
|
|
|
|
2020-06-16 15:15:04 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-12-22 10:55:37 +03:00
|
|
|
|
2020-06-16 15:15:04 +03:00
|
|
|
cmd.Flags().StringArrayVarP(&opts.FileNames, "file", "f", []string{}, "seed file to apply")
|
2022-12-22 10:55:37 +03:00
|
|
|
cmd.Flags().BoolVar(&opts.EC.AllDatabases, "all-databases", false, "set this flag to attempt to apply seeds on all databases present on server")
|
|
|
|
|
2020-06-16 15:15:04 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *SeedApplyOptions) Run() error {
|
2022-11-07 16:11:33 +03:00
|
|
|
var op errors.Op = "commands.SeedApplyOptions.Run"
|
2021-12-23 18:58:53 +03:00
|
|
|
o.EC.Spin("Applying seeds...")
|
|
|
|
defer o.EC.Spinner.Stop()
|
2022-12-22 10:55:37 +03:00
|
|
|
if o.EC.AllDatabases && o.EC.Config.Version >= cli.V3 {
|
2021-12-23 18:58:53 +03:00
|
|
|
sourcesAndKind, err := metadatautil.GetSourcesAndKind(o.EC.APIClient.V1Metadata.ExportMetadata)
|
|
|
|
if err != nil {
|
2022-11-07 16:11:33 +03:00
|
|
|
return errors.E(op, fmt.Errorf("got error while getting the sources list : %v", err))
|
2021-12-23 18:58:53 +03:00
|
|
|
}
|
|
|
|
for _, source := range sourcesAndKind {
|
|
|
|
o.Source = cli.Source(source)
|
|
|
|
err := o.ApplyOnSource()
|
2022-12-22 10:55:37 +03:00
|
|
|
|
2021-12-23 18:58:53 +03:00
|
|
|
if err != nil {
|
2022-12-22 10:55:37 +03:00
|
|
|
// skip error if no seed files are specified and no seeds are present
|
|
|
|
if len(o.FileNames) > 0 || !stderrors.Is(err, fs.ErrNotExist) {
|
|
|
|
return errors.E(op, fmt.Errorf("error while applying seeds for database '%s': %v", o.Source.Name, err))
|
|
|
|
} else {
|
|
|
|
o.EC.Logger.Infof("No seed data to plant for database: %s", o.Source.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
o.EC.Logger.Infof("Seed data planted for database: %s", o.Source.Name)
|
2021-12-23 18:58:53 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-22 10:55:37 +03:00
|
|
|
} else {
|
|
|
|
o.Source = o.EC.Source
|
|
|
|
if err := o.ApplyOnSource(); err != nil {
|
|
|
|
return errors.E(op, err)
|
|
|
|
}
|
|
|
|
o.EC.Logger.Info("Seeds planted")
|
2022-11-07 16:11:33 +03:00
|
|
|
}
|
|
|
|
return nil
|
2021-12-23 18:58:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *SeedApplyOptions) ApplyOnSource() error {
|
2022-11-07 16:11:33 +03:00
|
|
|
var op errors.Op = "commands.SeedApplyOptions.ApplyOnSource"
|
2020-06-16 15:15:04 +03:00
|
|
|
fs := afero.NewOsFs()
|
2022-11-07 16:11:33 +03:00
|
|
|
if err := o.Driver.ApplySeedsToDatabase(fs, o.EC.SeedsDirectory, o.FileNames, o.Source); err != nil {
|
|
|
|
return errors.E(op, err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-06-16 15:15:04 +03:00
|
|
|
}
|