forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31ce55e
commit 75d8b80
Showing
25 changed files
with
813 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/hasura/graphql-engine/cli" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// NewSeedCmd will return the seed command | ||
func NewSeedCmd(ec *cli.ExecutionContext) *cobra.Command { | ||
v := viper.New() | ||
ec.Viper = v | ||
seedCmd := &cobra.Command{ | ||
Use: "seeds", | ||
Aliases: []string{"sd"}, | ||
Short: "Manage seed data", | ||
SilenceUsage: true, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
err := ec.Prepare() | ||
if err != nil { | ||
return err | ||
} | ||
return ec.Validate() | ||
}, | ||
} | ||
|
||
seedCmd.AddCommand( | ||
newSeedCreateCmd(ec), | ||
newSeedApplyCmd(ec), | ||
) | ||
seedCmd.PersistentFlags().String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine") | ||
seedCmd.PersistentFlags().String("admin-secret", "", "admin secret for Hasura GraphQL Engine") | ||
seedCmd.PersistentFlags().String("access-key", "", "access key for Hasura GraphQL Engine") | ||
seedCmd.PersistentFlags().MarkDeprecated("access-key", "use --admin-secret instead") | ||
|
||
v.BindPFlag("endpoint", seedCmd.PersistentFlags().Lookup("endpoint")) | ||
v.BindPFlag("admin_secret", seedCmd.PersistentFlags().Lookup("admin-secret")) | ||
v.BindPFlag("access_key", seedCmd.PersistentFlags().Lookup("access-key")) | ||
|
||
return seedCmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/hasura/graphql-engine/cli/migrate" | ||
"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 | ||
|
||
// seed file to apply | ||
FileNames []string | ||
} | ||
|
||
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.EC.Spin("Applying seeds...") | ||
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 { | ||
migrateDriver, err := migrate.NewMigrate(o.EC, true) | ||
if err != nil { | ||
return err | ||
} | ||
fs := afero.NewOsFs() | ||
return seed.ApplySeedsToDatabase(o.EC, fs, migrateDriver, o.FileNames) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package commands | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/hasura/graphql-engine/cli/migrate" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/hasura/graphql-engine/cli" | ||
"github.com/hasura/graphql-engine/cli/metadata/actions/editor" | ||
"github.com/hasura/graphql-engine/cli/seed" | ||
) | ||
|
||
type SeedNewOptions struct { | ||
EC *cli.ExecutionContext | ||
|
||
// 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 | ||
FilePath string | ||
} | ||
|
||
func newSeedCreateCmd(ec *cli.ExecutionContext) *cobra.Command { | ||
opts := SeedNewOptions{ | ||
EC: ec, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "create seed_name", | ||
Short: "create a new seed file", | ||
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] | ||
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 { | ||
createSeedOpts := seed.CreateSeedOpts{ | ||
UserProvidedSeedName: o.SeedName, | ||
DirectoryPath: o.EC.SeedsDirectory, | ||
} | ||
|
||
// If we are initializing from a database table | ||
// create a hasura client and add table name opts | ||
if createSeedOpts.Data == nil { | ||
if len(o.FromTableNames) > 0 { | ||
migrateDriver, err := migrate.NewMigrate(ec, true) | ||
if err != nil { | ||
return errors.Wrap(err, "cannot initialize migrate driver") | ||
} | ||
// Send the query | ||
body, err := migrateDriver.ExportDataDump(o.FromTableNames) | ||
if err != nil { | ||
return errors.Wrap(err, "exporting seed data") | ||
} | ||
|
||
createSeedOpts.Data = bytes.NewReader(body) | ||
} else { | ||
const defaultText = "" | ||
data, err := editor.CaptureInputFromEditor(editor.GetPreferredEditorFromEnvironment, defaultText, "*.sql") | ||
if err != nil { | ||
return errors.Wrap(err, "cannot find default editor from env") | ||
} | ||
createSeedOpts.Data = bytes.NewReader(data) | ||
} | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE IF NOT EXISTS account( | ||
user_id serial PRIMARY KEY, | ||
username VARCHAR (50) UNIQUE NOT NULL, | ||
password VARCHAR (50) NOT NULL, | ||
email VARCHAR (355) UNIQUE NOT NULL, | ||
created_on TIMESTAMP NOT NULL, | ||
last_login TIMESTAMP | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE account2( | ||
user_id serial PRIMARY KEY, | ||
username VARCHAR (50) UNIQUE NOT NULL, | ||
password VARCHAR (50) NOT NULL, | ||
email VARCHAR (355) UNIQUE NOT NULL, | ||
created_on TIMESTAMP NOT NULL, | ||
last_login TIMESTAMP | ||
); |
Oops, something went wrong.