Skip to content

Commit 849deac

Browse files
authored
Rename kind flag for database creation (#1086)
* Switch flag name to engine instead of kind * Use string enum for the database engine * Simplify flags * Update planetscale-go
1 parent 90efee8 commit 849deac

4 files changed

Lines changed: 77 additions & 9 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/mattn/go-shellwords v1.0.12
2525
github.com/mitchellh/go-homedir v1.1.0
2626
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
27-
github.com/planetscale/planetscale-go v0.136.0
27+
github.com/planetscale/planetscale-go v0.137.0
2828
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7
2929
github.com/planetscale/psdbproxy v0.0.0-20250117221522-0c8e2b0e36e6
3030
github.com/spf13/cobra v1.9.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
163163
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
164164
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
165165
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
166-
github.com/planetscale/planetscale-go v0.136.0 h1:l675oGFteUJLjF1vsUffF4XoPL1+PnsojpuSyMZ+pOk=
167-
github.com/planetscale/planetscale-go v0.136.0/go.mod h1:/n7PU99UvYaajJlTbMWMOOlHmkEuN7SFjvLNDSgMQOk=
166+
github.com/planetscale/planetscale-go v0.137.0 h1:q1aqlk5g/jqp70JyEfzZZPpHtVKUoIZRuGsipRaAmcs=
167+
github.com/planetscale/planetscale-go v0.137.0/go.mod h1:/n7PU99UvYaajJlTbMWMOOlHmkEuN7SFjvLNDSgMQOk=
168168
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7 h1:dxdoFKWVDlV1gq8UQC8NWCofLjCEjEHw47gfeojgs28=
169169
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7/go.mod h1:WZmi4gw3rOK+ryd1inGxgfKwoFV04O7xBCqzWzv0/0U=
170170
github.com/planetscale/psdbproxy v0.0.0-20250117221522-0c8e2b0e36e6 h1:/Ox1ZTAdk+soSngzzKoJh5voOzptrpPrux11o30rIaw=

internal/cmd/database/create.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
1616
createReq := &ps.CreateDatabaseRequest{}
1717

18+
var flags struct {
19+
clusterSize string
20+
engine string
21+
}
22+
1823
cmd := &cobra.Command{
1924
Use: "create <database>",
2025
Short: "Create a database instance",
@@ -25,12 +30,12 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
2530
createReq.Organization = ch.Config.Organization
2631
createReq.Name = args[0]
2732

28-
clusterSize, err := cmd.Flags().GetString("cluster-size")
33+
engine, err := parseDatabaseEngine(flags.engine)
2934
if err != nil {
3035
return err
3136
}
3237

33-
createReq.ClusterSize = clusterSize
38+
createReq.Kind = engine
3439

3540
client, err := ch.Client()
3641
if err != nil {
@@ -62,10 +67,9 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
6267

6368
cmd.Flags().StringVar(&createReq.Region, "region", "", "region for the database")
6469

65-
cmd.Flags().String("cluster-size", "", "cluster size for Scaler Pro databases. Use `pscale size cluster list` to see the valid sizes.")
66-
67-
cmd.Flags().StringVar(&createReq.Kind, "kind", "mysql", "Kind of database to create. Supported values: mysql, postgresql. Defaults to mysql.")
68-
cmd.RegisterFlagCompletionFunc("kind", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
70+
cmd.Flags().StringVar(&createReq.ClusterSize, "cluster-size", "", "cluster size for Scaler Pro databases. Use `pscale size cluster list` to see the valid sizes.")
71+
cmd.Flags().StringVar(&flags.engine, "engine", string(ps.DatabaseEngineMySQL), "The database engine for the database. Supported values: mysql, postgresql. Defaults to mysql.")
72+
cmd.RegisterFlagCompletionFunc("engine", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
6973
return []cobra.Completion{
7074
cobra.CompletionWithDesc("mysql", "A Vitess database"),
7175
cobra.CompletionWithDesc("postgresql", "The fastest cloud Postgres"),
@@ -82,3 +86,14 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
8286

8387
return cmd
8488
}
89+
90+
func parseDatabaseEngine(engine string) (ps.DatabaseEngine, error) {
91+
switch engine {
92+
case "mysql":
93+
return ps.DatabaseEngineMySQL, nil
94+
case "postgresql", "postgres":
95+
return ps.DatabaseEnginePostgres, nil
96+
default:
97+
return ps.DatabaseEngineMySQL, fmt.Errorf("invalid database engine %q, supported values: mysql, postgresql", engine)
98+
}
99+
}

internal/cmd/database/create_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,56 @@ func TestDatabase_CreateCmd(t *testing.T) {
6565
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
6666
c.Assert(buf.String(), qt.JSONEquals, res)
6767
}
68+
69+
func TestDatabase_CreateCmdPostgres(t *testing.T) {
70+
c := qt.New(t)
71+
72+
var buf bytes.Buffer
73+
format := printer.JSON
74+
p := printer.NewPrinter(&format)
75+
p.SetResourceOutput(&buf)
76+
77+
org := "planetscale"
78+
db := "planetscale"
79+
80+
res := &ps.Database{Name: "foo"}
81+
82+
svc := &mock.DatabaseService{
83+
CreateFn: func(ctx context.Context, req *ps.CreateDatabaseRequest) (*ps.Database, error) {
84+
c.Assert(req.Organization, qt.Equals, org)
85+
c.Assert(req.Name, qt.Equals, db)
86+
c.Assert(req.Region, qt.Equals, "us-east")
87+
c.Assert(req.Kind, qt.Equals, ps.DatabaseEnginePostgres)
88+
89+
return res, nil
90+
},
91+
}
92+
93+
ch := &cmdutil.Helper{
94+
Printer: p,
95+
Config: &config.Config{
96+
Organization: org,
97+
},
98+
Client: func() (*ps.Client, error) {
99+
return &ps.Client{
100+
Databases: svc,
101+
Organizations: &mock.OrganizationsService{
102+
GetFn: func(ctx context.Context, request *ps.GetOrganizationRequest) (*ps.Organization, error) {
103+
return &ps.Organization{
104+
RemainingFreeDatabases: 1,
105+
Name: request.Organization,
106+
}, nil
107+
},
108+
},
109+
}, nil
110+
},
111+
}
112+
113+
cmd := CreateCmd(ch)
114+
cmd.SetArgs([]string{db, "--region", "us-east", "--engine", "postgresql"})
115+
err := cmd.Execute()
116+
117+
c.Assert(err, qt.IsNil)
118+
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
119+
c.Assert(buf.String(), qt.JSONEquals, res)
120+
}

0 commit comments

Comments
 (0)