Skip to content

Commit 9c00057

Browse files
authored
Merge pull request #1161 from planetscale/major-version
Support setting a major version when creating databases and branches
2 parents 24cdc9c + 16d4734 commit 9c00057

File tree

6 files changed

+138
-7
lines changed

6 files changed

+138
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/mattn/go-shellwords v1.0.12
2626
github.com/mitchellh/go-homedir v1.1.0
2727
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
28-
github.com/planetscale/planetscale-go v0.146.0
28+
github.com/planetscale/planetscale-go v0.147.0
2929
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4
3030
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7
3131
github.com/spf13/cobra v1.10.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
175175
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
176176
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
177177
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
178-
github.com/planetscale/planetscale-go v0.146.0 h1:cc65OzW4hkbhNLDApQlVAFG1680obE/OvQzEKPPsT+U=
179-
github.com/planetscale/planetscale-go v0.146.0/go.mod h1:PheYDHAwF14wfCBak1M0J64AdPW8NUeyvgPgWqe7zpI=
178+
github.com/planetscale/planetscale-go v0.147.0 h1:dof3HNIlEhJPN+gAM7BJlUCiuhVsf8mX2wlGtjVbp6U=
179+
github.com/planetscale/planetscale-go v0.147.0/go.mod h1:PheYDHAwF14wfCBak1M0J64AdPW8NUeyvgPgWqe7zpI=
180180
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4 h1:Xv5pj20Rhfty1Tv0OVcidg4ez4PvGrpKvb6rvUwQgDs=
181181
github.com/planetscale/psdb v0.0.0-20250717190954-65c6661ab6e4/go.mod h1:M52h5IWxAcbdQ1hSZrLAGQC4ZXslxEsK/Wh9nu3wdWs=
182182
github.com/planetscale/psdbproxy v0.0.0-20250728082226-3f4ea3a74ec7 h1:aRd6vdE1fyuSI4RVj7oCr8lFmgqXvpnPUmN85VbZCp8=

internal/cmd/branch/create.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
2020
parentBranch string
2121
clusterSize string
2222
backupID string
23+
majorVersion string
2324
}
2425

2526
cmd := &cobra.Command{
@@ -158,6 +159,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
158159
ClusterName: flags.clusterSize,
159160
ParentBranch: flags.parentBranch,
160161
BackupID: flags.backupID,
162+
MajorVersion: flags.majorVersion,
161163
}
162164

163165
dbBranch, err := client.PostgresBranches.Create(cmd.Context(), createReq)
@@ -212,6 +214,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
212214
cmd.Flags().StringVar(&flags.clusterSize, "cluster-size", "PS-10", "Cluster size for branches being created from a backup or seeded with data. Use 'pscale size cluster list' to see the valid sizes.")
213215
cmd.Flags().BoolVar(&flags.dataBranching, "seed-data", false, "Add seed data using the Data Branching™ feature. This branch will be created with the same resources as the base branch.")
214216
cmd.Flags().BoolVar(&flags.wait, "wait", false, "Wait until the branch is ready")
217+
cmd.Flags().StringVar(&flags.majorVersion, "major-version", "", "For PostgreSQL databases, the PostgreSQL major version to use for the branch. Defaults to the major version of the parent branch if it exists or the database's default branch major version. Ignored for branches restored from backups.")
215218
cmd.MarkFlagsMutuallyExclusive("from", "restore")
216219
cmd.MarkFlagsMutuallyExclusive("restore", "seed-data")
217220

@@ -223,6 +226,12 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
223226
return cmdutil.ClusterSizesCompletionFunc(ch, cmd, args, toComplete)
224227
})
225228

229+
cmd.RegisterFlagCompletionFunc("major-version", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
230+
return []cobra.Completion{
231+
cobra.CompletionWithDesc("17", "PostgreSQL 17"),
232+
}, cobra.ShellCompDirectiveNoFileComp
233+
})
234+
226235
return cmd
227236
}
228237

internal/cmd/branch/create_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,62 @@ func TestBranch_CreateCmdWithSeedData(t *testing.T) {
239239
c.Assert(buf.String(), qt.JSONEquals, res)
240240
}
241241

242+
func TestBranch_CreateCmdWithMajorVersion(t *testing.T) {
243+
c := qt.New(t)
244+
245+
var buf bytes.Buffer
246+
format := printer.JSON
247+
p := printer.NewPrinter(&format)
248+
p.SetResourceOutput(&buf)
249+
250+
org := "planetscale"
251+
db := "planetscale"
252+
branch := "development"
253+
254+
res := &ps.PostgresBranch{Name: branch}
255+
256+
svc := &mock.PostgresBranchesService{
257+
CreateFn: func(ctx context.Context, req *ps.CreatePostgresBranchRequest) (*ps.PostgresBranch, error) {
258+
c.Assert(req.Name, qt.Equals, branch)
259+
c.Assert(req.Database, qt.Equals, db)
260+
c.Assert(req.Region, qt.Equals, "us-east")
261+
c.Assert(req.Organization, qt.Equals, org)
262+
c.Assert(req.MajorVersion, qt.Equals, "17")
263+
264+
return res, nil
265+
},
266+
}
267+
268+
dbSvc := &mock.DatabaseService{
269+
GetFn: func(ctx context.Context, req *ps.GetDatabaseRequest) (*ps.Database, error) {
270+
c.Assert(req.Database, qt.Equals, db)
271+
c.Assert(req.Organization, qt.Equals, org)
272+
return &ps.Database{Kind: "postgresql"}, nil
273+
},
274+
}
275+
276+
ch := &cmdutil.Helper{
277+
Printer: p,
278+
Config: &config.Config{
279+
Organization: org,
280+
},
281+
Client: func() (*ps.Client, error) {
282+
return &ps.Client{
283+
PostgresBranches: svc,
284+
Databases: dbSvc,
285+
}, nil
286+
},
287+
}
288+
289+
cmd := CreateCmd(ch)
290+
cmd.SetArgs([]string{db, branch, "--region", "us-east", "--major-version", "17"})
291+
err := cmd.Execute()
292+
293+
c.Assert(err, qt.IsNil)
294+
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
295+
c.Assert(buf.String(), qt.JSONEquals, res)
296+
}
297+
242298
func TestBranch_CreateCmd_ServiceTokenAuthError(t *testing.T) {
243299
c := qt.New(t)
244300

internal/cmd/database/create.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
1919
createReq := &ps.CreateDatabaseRequest{}
2020

2121
var flags struct {
22-
clusterSize string
23-
engine string
24-
wait bool
25-
replicas *int
22+
clusterSize string
23+
engine string
24+
wait bool
25+
replicas *int
26+
majorVersion string
2627
}
2728

2829
cmd := &cobra.Command{
@@ -46,6 +47,10 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
4647
createReq.Replicas = flags.replicas
4748
}
4849

50+
if flags.majorVersion != "" {
51+
createReq.MajorVersion = flags.majorVersion
52+
}
53+
4954
client, err := ch.Client()
5055
if err != nil {
5156
return err
@@ -101,6 +106,13 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
101106
}, cobra.ShellCompDirectiveNoFileComp
102107
})
103108

109+
cmd.Flags().StringVar(&flags.majorVersion, "major-version", "", "For PostgreSQL databases, the PostgreSQL major version to use for the database. Defaults to the latest available major version.")
110+
cmd.RegisterFlagCompletionFunc("major-version", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
111+
return []cobra.Completion{
112+
cobra.CompletionWithDesc("17", "PostgreSQL 17"),
113+
}, cobra.ShellCompDirectiveNoFileComp
114+
})
115+
104116
cmd.RegisterFlagCompletionFunc("region", func(cmd *cobra.Command, args []string, toComplete string) ([]cobra.Completion, cobra.ShellCompDirective) {
105117
return cmdutil.RegionsCompletionFunc(ch, cmd, args, toComplete)
106118
})

internal/cmd/database/create_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,57 @@ func TestDatabase_CreateCmdWithReplicas(t *testing.T) {
227227
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
228228
c.Assert(buf.String(), qt.JSONEquals, res)
229229
}
230+
231+
func TestDatabase_CreateCmdPostgresWithMajorVersion(t *testing.T) {
232+
c := qt.New(t)
233+
234+
var buf bytes.Buffer
235+
format := printer.JSON
236+
p := printer.NewPrinter(&format)
237+
p.SetResourceOutput(&buf)
238+
239+
org := "planetscale"
240+
db := "planetscale"
241+
242+
res := &ps.Database{Name: "foo"}
243+
244+
svc := &mock.DatabaseService{
245+
CreateFn: func(ctx context.Context, req *ps.CreateDatabaseRequest) (*ps.Database, error) {
246+
c.Assert(req.Organization, qt.Equals, org)
247+
c.Assert(req.Name, qt.Equals, db)
248+
c.Assert(req.Region, qt.Equals, "us-east")
249+
c.Assert(req.Kind, qt.Equals, ps.DatabaseEnginePostgres)
250+
c.Assert(req.MajorVersion, qt.Equals, "17")
251+
252+
return res, nil
253+
},
254+
}
255+
256+
ch := &cmdutil.Helper{
257+
Printer: p,
258+
Config: &config.Config{
259+
Organization: org,
260+
},
261+
Client: func() (*ps.Client, error) {
262+
return &ps.Client{
263+
Databases: svc,
264+
Organizations: &mock.OrganizationsService{
265+
GetFn: func(ctx context.Context, request *ps.GetOrganizationRequest) (*ps.Organization, error) {
266+
return &ps.Organization{
267+
RemainingFreeDatabases: 1,
268+
Name: request.Organization,
269+
}, nil
270+
},
271+
},
272+
}, nil
273+
},
274+
}
275+
276+
cmd := CreateCmd(ch)
277+
cmd.SetArgs([]string{db, "--region", "us-east", "--engine", "postgresql", "--major-version", "17"})
278+
err := cmd.Execute()
279+
280+
c.Assert(err, qt.IsNil)
281+
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
282+
c.Assert(buf.String(), qt.JSONEquals, res)
283+
}

0 commit comments

Comments
 (0)