Skip to content

Commit

Permalink
improve no migrations found messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kutluhanmetin committed Nov 6, 2023
1 parent f4f41b6 commit 569c541
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
2 changes: 1 addition & 1 deletion base/commands/migration/cancel_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func noMigrationsCancelTest(t *testing.T) {
tcx.Tester(func(tcx it.TestContext) {
createMapping(ctx, tcx)
err := tcx.CLC().Execute(ctx, "cancel")
require.Contains(t, err.Error(), "finding migration in progress: no rows found")
require.Contains(t, err.Error(), "finding migration in progress: no result found")
})
}

Expand Down
16 changes: 14 additions & 2 deletions base/commands/migration/cancel_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ func (st *CancelStages) Build(ctx context.Context, ec plug.ExecContext) []stage.
FailureMsg: "Could not connect to the migration cluster",
Func: st.connectStage(ec),
},
{
ProgressMsg: "Finding migration in progress",
SuccessMsg: "Found migration in progress",
FailureMsg: "Could not find a migration in progress",
Func: st.findMigrationInProgress(ec),
},
{
ProgressMsg: "Canceling the migration",
SuccessMsg: "Canceled the migration",
Expand All @@ -47,13 +53,19 @@ func (st *CancelStages) connectStage(ec plug.ExecContext) func(context.Context,
if err != nil {
return nil, err
}
st.cancelQueue, err = st.ci.Client().GetQueue(ctx, CancelQueue)
return nil, nil
}
}

func (st *CancelStages) findMigrationInProgress(ec plug.ExecContext) func(context.Context, stage.Statuser[any]) (any, error) {
return func(ctx context.Context, status stage.Statuser[any]) (any, error) {
m, err := findMigrationInProgress(ctx, st.ci)
if err != nil {
return nil, err
}
st.migrationID = m.MigrationID
st.cancelQueue, err = st.ci.Client().GetQueue(ctx, CancelQueue)
return nil, err
return nil, nil
}
}

Expand Down
30 changes: 30 additions & 0 deletions base/commands/migration/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build std || migration

package migration

import (
"context"
"encoding/json"
"fmt"

"github.com/hazelcast/hazelcast-go-client"
"github.com/hazelcast/hazelcast-go-client/serialization"
)

type MigrationInProgress struct {
MigrationID string `json:"id"`
}

func findMigrationInProgress(ctx context.Context, ci *hazelcast.ClientInternal) (MigrationInProgress, error) {
var mip MigrationInProgress
q := fmt.Sprintf("SELECT this FROM %s WHERE JSON_VALUE(this, '$.status') IN('STARTED', 'IN_PROGRESS', 'CANCELING')", StatusMapName)
r, err := querySingleRow(ctx, ci, q)
if err != nil {
return mip, fmt.Errorf("finding migration in progress: %w", err)
}
m := r.(serialization.JSON)
if err = json.Unmarshal(m, &mip); err != nil {
return mip, fmt.Errorf("parsing migration in progress: %w", err)
}
return mip, nil
}
2 changes: 1 addition & 1 deletion base/commands/migration/migration_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,5 +290,5 @@ func querySingleRow(ctx context.Context, ci *hazelcast.ClientInternal, query str
}
return r, nil
}
return nil, errors.New("no rows found")
return nil, errors.New("no result found")
}
33 changes: 12 additions & 21 deletions base/commands/migration/status_stages.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ package migration

import (
"context"
"encoding/json"
"fmt"

"github.com/hazelcast/hazelcast-commandline-client/clc/ux/stage"
"github.com/hazelcast/hazelcast-commandline-client/internal/plug"
"github.com/hazelcast/hazelcast-go-client"
"github.com/hazelcast/hazelcast-go-client/serialization"
)

type StatusStages struct {
Expand All @@ -29,38 +26,32 @@ func (st *StatusStages) Build(ctx context.Context, ec plug.ExecContext) []stage.
FailureMsg: "Could not connect to the migration cluster",
Func: st.connectStage(ec),
},
{
ProgressMsg: "Finding migration in progress",
SuccessMsg: "Found migration in progress",
FailureMsg: "Could not find a migration in progress",
Func: st.findMigrationInProgress(ec),
},
}
}

type MigrationInProgress struct {
MigrationID string `json:"id"`
}

func (st *StatusStages) connectStage(ec plug.ExecContext) func(context.Context, stage.Statuser[any]) (any, error) {
return func(ctx context.Context, status stage.Statuser[any]) (any, error) {
var err error
st.ci, err = ec.ClientInternal(ctx)
if err != nil {
return nil, err
}
return nil, nil
}
}

func (st *StatusStages) findMigrationInProgress(ec plug.ExecContext) func(context.Context, stage.Statuser[any]) (any, error) {
return func(ctx context.Context, status stage.Statuser[any]) (any, error) {
m, err := findMigrationInProgress(ctx, st.ci)
if err != nil {
return nil, err
}
return m.MigrationID, err
}
}

func findMigrationInProgress(ctx context.Context, ci *hazelcast.ClientInternal) (MigrationInProgress, error) {
var mip MigrationInProgress
q := fmt.Sprintf("SELECT this FROM %s WHERE JSON_VALUE(this, '$.status') IN('STARTED', 'IN_PROGRESS', 'CANCELING')", StatusMapName)
r, err := querySingleRow(ctx, ci, q)
if err != nil {
return mip, fmt.Errorf("finding migration in progress: %w", err)
}
m := r.(serialization.JSON)
if err = json.Unmarshal(m, &mip); err != nil {
return mip, fmt.Errorf("parsing migration in progress: %w", err)
}
return mip, nil
}
2 changes: 1 addition & 1 deletion base/commands/migration/status_stages_it_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func noMigrationsStatusTest(t *testing.T) {
execErr = tcx.CLC().Execute(ctx, "status")
})
wg.Wait()
require.Contains(t, execErr.Error(), "finding migration in progress: no rows found")
require.Contains(t, execErr.Error(), "finding migration in progress: no result found")
})
}

Expand Down

0 comments on commit 569c541

Please sign in to comment.