graphql-engine/cli/commands/migrate_status.go

89 lines
2.0 KiB
Go
Raw Normal View History

2018-06-24 16:40:48 +03:00
package commands
import (
"bytes"
"fmt"
"os"
"text/tabwriter"
2018-06-24 16:40:48 +03:00
"github.com/hasura/graphql-engine/cli/util"
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/pkg/errors"
"github.com/spf13/cobra"
)
func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
opts := &MigrateStatusOptions{
2018-06-24 16:40:48 +03:00
EC: ec,
}
migrateStatusCmd := &cobra.Command{
Use: "status",
Short: "Display current status of migrations on a database",
Example: ` # Use with admin secret:
hasura migrate status --admin-secret "<your-admin-secret>"
# Check status on a different server:
hasura migrate status --endpoint "<endpoint>"`,
2018-06-24 16:40:48 +03:00
SilenceUsage: true,
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.Fprintf(os.Stdout, "%s", buf)
return nil
2018-06-24 16:40:48 +03:00
},
}
return migrateStatusCmd
}
type MigrateStatusOptions struct {
2018-06-24 16:40:48 +03:00
EC *cli.ExecutionContext
}
func (o *MigrateStatusOptions) Run() (*migrate.Status, error) {
2020-04-07 12:23:20 +03:00
migrateDrv, err := migrate.NewMigrate(o.EC, 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\tNAME\tSOURCE STATUS\tDATABASE STATUS\n")
for _, version := range status.Index {
w.Write(util.LEVEL_0, "%d\t%s\t%s\t%s\n",
version,
status.Migrations[version].Name,
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
}