2020-06-16 15:15:04 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-04-01 13:38:55 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-01-18 20:11:05 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-06-16 15:15:04 +03:00
|
|
|
|
2021-04-01 13:38:55 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/internal/hasura"
|
|
|
|
|
2020-06-16 15:15:04 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
2021-04-01 08:13:24 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/internal/metadataobject/actions/editor"
|
2020-06-16 15:15:04 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/seed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SeedNewOptions struct {
|
2021-04-01 13:38:55 +03:00
|
|
|
EC *cli.ExecutionContext
|
|
|
|
Driver *seed.Driver
|
2020-06-16 15:15:04 +03:00
|
|
|
|
|
|
|
// filename for the new seed file
|
|
|
|
SeedName string
|
|
|
|
// table name if seed file has to be created from a database table
|
|
|
|
FromTableNames []string
|
|
|
|
|
|
|
|
// seed file that was created
|
2021-02-17 15:51:43 +03:00
|
|
|
FilePath string
|
2021-03-08 14:59:35 +03:00
|
|
|
Source cli.Source
|
2020-06-16 15:15:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSeedCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
|
|
opts := SeedNewOptions{
|
|
|
|
EC: ec,
|
|
|
|
}
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create seed_name",
|
2020-08-18 15:29:58 +03:00
|
|
|
Short: "Create a new seed file",
|
2020-06-16 15:15:04 +03:00
|
|
|
Example: ` # Create a new seed file and use editor to add SQL:
|
|
|
|
hasura seed create new_table_seed
|
|
|
|
|
|
|
|
# Create a new seed by exporting data from tables already present in the database:
|
|
|
|
hasura seed create table1_seed --from-table table1
|
|
|
|
|
|
|
|
# Export data from multiple tables:
|
|
|
|
hasura seed create tables_seed --from-table table1 --from-table table2`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
SilenceUsage: false,
|
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return ec.Validate()
|
|
|
|
},
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.SeedName = args[0]
|
2021-03-08 14:59:35 +03:00
|
|
|
opts.Source = ec.Source
|
2021-04-01 13:38:55 +03:00
|
|
|
opts.Driver = getSeedDriver(ec.Config.Version)
|
2020-06-16 15:15:04 +03:00
|
|
|
err := opts.Run()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ec.Logger.WithField("file", opts.FilePath).Info("created seed file successfully")
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().StringArrayVar(&opts.FromTableNames, "from-table", []string{}, "name of table from which seed file has to be initialized")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *SeedNewOptions) Run() error {
|
2021-03-08 14:59:35 +03:00
|
|
|
databaseDirectory := filepath.Join(o.EC.SeedsDirectory, o.Source.Name)
|
2021-02-17 15:51:43 +03:00
|
|
|
if f, _ := os.Stat(databaseDirectory); f == nil {
|
|
|
|
if err := os.MkdirAll(databaseDirectory, 0755); err != nil {
|
2021-01-18 20:11:05 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-06-16 15:15:04 +03:00
|
|
|
createSeedOpts := seed.CreateSeedOpts{
|
|
|
|
UserProvidedSeedName: o.SeedName,
|
2021-03-08 14:59:35 +03:00
|
|
|
DirectoryPath: filepath.Join(o.EC.SeedsDirectory, o.Source.Name),
|
2020-06-16 15:15:04 +03:00
|
|
|
}
|
|
|
|
// If we are initializing from a database table
|
|
|
|
// create a hasura client and add table name opts
|
|
|
|
if createSeedOpts.Data == nil {
|
2020-08-10 08:58:03 +03:00
|
|
|
var body []byte
|
2020-06-16 15:15:04 +03:00
|
|
|
if len(o.FromTableNames) > 0 {
|
2021-04-01 13:38:55 +03:00
|
|
|
if o.Source.Kind != hasura.SourceKindPG && o.EC.Config.Version >= cli.V3 {
|
|
|
|
return fmt.Errorf("--from-table is supported only for postgres sources")
|
2020-06-16 15:15:04 +03:00
|
|
|
}
|
|
|
|
// Send the query
|
2021-04-01 13:38:55 +03:00
|
|
|
bodyReader, err := o.Driver.ExportDatadump(o.FromTableNames, o.Source.Name)
|
2020-06-16 15:15:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "exporting seed data")
|
|
|
|
}
|
2021-04-01 13:38:55 +03:00
|
|
|
body, err = ioutil.ReadAll(bodyReader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-16 15:15:04 +03:00
|
|
|
} else {
|
|
|
|
const defaultText = ""
|
2020-08-10 08:58:03 +03:00
|
|
|
var err error
|
2021-03-08 11:29:21 +03:00
|
|
|
body, err = editor.CaptureInputFromEditor(editor.GetPreferredEditorFromEnvironment, defaultText, "sql")
|
2020-06-16 15:15:04 +03:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot find default editor from env")
|
|
|
|
}
|
|
|
|
}
|
2020-08-10 08:58:03 +03:00
|
|
|
createSeedOpts.Data = bytes.NewReader(body)
|
2020-06-16 15:15:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fs := afero.NewOsFs()
|
|
|
|
filepath, err := seed.CreateSeedFile(fs, createSeedOpts)
|
|
|
|
if err != nil || filepath == nil {
|
|
|
|
return errors.Wrap(err, "failed to create seed file")
|
|
|
|
}
|
|
|
|
|
|
|
|
o.FilePath = *filepath
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|