graphql-engine/cli/commands/migrate_status.go

98 lines
2.5 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"bytes"
"fmt"
"text/tabwriter"
2018-06-24 16:40:48 +03:00
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/migrate"
2018-06-24 16:40:48 +03:00
"github.com/hasura/graphql-engine/cli/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
2018-06-24 16:40:48 +03:00
)
func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
v := viper.New()
2018-06-24 16:40:48 +03:00
opts := &migrateStatusOptions{
EC: ec,
}
migrateStatusCmd := &cobra.Command{
Use: "status",
Short: "Display current status of migrations on a database",
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
ec.Viper = v
return ec.Validate()
},
2018-06-24 16:40:48 +03:00
RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("Fetching migration status...")
status, err := opts.run()
opts.EC.Spinner.Stop()
if err != nil {
return err
}
buf := printStatus(status)
fmt.Println(buf.String())
return nil
2018-06-24 16:40:48 +03:00
},
}
f := migrateStatusCmd.Flags()
f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
f.String("admin-secret", "", "admin secret for Hasura GraphQL Engine")
f.String("access-key", "", "access key for Hasura GraphQL Engine")
f.MarkDeprecated("access-key", "use --admin-secret instead")
// need to create a new viper because https://github.com/spf13/viper/issues/233
v.BindPFlag("endpoint", f.Lookup("endpoint"))
v.BindPFlag("admin_secret", f.Lookup("admin-secret"))
v.BindPFlag("access_key", f.Lookup("access-key"))
2018-06-24 16:40:48 +03:00
return migrateStatusCmd
}
type migrateStatusOptions struct {
EC *cli.ExecutionContext
}
func (o *migrateStatusOptions) run() (*migrate.Status, error) {
migrateDrv, err := newMigrate(o.EC.MigrationDir, o.EC.ServerConfig.ParsedEndpoint, o.EC.ServerConfig.AdminSecret, o.EC.Logger, o.EC.Version, true)
2018-06-24 16:40:48 +03:00
if err != nil {
return nil, err
2018-06-24 16:40:48 +03:00
}
status, err := executeStatus(migrateDrv)
if err != nil {
return nil, errors.Wrap(err, "cannot fetch migrate status")
}
return status, nil
}
func printStatus(status *migrate.Status) *bytes.Buffer {
out := new(tabwriter.Writer)
buf := &bytes.Buffer{}
out.Init(buf, 0, 8, 2, ' ', 0)
w := util.NewPrefixWriter(out)
w.Write(util.LEVEL_0, "VERSION\tSOURCE STATUS\tDATABASE STATUS\n")
for _, version := range status.Index {
w.Write(util.LEVEL_0, "%d\t%s\t%s\n",
version,
convertBool(status.Migrations[version].IsPresent),
convertBool(status.Migrations[version].IsApplied),
)
}
out.Flush()
return buf
}
func convertBool(ok bool) string {
switch ok {
case true:
return "Present"
case false:
return "Not Present"
}
return ""
2018-06-24 16:40:48 +03:00
}