mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
a398d3b190
GITHUB_PR_NUMBER: 6111 GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/6111 Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: 1f6517acfacb58c566bb5e48f74ea0dfa5c6f063
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package commands
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"text/tabwriter"
|
|
|
|
"github.com/hasura/graphql-engine/cli/util"
|
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
opts := &MigrateStatusOptions{
|
|
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>"`,
|
|
SilenceUsage: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
opts.EC.Spin("Fetching migration status...")
|
|
opts.Datasource = ec.Datasource
|
|
status, err := opts.Run()
|
|
opts.EC.Spinner.Stop()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buf := printStatus(status)
|
|
fmt.Fprintf(os.Stdout, "%s", buf)
|
|
return nil
|
|
},
|
|
}
|
|
return migrateStatusCmd
|
|
}
|
|
|
|
type MigrateStatusOptions struct {
|
|
EC *cli.ExecutionContext
|
|
Datasource string
|
|
}
|
|
|
|
func (o *MigrateStatusOptions) Run() (*migrate.Status, error) {
|
|
migrateDrv, err := migrate.NewMigrate(o.EC, true, o.Datasource)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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\tDIRTY\n")
|
|
for _, version := range status.Index {
|
|
w.Write(util.LEVEL_0, "%d\t%s\t%s\t%s\t%t\n",
|
|
version,
|
|
status.Migrations[version].Name,
|
|
convertBool(status.Migrations[version].IsPresent),
|
|
convertBool(status.Migrations[version].IsApplied),
|
|
status.Migrations[version].IsDirty,
|
|
)
|
|
}
|
|
out.Flush()
|
|
return buf
|
|
}
|
|
|
|
func convertBool(ok bool) string {
|
|
switch ok {
|
|
case true:
|
|
return "Present"
|
|
case false:
|
|
return "Not Present"
|
|
}
|
|
return ""
|
|
}
|