2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2018-07-06 08:06:27 +03:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"text/tabwriter"
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
2018-07-06 08:06:27 +03:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2018-06-28 11:36:25 +03:00
|
|
|
func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
|
2020-02-24 19:14:46 +03:00
|
|
|
opts := &MigrateStatusOptions{
|
2018-06-24 16:40:48 +03:00
|
|
|
EC: ec,
|
|
|
|
}
|
|
|
|
migrateStatusCmd := &cobra.Command{
|
2020-02-24 19:14:46 +03:00
|
|
|
Use: "status",
|
|
|
|
Short: "Display current status of migrations on a database",
|
2019-12-12 08:16:36 +03:00
|
|
|
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 {
|
2019-04-03 14:29:58 +03:00
|
|
|
opts.EC.Spin("Fetching migration status...")
|
2020-02-24 19:14:46 +03:00
|
|
|
status, err := opts.Run()
|
2019-04-03 14:29:58 +03:00
|
|
|
opts.EC.Spinner.Stop()
|
2018-07-06 08:06:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-20 08:10:06 +03:00
|
|
|
buf := printStatus(status)
|
2018-08-21 21:37:47 +03:00
|
|
|
fmt.Println(buf.String())
|
2018-07-06 08:06:27 +03:00
|
|
|
return nil
|
2018-06-24 16:40:48 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return migrateStatusCmd
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
type MigrateStatusOptions struct {
|
2018-06-24 16:40:48 +03:00
|
|
|
EC *cli.ExecutionContext
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
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 {
|
2019-01-04 12:45:54 +03:00
|
|
|
return nil, err
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
2018-07-09 16:47:38 +03:00
|
|
|
status, err := executeStatus(migrateDrv)
|
2018-06-28 11:36:57 +03:00
|
|
|
if err != nil {
|
2018-07-06 08:06:27 +03:00
|
|
|
return nil, errors.Wrap(err, "cannot fetch migrate status")
|
2018-06-28 11:36:57 +03:00
|
|
|
}
|
2018-07-06 08:06:27 +03:00
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
2019-03-20 08:10:06 +03:00
|
|
|
func printStatus(status *migrate.Status) *bytes.Buffer {
|
2018-07-06 08:06:27 +03:00
|
|
|
out := new(tabwriter.Writer)
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
out.Init(buf, 0, 8, 2, ' ', 0)
|
|
|
|
w := util.NewPrefixWriter(out)
|
2019-11-28 14:36:16 +03:00
|
|
|
w.Write(util.LEVEL_0, "VERSION\tNAME\tSOURCE STATUS\tDATABASE STATUS\n")
|
2018-07-06 08:06:27 +03:00
|
|
|
for _, version := range status.Index {
|
2019-11-28 14:36:16 +03:00
|
|
|
w.Write(util.LEVEL_0, "%d\t%s\t%s\t%s\n",
|
2018-07-06 08:06:27 +03:00
|
|
|
version,
|
2019-11-28 14:36:16 +03:00
|
|
|
status.Migrations[version].Name,
|
2018-07-06 08:06:27 +03:00
|
|
|
convertBool(status.Migrations[version].IsPresent),
|
|
|
|
convertBool(status.Migrations[version].IsApplied),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
out.Flush()
|
2018-08-21 21:37:47 +03:00
|
|
|
return buf
|
2018-07-06 08:06:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|