diff --git a/go.mod b/go.mod index 0c90886f..698e7424 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c github.com/pkg/errors v0.9.1 - github.com/planetscale/planetscale-go v0.112.0 + github.com/planetscale/planetscale-go v0.113.0 github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7 github.com/planetscale/psdbproxy v0.0.0-20241009145102-7fdfa92ae3ca github.com/spf13/cobra v1.8.1 diff --git a/go.sum b/go.sum index c5167533..3997ad5f 100644 --- a/go.sum +++ b/go.sum @@ -114,8 +114,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY= github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q= -github.com/planetscale/planetscale-go v0.112.0 h1:cTuXJrVhO/wh89aKvqxeVeLOgjbDSWcIaf0zEcb58Vg= -github.com/planetscale/planetscale-go v0.112.0/go.mod h1:ldGffCLckkR8fjGDjDFs4WcjlDr8uqg2qRUZhRYBEMI= +github.com/planetscale/planetscale-go v0.113.0 h1:0jYB906v5bn61MncpW+ixA3qroDy8kMuaxmwVBTB20w= +github.com/planetscale/planetscale-go v0.113.0/go.mod h1:ldGffCLckkR8fjGDjDFs4WcjlDr8uqg2qRUZhRYBEMI= github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7 h1:dxdoFKWVDlV1gq8UQC8NWCofLjCEjEHw47gfeojgs28= github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7/go.mod h1:WZmi4gw3rOK+ryd1inGxgfKwoFV04O7xBCqzWzv0/0U= github.com/planetscale/psdbproxy v0.0.0-20241009145102-7fdfa92ae3ca h1:E5E1yyZ03FzA/6styZr5C2L3DdqhnkZsKSJy5q4JnuU= diff --git a/internal/cmd/branch/branch.go b/internal/cmd/branch/branch.go index fbac6835..198f7b24 100644 --- a/internal/cmd/branch/branch.go +++ b/internal/cmd/branch/branch.go @@ -31,9 +31,7 @@ func BranchCmd(ch *cmdutil.Helper) *cobra.Command { cmd.AddCommand(RefreshSchemaCmd(ch)) cmd.AddCommand(PromoteCmd(ch)) cmd.AddCommand(DemoteCmd(ch)) - cmd.AddCommand(VSchemaCmd(ch)) cmd.AddCommand(RoutingRulesCmd(ch)) - cmd.AddCommand(KeyspaceCmd(ch)) cmd.AddCommand(SafeMigrationsCmd(ch)) cmd.AddCommand(LintCmd(ch)) @@ -84,44 +82,3 @@ func toDatabaseBranches(branches []*ps.DatabaseBranch) []*DatabaseBranch { return bs } - -type DatabaseBranchKeyspace struct { - Name string `header:"name" json:"name"` - Shards int `header:"shards" json:"shards"` - Sharded bool `header:"sharded" json:"sharded"` - CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"` - UpdatedAt int64 `header:"updated_at,timestamp(ms|utc|human)" json:"updated_at"` - - orig *ps.Keyspace -} - -func (d *DatabaseBranchKeyspace) MarshalJSON() ([]byte, error) { - return json.MarshalIndent(d.orig, "", " ") -} - -func (d *DatabaseBranchKeyspace) MarshalCSVValue() interface{} { - return []*DatabaseBranchKeyspace{d} -} - -// ToDatabaseBranch returns a struct that prints out the various fields of a -// database model. -func ToDatabaseBranchKeyspace(ks *ps.Keyspace) *DatabaseBranchKeyspace { - return &DatabaseBranchKeyspace{ - Name: ks.Name, - Shards: ks.Shards, - Sharded: ks.Sharded, - CreatedAt: ks.CreatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)), - UpdatedAt: ks.UpdatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)), - orig: ks, - } -} - -func toDatabaseBranchkeyspaces(keyspaces []*ps.Keyspace) []*DatabaseBranchKeyspace { - bs := make([]*DatabaseBranchKeyspace, 0, len(keyspaces)) - - for _, ks := range keyspaces { - bs = append(bs, ToDatabaseBranchKeyspace(ks)) - } - - return bs -} diff --git a/internal/cmd/branch/keyspaces.go b/internal/cmd/branch/keyspaces.go deleted file mode 100644 index e37bee49..00000000 --- a/internal/cmd/branch/keyspaces.go +++ /dev/null @@ -1,47 +0,0 @@ -package branch - -import ( - "fmt" - - "github.com/planetscale/cli/internal/cmdutil" - "github.com/planetscale/cli/internal/printer" - "github.com/planetscale/planetscale-go/planetscale" - "github.com/spf13/cobra" -) - -// VSchemaCmd is the command for showing the VSchema of a branch. -func KeyspaceCmd(ch *cmdutil.Helper) *cobra.Command { - cmd := &cobra.Command{ - Use: "keyspaces ", - Short: "Show the keyspaces of a branch", - Args: cmdutil.RequiredArgs("database", "branch"), - RunE: func(cmd *cobra.Command, args []string) error { - ctx := cmd.Context() - database, branch := args[0], args[1] - - client, err := ch.Client() - if err != nil { - return err - } - - keyspaces, err := client.DatabaseBranches.Keyspaces(ctx, &planetscale.BranchKeyspacesRequest{ - Organization: ch.Config.Organization, - Database: database, - Branch: branch, - }) - if err != nil { - switch cmdutil.ErrCode(err) { - case planetscale.ErrNotFound: - return fmt.Errorf("branch %s does not exist in database %s (organization: %s)", - printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization)) - default: - return cmdutil.HandleError(err) - } - } - - return ch.Printer.PrintResource(toDatabaseBranchkeyspaces(keyspaces)) - }, - } - - return cmd -} diff --git a/internal/cmd/branch/keyspaces_test.go b/internal/cmd/branch/keyspaces_test.go deleted file mode 100644 index 1650fedb..00000000 --- a/internal/cmd/branch/keyspaces_test.go +++ /dev/null @@ -1,67 +0,0 @@ -package branch - -import ( - "bytes" - "context" - "testing" - - "github.com/planetscale/cli/internal/cmdutil" - "github.com/planetscale/cli/internal/config" - "github.com/planetscale/cli/internal/mock" - "github.com/planetscale/cli/internal/printer" - - qt "github.com/frankban/quicktest" - ps "github.com/planetscale/planetscale-go/planetscale" -) - -func TestBranchKeyspacesCmd(t *testing.T) { - c := qt.New(t) - - var buf bytes.Buffer - format := printer.JSON - p := printer.NewPrinter(&format) - p.SetResourceOutput(&buf) - - org := "planetscale" - db := "planetscale" - branch := "feature" - - res := []*ps.Keyspace{ - { - Name: "blah", - Shards: 1, - Sharded: false, - }, - } - - svc := &mock.DatabaseBranchesService{ - KeyspacesFn: func(ctx context.Context, req *ps.BranchKeyspacesRequest) ([]*ps.Keyspace, error) { - c.Assert(req.Organization, qt.Equals, org) - c.Assert(req.Database, qt.Equals, db) - c.Assert(req.Branch, qt.Equals, branch) - - return res, nil - }, - } - - ch := &cmdutil.Helper{ - Printer: p, - Config: &config.Config{ - Organization: org, - }, - Client: func() (*ps.Client, error) { - return &ps.Client{ - DatabaseBranches: svc, - }, nil - }, - } - - cmd := KeyspaceCmd(ch) - cmd.SetArgs([]string{db, branch}) - err := cmd.Execute() - - c.Assert(err, qt.IsNil) - c.Assert(svc.KeyspacesFnInvoked, qt.IsTrue) - - c.Assert(buf.String(), qt.JSONEquals, res) -} diff --git a/internal/cmd/branch/vschema.go b/internal/cmd/branch/vschema.go deleted file mode 100644 index a6807d84..00000000 --- a/internal/cmd/branch/vschema.go +++ /dev/null @@ -1,171 +0,0 @@ -package branch - -import ( - "errors" - "fmt" - "io" - "os" - - "github.com/planetscale/cli/internal/cmdutil" - "github.com/planetscale/cli/internal/printer" - "github.com/planetscale/planetscale-go/planetscale" - "github.com/spf13/cobra" -) - -// VSchemaCmd is the top-level command for fetching or updating the VSchema of a branch. -func VSchemaCmd(ch *cmdutil.Helper) *cobra.Command { - cmd := &cobra.Command{ - Use: "vschema ", - Deprecated: "use `pscale keyspace vschema` instead", - Short: "Fetch or update your keyspace VSchema", - RunE: func(cmd *cobra.Command, args []string) error { - // keep this around for backwards compat. - return GetVSchemaCmd(ch).RunE(cmd, args) - }, - } - - cmd.AddCommand(GetVSchemaCmd(ch)) - cmd.AddCommand(UpdateVSchemaCmd(ch)) - - return cmd -} - -// GetVSchemaCmd is the command for showing the VSchema of a branch. -func GetVSchemaCmd(ch *cmdutil.Helper) *cobra.Command { - var flags struct { - keyspace string - } - - cmd := &cobra.Command{ - Use: "show ", - Short: "Show the vschema of a branch", - Deprecated: "use `pscale keyspace vschema show` instead", - Args: cmdutil.RequiredArgs("database", "branch"), - Aliases: []string{"get"}, - RunE: func(cmd *cobra.Command, args []string) error { - ctx := cmd.Context() - database, branch := args[0], args[1] - - client, err := ch.Client() - if err != nil { - return err - } - - vschema, err := client.DatabaseBranches.VSchema(ctx, &planetscale.BranchVSchemaRequest{ - Organization: ch.Config.Organization, - Database: database, - Branch: branch, - Keyspace: flags.keyspace, - }) - if err != nil { - switch cmdutil.ErrCode(err) { - case planetscale.ErrNotFound: - return fmt.Errorf("received HTTP 404 for branch %s in database %s (organization: %s). This may mean you're requesting a keyspace that does not exist", - printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization)) - default: - return cmdutil.HandleError(err) - } - } - - if ch.Printer.Format() != printer.Human { - return ch.Printer.PrintResource(vschema) - } - - err = ch.Printer.PrettyPrintJSON([]byte(vschema.Raw)) - if err != nil { - return fmt.Errorf("reading vschema raw: %s", err) - } - - return nil - }, - } - - cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "The keyspace in the branch") - - return cmd -} - -// UpdateVSchemaCmd is the command for updating the VSchema of a branch. -func UpdateVSchemaCmd(ch *cmdutil.Helper) *cobra.Command { - var flags struct { - keyspace string - vschema string - } - - cmd := &cobra.Command{ - Use: "update --vschema ", - Short: "Update the vschema of a branch", - Deprecated: "use `pscale keyspace vschema update` instead", - Args: cmdutil.RequiredArgs("database", "branch"), - RunE: func(cmd *cobra.Command, args []string) error { - ctx := cmd.Context() - database, branch := args[0], args[1] - - client, err := ch.Client() - if err != nil { - return err - } - - var data string - - if flags.vschema != "" { - rawVSchema, err := os.ReadFile(flags.vschema) - if err != nil { - return err - } - data = string(rawVSchema) - } else { - stdinFile, err := os.Stdin.Stat() - if err != nil { - return err - } - - if (stdinFile.Mode() & os.ModeCharDevice) == 0 { - stdin, err := io.ReadAll(os.Stdin) - if err != nil { - return err - } - - data = string(stdin) - } - } - - if len(data) == 0 { - return errors.New("no vschema provided, use the --vschema and provide a file or pipe the vschema to standard in") - } - - vschema, err := client.DatabaseBranches.UpdateVSchema(ctx, &planetscale.UpdateBranchVschemaRequest{ - Organization: ch.Config.Organization, - Database: database, - Branch: branch, - Keyspace: flags.keyspace, - VSchema: data, - }) - if err != nil { - switch cmdutil.ErrCode(err) { - case planetscale.ErrNotFound: - return fmt.Errorf("received HTTP 404 for branch %s in database %s (org: %s). This may mean you're requesting a keyspace that does not exist or not supplying one if you have multiple", - printer.BoldBlue(branch), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization)) - default: - return cmdutil.HandleError(err) - } - } - - if ch.Printer.Format() != printer.Human { - return ch.Printer.PrintResource(vschema) - } - - err = ch.Printer.PrettyPrintJSON([]byte(vschema.Raw)) - if err != nil { - return fmt.Errorf("reading vschema raw: %s", err) - } - - return nil - }, - } - - cmd.Flags().StringVar(&flags.vschema, "vschema", "", "The vschema to set in JSON format") - cmd.Flags().StringVar(&flags.keyspace, "keyspace", "", "The keyspace to apply the vschema to") - - return cmd -} diff --git a/internal/cmd/branch/vschema_test.go b/internal/cmd/branch/vschema_test.go deleted file mode 100644 index 7615181d..00000000 --- a/internal/cmd/branch/vschema_test.go +++ /dev/null @@ -1,63 +0,0 @@ -package branch - -import ( - "bytes" - "context" - "testing" - - "github.com/planetscale/cli/internal/cmdutil" - "github.com/planetscale/cli/internal/config" - "github.com/planetscale/cli/internal/mock" - "github.com/planetscale/cli/internal/printer" - - qt "github.com/frankban/quicktest" - ps "github.com/planetscale/planetscale-go/planetscale" -) - -func TestBranchVSchemaCmd(t *testing.T) { - c := qt.New(t) - - var buf bytes.Buffer - format := printer.JSON - p := printer.NewPrinter(&format) - p.SetResourceOutput(&buf) - - org := "planetscale" - db := "planetscale" - branch := "feature" - - res := &ps.VSchema{ - Raw: `{"sharded": true}`, - } - - svc := &mock.DatabaseBranchesService{ - VSchemaFn: func(ctx context.Context, req *ps.BranchVSchemaRequest) (*ps.VSchema, error) { - c.Assert(req.Organization, qt.Equals, org) - c.Assert(req.Database, qt.Equals, db) - c.Assert(req.Branch, qt.Equals, branch) - - return res, nil - }, - } - - ch := &cmdutil.Helper{ - Printer: p, - Config: &config.Config{ - Organization: org, - }, - Client: func() (*ps.Client, error) { - return &ps.Client{ - DatabaseBranches: svc, - }, nil - }, - } - - cmd := VSchemaCmd(ch) - cmd.SetArgs([]string{"get", db, branch}) - err := cmd.Execute() - - c.Assert(err, qt.IsNil) - c.Assert(svc.VSchemaFnInvoked, qt.IsTrue) - - c.Assert(buf.String(), qt.JSONEquals, res) -} diff --git a/internal/mock/branch.go b/internal/mock/branch.go index 93e921ef..04325f42 100644 --- a/internal/mock/branch.go +++ b/internal/mock/branch.go @@ -25,21 +25,12 @@ type DatabaseBranchesService struct { SchemaFn func(context.Context, *ps.BranchSchemaRequest) ([]*ps.Diff, error) SchemaFnInvoked bool - VSchemaFn func(context.Context, *ps.BranchVSchemaRequest) (*ps.VSchema, error) - VSchemaFnInvoked bool - - UpdateVSchemaFn func(context.Context, *ps.UpdateBranchVschemaRequest) (*ps.VSchema, error) - UpdateVSchemaFnInvoked bool - RoutingRulesFn func(context.Context, *ps.BranchRoutingRulesRequest) (*ps.RoutingRules, error) RoutingRulesFnInvoked bool UpdateRoutingRulesFn func(context.Context, *ps.UpdateBranchRoutingRulesRequest) (*ps.RoutingRules, error) UpdateRoutingRulesFnInvoked bool - KeyspacesFn func(context.Context, *ps.BranchKeyspacesRequest) ([]*ps.Keyspace, error) - KeyspacesFnInvoked bool - RefreshSchemaFn func(context.Context, *ps.RefreshSchemaRequest) error RefreshSchemaFnInvoked bool @@ -89,16 +80,6 @@ func (d *DatabaseBranchesService) Schema(ctx context.Context, req *ps.BranchSche return d.SchemaFn(ctx, req) } -func (d *DatabaseBranchesService) VSchema(ctx context.Context, req *ps.BranchVSchemaRequest) (*ps.VSchema, error) { - d.VSchemaFnInvoked = true - return d.VSchemaFn(ctx, req) -} - -func (d *DatabaseBranchesService) UpdateVSchema(ctx context.Context, req *ps.UpdateBranchVschemaRequest) (*ps.VSchema, error) { - d.UpdateVSchemaFnInvoked = true - return d.UpdateVSchemaFn(ctx, req) -} - func (d *DatabaseBranchesService) RoutingRules(ctx context.Context, req *ps.BranchRoutingRulesRequest) (*ps.RoutingRules, error) { d.RoutingRulesFnInvoked = true return d.RoutingRulesFn(ctx, req) @@ -109,11 +90,6 @@ func (d *DatabaseBranchesService) UpdateRoutingRules(ctx context.Context, req *p return d.UpdateRoutingRulesFn(ctx, req) } -func (d *DatabaseBranchesService) Keyspaces(ctx context.Context, req *ps.BranchKeyspacesRequest) ([]*ps.Keyspace, error) { - d.KeyspacesFnInvoked = true - return d.KeyspacesFn(ctx, req) -} - func (d *DatabaseBranchesService) RefreshSchema(ctx context.Context, req *ps.RefreshSchemaRequest) error { d.RefreshSchemaFnInvoked = true return d.RefreshSchemaFn(ctx, req)