Skip to content

Commit a77666d

Browse files
authored
Add cluster-size to backup restoration commands (#1023)
* Add cluster size to restore commands * Add cluster size to backup restore command * Update tests * Update planetscale-go * Require restore and cluster-size flags to be present together * Make PS-20 flag required for backup restoration * Remove deprecated flag
1 parent 0c912cf commit a77666d

File tree

7 files changed

+117
-9
lines changed

7 files changed

+117
-9
lines changed

go.mod

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

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjL
142142
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
143143
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e h1:MZ8D+Z3m2vvqGZLvoQfpaGg/j1fNDr4j03s3PRz4rVY=
144144
github.com/planetscale/noglog v0.2.1-0.20210421230640-bea75fcd2e8e/go.mod h1:hwAsSPQdvPa3WcfKfzTXxtEq/HlqwLjQasfO6QbGo4Q=
145-
github.com/planetscale/planetscale-go v0.131.0 h1:ojr8A1yrkJMRS3oUcJH58mv5efRQwCV4MFFQYJnLs9s=
146-
github.com/planetscale/planetscale-go v0.131.0/go.mod h1:VE4Vn8+qWV8KlfcasKurXByncyqaL4NIA68DGep+AFg=
145+
github.com/planetscale/planetscale-go v0.132.0 h1:SoQczeRfMUwpNSlZwsht+T+x4BlvOCJZA3+xW52In6c=
146+
github.com/planetscale/planetscale-go v0.132.0/go.mod h1:Ht/CUQOhz22XoXd5+ouiQAFfPWSyiGy5dATcyyoXcq0=
147147
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7 h1:dxdoFKWVDlV1gq8UQC8NWCofLjCEjEHw47gfeojgs28=
148148
github.com/planetscale/psdb v0.0.0-20240109164348-6848e728f6e7/go.mod h1:WZmi4gw3rOK+ryd1inGxgfKwoFV04O7xBCqzWzv0/0U=
149149
github.com/planetscale/psdbproxy v0.0.0-20250117221522-0c8e2b0e36e6 h1:/Ox1ZTAdk+soSngzzKoJh5voOzptrpPrux11o30rIaw=

internal/cmd/backup/restore.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
)
1414

1515
func RestoreCmd(ch *cmdutil.Helper) *cobra.Command {
16+
var clusterSize string
17+
1618
cmd := &cobra.Command{
1719
Use: "restore <database> <branch> <backup>",
1820
Short: "Restore a backup to a new branch",
@@ -35,6 +37,7 @@ func RestoreCmd(ch *cmdutil.Helper) *cobra.Command {
3537
Database: database,
3638
Name: branchName,
3739
BackupID: backup,
40+
ClusterSize: clusterSize,
3841
})
3942
if err != nil {
4043
return cmdutil.HandleError(err)
@@ -45,5 +48,11 @@ func RestoreCmd(ch *cmdutil.Helper) *cobra.Command {
4548
},
4649
}
4750

51+
cmd.Flags().StringVar(&clusterSize, "cluster-size", "PS-10", "Cluster size for restored backup branch. Use `pscale size cluster list` to see the valid sizes.")
52+
cmd.MarkFlagRequired("cluster-size")
53+
cmd.RegisterFlagCompletionFunc("cluster-size", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
54+
return cmdutil.ClusterSizesCompletionFunc(ch, cmd, args, toComplete)
55+
})
56+
4857
return cmd
4958
}

internal/cmd/backup/restore_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestBackup_RestoreCmd(t *testing.T) {
3535
c.Assert(req.Database, qt.Equals, db)
3636
c.Assert(req.Name, qt.Equals, branch)
3737
c.Assert(req.BackupID, qt.Equals, backup)
38+
c.Assert(req.ClusterSize, qt.Equals, "PS-20")
3839
return res, nil
3940
},
4041
}
@@ -52,7 +53,7 @@ func TestBackup_RestoreCmd(t *testing.T) {
5253
}
5354

5455
cmd := RestoreCmd(ch)
55-
cmd.SetArgs([]string{db, branch, backup})
56+
cmd.SetArgs([]string{db, branch, backup, "--cluster-size", "PS-20"})
5657
err := cmd.Execute()
5758

5859
c.Assert(err, qt.IsNil)

internal/cmd/branch/create.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,23 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
119119
}
120120

121121
cmd.Flags().StringVar(&createReq.ParentBranch, "from", "", "Parent branch to create the new branch from. Cannot be used with --restore")
122-
cmd.Flags().StringVar(&createReq.Region, "region", "", "Region for the branch. Can not be used with --restore")
123-
cmd.Flags().StringVar(&createReq.BackupID, "restore", "", "Backup to restore into the branch. Backup can only be restored to its original region")
122+
cmd.Flags().StringVar(&createReq.Region, "region", "", "Region for the branch to be created in.")
123+
cmd.Flags().StringVar(&createReq.BackupID, "restore", "", "ID of Backup to restore into branch.")
124+
cmd.Flags().StringVar(&createReq.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.")
124125
cmd.Flags().BoolVar(&flags.dataBranching, "seed-data", false, "Add seed data using the Data Branching™ feature")
125126
cmd.Flags().BoolVar(&flags.wait, "wait", false, "Wait until the branch is ready")
126127
cmd.MarkFlagsMutuallyExclusive("from", "restore")
127-
cmd.MarkFlagsMutuallyExclusive("region", "restore")
128128
cmd.MarkFlagsMutuallyExclusive("restore", "seed-data")
129+
cmd.MarkFlagsRequiredTogether("restore", "cluster-size")
129130

130131
cmd.RegisterFlagCompletionFunc("region", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
131132
return cmdutil.RegionsCompletionFunc(ch, cmd, args, toComplete)
132133
})
133134

135+
cmd.RegisterFlagCompletionFunc("cluster-size", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
136+
return cmdutil.ClusterSizesCompletionFunc(ch, cmd, args, toComplete)
137+
})
138+
134139
return cmd
135140
}
136141

internal/cmd/branch/create_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,98 @@ func TestBranch_CreateCmd(t *testing.T) {
5959
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
6060
c.Assert(buf.String(), qt.JSONEquals, res)
6161
}
62+
63+
func TestBranch_CreateCmdWithRestore(t *testing.T) {
64+
c := qt.New(t)
65+
66+
var buf bytes.Buffer
67+
format := printer.JSON
68+
p := printer.NewPrinter(&format)
69+
p.SetResourceOutput(&buf)
70+
71+
org := "planetscale"
72+
db := "planetscale"
73+
branch := "development"
74+
75+
res := &ps.DatabaseBranch{Name: branch}
76+
77+
svc := &mock.DatabaseBranchesService{
78+
CreateFn: func(ctx context.Context, req *ps.CreateDatabaseBranchRequest) (*ps.DatabaseBranch, error) {
79+
c.Assert(req.Name, qt.Equals, branch)
80+
c.Assert(req.Database, qt.Equals, db)
81+
c.Assert(req.Region, qt.Equals, "us-east")
82+
c.Assert(req.Organization, qt.Equals, org)
83+
c.Assert(req.BackupID, qt.Equals, "somebackupid")
84+
c.Assert(req.ClusterSize, qt.Equals, "PS-20")
85+
86+
return res, nil
87+
},
88+
}
89+
90+
ch := &cmdutil.Helper{
91+
Printer: p,
92+
Config: &config.Config{
93+
Organization: org,
94+
},
95+
Client: func() (*ps.Client, error) {
96+
return &ps.Client{
97+
DatabaseBranches: svc,
98+
}, nil
99+
},
100+
}
101+
102+
cmd := CreateCmd(ch)
103+
cmd.SetArgs([]string{db, branch, "--region", "us-east", "--restore", "somebackupid", "--cluster-size", "PS-20"})
104+
err := cmd.Execute()
105+
106+
c.Assert(err, qt.IsNil)
107+
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
108+
c.Assert(buf.String(), qt.JSONEquals, res)
109+
}
110+
111+
func TestBranch_CreateCmdWithSeedData(t *testing.T) {
112+
c := qt.New(t)
113+
114+
var buf bytes.Buffer
115+
format := printer.JSON
116+
p := printer.NewPrinter(&format)
117+
p.SetResourceOutput(&buf)
118+
119+
org := "planetscale"
120+
db := "planetscale"
121+
branch := "development"
122+
123+
res := &ps.DatabaseBranch{Name: branch}
124+
125+
svc := &mock.DatabaseBranchesService{
126+
CreateFn: func(ctx context.Context, req *ps.CreateDatabaseBranchRequest) (*ps.DatabaseBranch, error) {
127+
c.Assert(req.Name, qt.Equals, branch)
128+
c.Assert(req.Database, qt.Equals, db)
129+
c.Assert(req.Region, qt.Equals, "us-east")
130+
c.Assert(req.Organization, qt.Equals, org)
131+
c.Assert(req.SeedData, qt.Equals, "last_successful_backup")
132+
133+
return res, nil
134+
},
135+
}
136+
137+
ch := &cmdutil.Helper{
138+
Printer: p,
139+
Config: &config.Config{
140+
Organization: org,
141+
},
142+
Client: func() (*ps.Client, error) {
143+
return &ps.Client{
144+
DatabaseBranches: svc,
145+
}, nil
146+
},
147+
}
148+
149+
cmd := CreateCmd(ch)
150+
cmd.SetArgs([]string{db, branch, "--region", "us-east", "--seed-data"})
151+
err := cmd.Execute()
152+
153+
c.Assert(err, qt.IsNil)
154+
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
155+
c.Assert(buf.String(), qt.JSONEquals, res)
156+
}

internal/cmd/database/create.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
6060
},
6161
}
6262

63-
cmd.Flags().StringVar(&createReq.Notes, "notes", "", "notes for the database")
64-
cmd.Flags().MarkDeprecated("notes", "is no longer available.")
6563
cmd.Flags().StringVar(&createReq.Region, "region", "", "region for the database")
6664

6765
cmd.Flags().String("cluster-size", "PS-10", "cluster size for Scaler Pro databases. Use `pscale size cluster list` to see the valid sizes.")

0 commit comments

Comments
 (0)