Skip to content
Open
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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/mattn/go-shellwords v1.0.12
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/planetscale/planetscale-go v0.168.1
github.com/planetscale/planetscale-go v0.168.2-0.20260611211624-109b07535906
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7
github.com/spf13/cobra v1.10.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ github.com/planetscale/planetscale-go v0.168.1-0.20260610223405-068c67a15c9b h1:
github.com/planetscale/planetscale-go v0.168.1-0.20260610223405-068c67a15c9b/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
github.com/planetscale/planetscale-go v0.168.1 h1:ikGvRC5YlQQiNf7vF9vxi6WBXNs2NokNie0sazMTTcA=
github.com/planetscale/planetscale-go v0.168.1/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
github.com/planetscale/planetscale-go v0.168.2-0.20260611211624-109b07535906 h1:n+R4KmP6Ud62T8tvCK9uJ5pzpx4g0NHaRahecOH/C4M=
github.com/planetscale/planetscale-go v0.168.2-0.20260611211624-109b07535906/go.mod h1:paQCI5SgquuoewvMQM7R+r1XJO868bdP6/ihGidYRM0=
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4 h1:Xv5pj20Rhfty1Tv0OVcidg4ez4PvGrpKvb6rvUwQgDs=
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4/go.mod h1:M52h5IWxAcbdQ1hSZrLAGQC4ZXslxEsK/Wh9nu3wdWs=
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7 h1:aRd6vdE1fyuSI4RVj7oCr8lFmgqXvpnPUmN85VbZCp8=
Expand Down
67 changes: 67 additions & 0 deletions internal/cmd/branch/vtctld/shard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package vtctld

import (
"fmt"

"github.com/planetscale/cli/internal/cmdutil"
ps "github.com/planetscale/planetscale-go/planetscale"
"github.com/spf13/cobra"
)

// GetShardCmd reads a shard record from the cluster via vtctld.
func GetShardCmd(ch *cmdutil.Helper) *cobra.Command {
var flags struct {
keyspace string
shard string
}

cmd := &cobra.Command{
Use: "get-shard <database> <branch>",
Short: "Get a shard record for a branch",
Long: "Get a live shard record from the cluster via vtctld, including tablet controls and denied tables.",
Args: cmdutil.RequiredArgs("database", "branch"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
database, branch := args[0], args[1]

if flags.keyspace == "" {
return fmt.Errorf("keyspace is required")
}
if flags.shard == "" {
return fmt.Errorf("shard is required")
}

client, err := ch.Client()
if err != nil {
return err
}

end := ch.Printer.PrintProgress(
fmt.Sprintf("Fetching shard %s/%s for %s\u2026",
flags.keyspace, flags.shard,
progressTarget(ch.Config.Organization, database, branch)))
defer end()

data, err := client.Vtctld.GetShard(ctx, &ps.VtctldGetShardRequest{
Organization: ch.Config.Organization,
Database: database,
Branch: branch,
Keyspace: flags.keyspace,
Shard: flags.shard,
})
if err != nil {
return cmdutil.HandleError(err)
}

end()
return ch.Printer.PrettyPrintJSON(data)
},
}

cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "Keyspace name")
cmd.Flags().StringVar(&flags.shard, "shard", "", "Shard name (e.g. \"-\" for unsharded)")
cmd.MarkFlagRequired("keyspace")
cmd.MarkFlagRequired("shard")

return cmd
}
56 changes: 56 additions & 0 deletions internal/cmd/branch/vtctld/shard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package vtctld

import (
"bytes"
"context"
"encoding/json"
"testing"

qt "github.com/frankban/quicktest"

"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/config"
"github.com/planetscale/cli/internal/mock"
"github.com/planetscale/cli/internal/printer"
ps "github.com/planetscale/planetscale-go/planetscale"
)

func TestGetShard(t *testing.T) {
c := qt.New(t)

org := "my-org"
db := "my-db"
branch := "my-branch"

svc := &mock.VtctldService{
GetShardFn: func(ctx context.Context, req *ps.VtctldGetShardRequest) (json.RawMessage, error) {
c.Assert(req.Organization, qt.Equals, org)
c.Assert(req.Database, qt.Equals, db)
c.Assert(req.Branch, qt.Equals, branch)
c.Assert(req.Keyspace, qt.Equals, "commerce")
c.Assert(req.Shard, qt.Equals, "-")
return json.RawMessage(`{"keyspace":"commerce","name":"-"}`), nil
},
}

var buf bytes.Buffer
format := printer.JSON
p := printer.NewPrinter(&format)
p.SetResourceOutput(&buf)

ch := &cmdutil.Helper{
Printer: p,
Config: &config.Config{Organization: org},
Client: func() (*ps.Client, error) {
return &ps.Client{
Vtctld: svc,
}, nil
},
}

cmd := GetShardCmd(ch)
cmd.SetArgs([]string{db, branch, "--keyspace", "commerce", "--shard", "-"})
err := cmd.Execute()
c.Assert(err, qt.IsNil)
c.Assert(svc.GetShardFnInvoked, qt.IsTrue)
}
1 change: 1 addition & 0 deletions internal/cmd/branch/vtctld/vtctld.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func VtctldCmd(ch *cmdutil.Helper) *cobra.Command {
cmd.AddCommand(ListWorkflowsCmd(ch))
cmd.AddCommand(ListKeyspacesCmd(ch))
cmd.AddCommand(GetRoutingRulesCmd(ch))
cmd.AddCommand(GetShardCmd(ch))
cmd.AddCommand(ListTabletsCmd(ch))
cmd.AddCommand(StartWorkflowCmd(ch))
cmd.AddCommand(StopWorkflowCmd(ch))
Expand Down
8 changes: 8 additions & 0 deletions internal/mock/vtctld_general.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type VtctldService struct {
GetRoutingRulesFn func(context.Context, *ps.VtctldGetRoutingRulesRequest) (json.RawMessage, error)
GetRoutingRulesFnInvoked bool

GetShardFn func(context.Context, *ps.VtctldGetShardRequest) (json.RawMessage, error)
GetShardFnInvoked bool

ListTabletsFn func(context.Context, *ps.ListBranchTabletsRequest) ([]*ps.TabletGroup, error)
ListTabletsFnInvoked bool

Expand Down Expand Up @@ -54,6 +57,11 @@ func (s *VtctldService) GetRoutingRules(ctx context.Context, req *ps.VtctldGetRo
return s.GetRoutingRulesFn(ctx, req)
}

func (s *VtctldService) GetShard(ctx context.Context, req *ps.VtctldGetShardRequest) (json.RawMessage, error) {
s.GetShardFnInvoked = true
return s.GetShardFn(ctx, req)
}

func (s *VtctldService) ListTablets(ctx context.Context, req *ps.ListBranchTabletsRequest) ([]*ps.TabletGroup, error) {
s.ListTabletsFnInvoked = true
return s.ListTabletsFn(ctx, req)
Expand Down