added feedback on migration commands (close #1410) (#1931)

This commit is contained in:
Aravind Shankar 2019-04-03 16:59:58 +05:30 committed by Shahidh K Muhammed
parent 56c67509aa
commit 5f02ebdefa
5 changed files with 36 additions and 25 deletions

View File

@ -25,7 +25,14 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
return ec.Validate() return ec.Validate()
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return opts.run() opts.EC.Spin("Applying migrations...")
err := opts.run()
opts.EC.Spinner.Stop()
if err != nil {
return err
}
opts.EC.Logger.Info("migrations applied")
return nil
}, },
} }
f := migrateApplyCmd.Flags() f := migrateApplyCmd.Flags()
@ -85,7 +92,6 @@ func (o *migrateApplyOptions) run() error {
} }
return errors.Wrap(err, "apply failed") return errors.Wrap(err, "apply failed")
} }
o.EC.Logger.Info("migrations applied")
return nil return nil
} }

View File

@ -13,11 +13,10 @@ import (
"github.com/hasura/graphql-engine/cli" "github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/version" "github.com/hasura/graphql-engine/cli/version"
"github.com/sirupsen/logrus/hooks/test" "github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
) )
func testMigrateApply(t *testing.T, endpoint *url.URL, migrationsDir string, up string, down string, v string, vType string) { func testMigrateApply(t *testing.T, endpoint *url.URL, migrationsDir string, up string, down string, v string, vType string) {
logger, hook := test.NewNullLogger() logger, _ := test.NewNullLogger()
opts := &migrateApplyOptions{ opts := &migrateApplyOptions{
EC: &cli.ExecutionContext{ EC: &cli.ExecutionContext{
Logger: logger, Logger: logger,
@ -46,8 +45,6 @@ func testMigrateApply(t *testing.T, endpoint *url.URL, migrationsDir string, up
if err != nil { if err != nil {
t.Fatalf("failed applying migration: %v", err) t.Fatalf("failed applying migration: %v", err)
} }
assert.Equal(t, "migrations applied", hook.LastEntry().Message)
} }
func TestMigrateApplyWithInvalidEndpoint(t *testing.T) { func TestMigrateApplyWithInvalidEndpoint(t *testing.T) {

View File

@ -33,7 +33,17 @@ func newMigrateCreateCmd(ec *cli.ExecutionContext) *cobra.Command {
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.name = args[0] opts.name = args[0]
return opts.run() opts.EC.Spin("Creating migration files...")
version, err := opts.run()
opts.EC.Spinner.Stop()
if err != nil {
return err
}
opts.EC.Logger.WithFields(log.Fields{
"version": version,
"name": opts.name,
}).Info("Migrations files created")
return nil
}, },
} }
f := migrateCreateCmd.Flags() f := migrateCreateCmd.Flags()
@ -68,7 +78,7 @@ type migrateCreateOptions struct {
metaDataServer bool metaDataServer bool
} }
func (o *migrateCreateOptions) run() (err error) { func (o *migrateCreateOptions) run() (version int64, err error) {
timestamp := getTime() timestamp := getTime()
createOptions := mig.New(timestamp, o.name, o.EC.MigrationDir) createOptions := mig.New(timestamp, o.name, o.EC.MigrationDir)
@ -76,19 +86,19 @@ func (o *migrateCreateOptions) run() (err error) {
// sql-file flag is set // sql-file flag is set
err := createOptions.SetSQLUpFromFile(o.sqlFile) err := createOptions.SetSQLUpFromFile(o.sqlFile)
if err != nil { if err != nil {
return errors.Wrap(err, "cannot set sql file") return 0, errors.Wrap(err, "cannot set sql file")
} }
} }
if o.flags.Changed("metadata-from-file") && o.metaDataServer { if o.flags.Changed("metadata-from-file") && o.metaDataServer {
return errors.New("only one metadata type can be set") return 0, errors.New("only one metadata type can be set")
} }
if o.flags.Changed("metadata-from-file") { if o.flags.Changed("metadata-from-file") {
// metadata-file flag is set // metadata-file flag is set
err := createOptions.SetMetaUpFromFile(o.metaDataFile) err := createOptions.SetMetaUpFromFile(o.metaDataFile)
if err != nil { if err != nil {
return errors.Wrap(err, "cannot set metadata file") return 0, errors.Wrap(err, "cannot set metadata file")
} }
} }
@ -96,35 +106,35 @@ func (o *migrateCreateOptions) run() (err error) {
// create new migrate instance // create new migrate instance
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.ServerConfig.ParsedEndpoint, o.EC.ServerConfig.AdminSecret, o.EC.Logger, o.EC.Version) migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.ServerConfig.ParsedEndpoint, o.EC.ServerConfig.AdminSecret, o.EC.Logger, o.EC.Version)
if err != nil { if err != nil {
return errors.Wrap(err, "cannot create migrate instance") return 0, errors.Wrap(err, "cannot create migrate instance")
} }
// fetch metadata from server // fetch metadata from server
metaData, err := migrateDrv.ExportMetadata() metaData, err := migrateDrv.ExportMetadata()
if err != nil { if err != nil {
return errors.Wrap(err, "cannot fetch metadata from server") return 0, errors.Wrap(err, "cannot fetch metadata from server")
} }
tmpfile, err := ioutil.TempFile("", "metadata") tmpfile, err := ioutil.TempFile("", "metadata")
if err != nil { if err != nil {
return errors.Wrap(err, "cannot create tempfile") return 0, errors.Wrap(err, "cannot create tempfile")
} }
defer os.Remove(tmpfile.Name()) defer os.Remove(tmpfile.Name())
t, err := yaml.Marshal(metaData) t, err := yaml.Marshal(metaData)
if err != nil { if err != nil {
return errors.Wrap(err, "cannot marshal metadata") return 0, errors.Wrap(err, "cannot marshal metadata")
} }
if _, err := tmpfile.Write(t); err != nil { if _, err := tmpfile.Write(t); err != nil {
return errors.Wrap(err, "cannot write to temp file") return 0, errors.Wrap(err, "cannot write to temp file")
} }
if err := tmpfile.Close(); err != nil { if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "cannot close tmp file") return 0, errors.Wrap(err, "cannot close tmp file")
} }
err = createOptions.SetMetaUpFromFile(tmpfile.Name()) err = createOptions.SetMetaUpFromFile(tmpfile.Name())
if err != nil { if err != nil {
return errors.Wrap(err, "cannot parse metadata from the server") return 0, errors.Wrap(err, "cannot parse metadata from the server")
} }
} }
@ -141,13 +151,9 @@ func (o *migrateCreateOptions) run() (err error) {
}() }()
err = createOptions.Create() err = createOptions.Create()
if err != nil { if err != nil {
return errors.Wrap(err, "error creating migration files") return 0, errors.Wrap(err, "error creating migration files")
} }
o.EC.Logger.WithFields(log.Fields{ return 0, nil
"version": timestamp,
"name": o.name,
}).Info("Migrations files created")
return nil
} }
func getTime() int64 { func getTime() int64 {

View File

@ -26,7 +26,7 @@ func TestMigrateCreateCmd(t *testing.T) {
flags: pflag.NewFlagSet("migrate-create-test", pflag.ContinueOnError), flags: pflag.NewFlagSet("migrate-create-test", pflag.ContinueOnError),
} }
err := opts.run() _, err := opts.run()
if err != nil { if err != nil {
t.Fatalf("failed creating migration: %v", err) t.Fatalf("failed creating migration: %v", err)
} }

View File

@ -27,7 +27,9 @@ func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
return ec.Validate() return ec.Validate()
}, },
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("Fetching migration status...")
status, err := opts.run() status, err := opts.run()
opts.EC.Spinner.Stop()
if err != nil { if err != nil {
return err return err
} }