Skip to content

Commit a553e92

Browse files
Merge pull request #1164 from planetscale/auto-apply-create
Keep --auto-apply flag as deprecated
2 parents f21f041 + 304627d commit a553e92

File tree

2 files changed

+168
-4
lines changed

2 files changed

+168
-4
lines changed

internal/cmd/deployrequest/edit.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func EditCmd(ch *cmdutil.Helper) *cobra.Command {
1616
var flags struct {
1717
enable_auto_apply bool
1818
disable_auto_apply bool
19+
autoApply string // deprecated
1920
}
2021

2122
cmd := &cobra.Command{
@@ -41,15 +42,33 @@ func EditCmd(ch *cmdutil.Helper) *cobra.Command {
4142
return fmt.Errorf("cannot use both --enable-auto-apply and --disable-auto-apply flags together")
4243
}
4344

44-
if !flags.enable_auto_apply && !flags.disable_auto_apply {
45-
return fmt.Errorf("must specify either --enable-auto-apply or --disable-auto-apply")
45+
hasNewFlags := flags.enable_auto_apply || flags.disable_auto_apply
46+
hasDeprecatedFlag := flags.autoApply != ""
47+
48+
if !hasNewFlags && !hasDeprecatedFlag {
49+
return fmt.Errorf("must specify either --enable-auto-apply, --disable-auto-apply, or --auto-apply")
50+
}
51+
52+
if hasDeprecatedFlag {
53+
switch flags.autoApply {
54+
case "enable", "disable":
55+
default:
56+
return fmt.Errorf("--auto-apply accepts only \"enable\" or \"disable\" but got %q", flags.autoApply)
57+
}
58+
}
59+
60+
var enable bool
61+
if hasNewFlags {
62+
enable = flags.enable_auto_apply
63+
} else {
64+
enable = flags.autoApply == "enable"
4665
}
4766

4867
dr, err := client.DeployRequests.AutoApplyDeploy(ctx, &planetscale.AutoApplyDeployRequestRequest{
4968
Organization: ch.Config.Organization,
5069
Database: database,
5170
Number: n,
52-
Enable: flags.enable_auto_apply,
71+
Enable: enable,
5372
})
5473
if err != nil {
5574
switch cmdutil.ErrCode(err) {
@@ -74,5 +93,9 @@ func EditCmd(ch *cmdutil.Helper) *cobra.Command {
7493

7594
cmd.Flags().BoolVar(&flags.enable_auto_apply, "enable-auto-apply", false, "Enable auto-apply. The deploy request will automatically swap over to the new schema once ready.")
7695
cmd.Flags().BoolVar(&flags.disable_auto_apply, "disable-auto-apply", false, "Disable auto-apply. The deploy request will wait for your confirmation before swapping to the new schema. Use 'deploy-request apply' to apply the changes manually.")
96+
97+
cmd.Flags().StringVar(&flags.autoApply, "auto-apply", "", "Update the auto apply setting for a deploy request. Possible values: [enable,disable]")
98+
cmd.Flags().MarkDeprecated("auto-apply", "use --enable-auto-apply or --disable-auto-apply instead")
99+
77100
return cmd
78101
}

internal/cmd/deployrequest/edit_test.go

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestDeployRequest_EditCmdNoFlags(t *testing.T) {
135135
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10)})
136136
err := cmd.Execute()
137137

138-
c.Assert(err, qt.ErrorMatches, "must specify either --enable-auto-apply or --disable-auto-apply")
138+
c.Assert(err, qt.ErrorMatches, "must specify either --enable-auto-apply, --disable-auto-apply, or --auto-apply")
139139
}
140140

141141
func TestDeployRequest_EditCmdBothFlags(t *testing.T) {
@@ -166,3 +166,144 @@ func TestDeployRequest_EditCmdBothFlags(t *testing.T) {
166166

167167
c.Assert(err, qt.ErrorMatches, "cannot use both --enable-auto-apply and --disable-auto-apply flags together")
168168
}
169+
170+
func TestDeployRequest_EditCmdDeprecatedAutoApplyEnable(t *testing.T) {
171+
c := qt.New(t)
172+
173+
var buf bytes.Buffer
174+
format := printer.JSON
175+
p := printer.NewPrinter(&format)
176+
p.SetResourceOutput(&buf)
177+
178+
org := "planetscale"
179+
db := "planetscale"
180+
number := uint64(10)
181+
enable := true
182+
183+
svc := &mock.DeployRequestsService{
184+
AutoApplyFn: func(ctx context.Context, req *ps.AutoApplyDeployRequestRequest) (*ps.DeployRequest, error) {
185+
c.Assert(req.Number, qt.Equals, number)
186+
c.Assert(req.Database, qt.Equals, db)
187+
c.Assert(req.Organization, qt.Equals, org)
188+
c.Assert(req.Enable, qt.Equals, enable)
189+
190+
return &ps.DeployRequest{Number: number}, nil
191+
},
192+
}
193+
194+
ch := &cmdutil.Helper{
195+
Printer: p,
196+
Config: &config.Config{
197+
Organization: org,
198+
},
199+
Client: func() (*ps.Client, error) {
200+
return &ps.Client{
201+
DeployRequests: svc,
202+
}, nil
203+
},
204+
}
205+
206+
cmd := EditCmd(ch)
207+
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10), "--auto-apply=enable"})
208+
err := cmd.Execute()
209+
210+
c.Assert(err, qt.IsNil)
211+
c.Assert(svc.AutoApplyFnInvoked, qt.IsTrue)
212+
213+
res := &ps.DeployRequest{Number: number}
214+
c.Assert(buf.String(), qt.JSONEquals, res)
215+
}
216+
217+
func TestDeployRequest_EditCmdDeprecatedAutoApplyDisable(t *testing.T) {
218+
c := qt.New(t)
219+
220+
var buf bytes.Buffer
221+
format := printer.JSON
222+
p := printer.NewPrinter(&format)
223+
p.SetResourceOutput(&buf)
224+
225+
org := "planetscale"
226+
db := "planetscale"
227+
number := uint64(10)
228+
enable := false
229+
230+
svc := &mock.DeployRequestsService{
231+
AutoApplyFn: func(ctx context.Context, req *ps.AutoApplyDeployRequestRequest) (*ps.DeployRequest, error) {
232+
c.Assert(req.Number, qt.Equals, number)
233+
c.Assert(req.Database, qt.Equals, db)
234+
c.Assert(req.Organization, qt.Equals, org)
235+
c.Assert(req.Enable, qt.Equals, enable)
236+
237+
return &ps.DeployRequest{Number: number}, nil
238+
},
239+
}
240+
241+
ch := &cmdutil.Helper{
242+
Printer: p,
243+
Config: &config.Config{
244+
Organization: org,
245+
},
246+
Client: func() (*ps.Client, error) {
247+
return &ps.Client{
248+
DeployRequests: svc,
249+
}, nil
250+
},
251+
}
252+
253+
cmd := EditCmd(ch)
254+
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10), "--auto-apply=disable"})
255+
err := cmd.Execute()
256+
257+
c.Assert(err, qt.IsNil)
258+
c.Assert(svc.AutoApplyFnInvoked, qt.IsTrue)
259+
260+
res := &ps.DeployRequest{Number: number}
261+
c.Assert(buf.String(), qt.JSONEquals, res)
262+
}
263+
264+
func TestDeployRequest_EditCmdMixedFlags(t *testing.T) {
265+
c := qt.New(t)
266+
267+
var buf bytes.Buffer
268+
format := printer.JSON
269+
p := printer.NewPrinter(&format)
270+
p.SetResourceOutput(&buf)
271+
272+
org := "planetscale"
273+
db := "planetscale"
274+
number := uint64(10)
275+
enable := true // Should use --enable-auto-apply and ignore --auto-apply
276+
277+
svc := &mock.DeployRequestsService{
278+
AutoApplyFn: func(ctx context.Context, req *ps.AutoApplyDeployRequestRequest) (*ps.DeployRequest, error) {
279+
c.Assert(req.Number, qt.Equals, number)
280+
c.Assert(req.Database, qt.Equals, db)
281+
c.Assert(req.Organization, qt.Equals, org)
282+
c.Assert(req.Enable, qt.Equals, enable)
283+
284+
return &ps.DeployRequest{Number: number}, nil
285+
},
286+
}
287+
288+
ch := &cmdutil.Helper{
289+
Printer: p,
290+
Config: &config.Config{
291+
Organization: org,
292+
},
293+
Client: func() (*ps.Client, error) {
294+
return &ps.Client{
295+
DeployRequests: svc,
296+
}, nil
297+
},
298+
}
299+
300+
cmd := EditCmd(ch)
301+
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10), "--enable-auto-apply", "--auto-apply=disable"})
302+
err := cmd.Execute()
303+
304+
c.Assert(err, qt.IsNil)
305+
c.Assert(svc.AutoApplyFnInvoked, qt.IsTrue)
306+
307+
res := &ps.DeployRequest{Number: number}
308+
c.Assert(buf.String(), qt.JSONEquals, res)
309+
}

0 commit comments

Comments
 (0)