Skip to content

Commit

Permalink
feat: add flag to block until migrations are done (#1380)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Jul 24, 2023
1 parent ef5383c commit 129902b
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions cmd/migrate/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ package migrate

import (
"fmt"
"time"

"github.com/ory/x/cmdx"
"github.com/ory/x/popx"

"github.com/ory/x/cmdx"
"github.com/spf13/cobra"

"github.com/ory/keto/internal/driver"
"github.com/ory/keto/ketoctx"
)

func newStatusCmd(opts []ketoctx.Option) *cobra.Command {
block := false
cmd := &cobra.Command{
Use: "status",
Short: "Get the current migration status",
Expand All @@ -33,22 +36,34 @@ func newStatusCmd(opts []ketoctx.Option) *cobra.Command {
return err
}

return BoxStatus(cmd, mb, "")
s, err := mb.Status(ctx)
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not get migration status: %+v\n", err)
return cmdx.FailSilently(cmd)
}

for block && s.HasPending() {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Waiting for migrations to finish...\n")
for _, m := range s {
if m.State == popx.Pending {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " - %s\n", m.Name)
}
}
time.Sleep(time.Second)
s, err = mb.Status(ctx)
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not get migration status: %+v\n", err)
return cmdx.FailSilently(cmd)
}
}

cmdx.PrintTable(cmd, s)
return nil
},
}

cmdx.RegisterFormatFlags(cmd.Flags())
cmd.Flags().BoolVar(&block, "block", false, "Block until all migrations have been applied")

return cmd
}

func BoxStatus(cmd *cobra.Command, mb *popx.MigrationBox, msgPrefix string) error {
s, err := mb.Status(cmd.Context())
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%sCould not get migration status: %+v\n", msgPrefix, err)
return cmdx.FailSilently(cmd)
}

cmdx.PrintTable(cmd, s)
return nil
}

0 comments on commit 129902b

Please sign in to comment.