Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing block flag on migrate status #1432

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}