Skip to content

Commit

Permalink
fix: missing block flag on migrate status
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Sep 20, 2023
1 parent 03e1f4a commit cd87899
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,5 @@ func RegisterRemoteURLFlags(flags *pflag.FlagSet) {
flags.String(FlagAuthority, "", "Set the authority header for the remote gRPC server.")
flags.Bool(FlagInsecureNoTransportSecurity, false, "Disables transport security. Do not use this in production.")
flags.Bool(FlagInsecureSkipHostVerification, false, "Disables hostname verification. Do not use this in production.")
flags.Bool(FlagBlock, false, "Block until all migrations have been applied")
flags.Bool(FlagBlock, false, "Block until the connection is up.")
}
19 changes: 19 additions & 0 deletions cmd/migrate/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"strings"
"testing"
"time"

"github.com/ory/x/dbal"

Expand Down Expand Up @@ -98,6 +99,24 @@ func TestMigrate(t *testing.T) {

cmd := newCmd(ctx, "-c", cf)

t.Run("case=shows status", func(t *testing.T) {
stdOut := cmd.ExecNoErr(t, "status")
assert.Contains(t, stdOut, "Pending")
assert.NotContains(t, stdOut, "Applied")
})

t.Run("case=status blocks until all are applied", func(t *testing.T) {
cmd := *cmd
ctx, cancel := context.WithTimeout(ctx, 100*time.Millisecond)
defer cancel()
cmd.Ctx = ctx

stdOut, stdErr, err := cmd.Exec(nil, "status", "--block")
require.ErrorIs(t, err, cmdx.ErrNoPrintButFail)
assert.Contains(t, stdOut, "Waiting for migrations to finish...")
assert.Contains(t, stdErr, "Context was canceled, exiting...", stdOut)
})

t.Run("case=aborts on no", func(t *testing.T) {
stdOut, stdErr, err := cmd.Exec(bytes.NewBufferString("n\n"), "up")
require.NoError(t, err, "%s %s", stdOut, stdErr)
Expand Down
15 changes: 8 additions & 7 deletions cmd/migrate/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"github.com/ory/x/cmdx"
"github.com/spf13/cobra"

"github.com/ory/keto/cmd/client"
"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 @@ -26,11 +26,6 @@ func newStatusCmd(opts []ketoctx.Option) *cobra.Command {
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

block, err := cmd.Flags().GetBool(client.FlagBlock)
if err != nil {
return err
}

reg, err := driver.NewDefaultRegistry(ctx, cmd.Flags(), true, opts)
if err != nil {
return err
Expand All @@ -54,7 +49,12 @@ func newStatusCmd(opts []ketoctx.Option) *cobra.Command {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " - %s\n", m.Name)
}
}
time.Sleep(time.Second)
select {
case <-ctx.Done():
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Context was canceled, exiting...")
return cmdx.FailSilently(cmd)
case <-time.After(time.Second):
}
s, err = mb.Status(ctx)
if err != nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Could not get migration status: %+v\n", err)
Expand All @@ -68,6 +68,7 @@ func newStatusCmd(opts []ketoctx.Option) *cobra.Command {
}

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

return cmd
}

0 comments on commit cd87899

Please sign in to comment.