Skip to content

Commit f21f041

Browse files
Merge pull request #1163 from planetscale/auto-apply-create
`auto-apply` flags for create/edit deploy requests
2 parents 5179a75 + c79e5fe commit f21f041

File tree

4 files changed

+256
-9
lines changed

4 files changed

+256
-9
lines changed

internal/cmd/deployrequest/create.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
1616
into string
1717
notes string
1818
auto_delete_branch bool
19+
enable_auto_apply bool
20+
disable_auto_apply bool
1921
}
2022

2123
cmd := &cobra.Command{
@@ -32,6 +34,10 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
3234
return err
3335
}
3436

37+
if flags.enable_auto_apply && flags.disable_auto_apply {
38+
return fmt.Errorf("cannot use both --enable-auto-apply and --disable-auto-apply flags together")
39+
}
40+
3541
end := ch.Printer.PrintProgress(fmt.Sprintf("Request deploying of %s branch in %s...", printer.BoldBlue(branch), printer.BoldBlue(database)))
3642
defer end()
3743

@@ -43,6 +49,12 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
4349
Notes: flags.notes,
4450
}
4551

52+
if flags.enable_auto_apply {
53+
request.AutoCutover = true
54+
} else if flags.disable_auto_apply {
55+
request.AutoCutover = false
56+
}
57+
4658
if flags.auto_delete_branch {
4759
request.AutoDeleteBranch = true
4860
}
@@ -73,6 +85,8 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
7385
cmd.PersistentFlags().StringVar(&flags.into, "into", "", "Branch to deploy into. By default, it's the parent branch (if present) or the database's default branch.")
7486
cmd.PersistentFlags().StringVar(&flags.notes, "notes", "", "Notes to include with the deploy request.")
7587
cmd.Flags().BoolVar(&flags.auto_delete_branch, "auto-delete-branch", false, "Delete the branch after the deploy request completes.")
88+
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.")
89+
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.")
7690

7791
return cmd
7892
}

internal/cmd/deployrequest/create_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestDeployRequest_CreateCmd(t *testing.T) {
3333
c.Assert(req.Organization, qt.Equals, org)
3434
c.Assert(req.Database, qt.Equals, db)
3535
c.Assert(req.Branch, qt.Equals, branch)
36+
c.Assert(req.AutoCutover, qt.Equals, false)
3637
c.Assert(req.Notes, qt.Equals, notes)
3738
c.Assert(req.AutoDeleteBranch, qt.Equals, true)
3839
c.Assert(req.IntoBranch, qt.Equals, "", qt.Commentf("default value of the '--into' flag has changed"))
@@ -64,6 +65,129 @@ func TestDeployRequest_CreateCmd(t *testing.T) {
6465
c.Assert(buf.String(), qt.JSONEquals, res)
6566
}
6667

68+
func TestDeployRequest_CreateCmdEnableAutoApplyFlag(t *testing.T) {
69+
c := qt.New(t)
70+
71+
var buf bytes.Buffer
72+
format := printer.JSON
73+
p := printer.NewPrinter(&format)
74+
p.SetResourceOutput(&buf)
75+
76+
org := "planetscale"
77+
db := "planetscale"
78+
branch := "development"
79+
var number uint64 = 10
80+
81+
svc := &mock.DeployRequestsService{
82+
CreateFn: func(ctx context.Context, req *ps.CreateDeployRequestRequest) (*ps.DeployRequest, error) {
83+
c.Assert(req.Organization, qt.Equals, org)
84+
c.Assert(req.Database, qt.Equals, db)
85+
c.Assert(req.Branch, qt.Equals, branch)
86+
c.Assert(req.AutoCutover, qt.Equals, true)
87+
88+
return &ps.DeployRequest{Number: number}, nil
89+
},
90+
}
91+
92+
ch := &cmdutil.Helper{
93+
Printer: p,
94+
Config: &config.Config{
95+
Organization: org,
96+
},
97+
Client: func() (*ps.Client, error) {
98+
return &ps.Client{
99+
DeployRequests: svc,
100+
}, nil
101+
},
102+
}
103+
104+
cmd := CreateCmd(ch)
105+
cmd.SetArgs([]string{db, branch, "--enable-auto-apply"})
106+
err := cmd.Execute()
107+
108+
c.Assert(err, qt.IsNil)
109+
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
110+
111+
res := &ps.DeployRequest{Number: number}
112+
c.Assert(buf.String(), qt.JSONEquals, res)
113+
}
114+
115+
func TestDeployRequest_CreateCmdDisableAutoApplyFlag(t *testing.T) {
116+
c := qt.New(t)
117+
118+
var buf bytes.Buffer
119+
format := printer.JSON
120+
p := printer.NewPrinter(&format)
121+
p.SetResourceOutput(&buf)
122+
123+
org := "planetscale"
124+
db := "planetscale"
125+
branch := "development"
126+
var number uint64 = 10
127+
128+
svc := &mock.DeployRequestsService{
129+
CreateFn: func(ctx context.Context, req *ps.CreateDeployRequestRequest) (*ps.DeployRequest, error) {
130+
c.Assert(req.Organization, qt.Equals, org)
131+
c.Assert(req.Database, qt.Equals, db)
132+
c.Assert(req.Branch, qt.Equals, branch)
133+
c.Assert(req.AutoCutover, qt.Equals, false)
134+
135+
return &ps.DeployRequest{Number: number}, nil
136+
},
137+
}
138+
139+
ch := &cmdutil.Helper{
140+
Printer: p,
141+
Config: &config.Config{
142+
Organization: org,
143+
},
144+
Client: func() (*ps.Client, error) {
145+
return &ps.Client{
146+
DeployRequests: svc,
147+
}, nil
148+
},
149+
}
150+
151+
cmd := CreateCmd(ch)
152+
cmd.SetArgs([]string{db, branch, "--disable-auto-apply"})
153+
err := cmd.Execute()
154+
155+
c.Assert(err, qt.IsNil)
156+
c.Assert(svc.CreateFnInvoked, qt.IsTrue)
157+
158+
res := &ps.DeployRequest{Number: number}
159+
c.Assert(buf.String(), qt.JSONEquals, res)
160+
}
161+
162+
func TestDeployRequest_CreateCmdBothAutoApplyFlags(t *testing.T) {
163+
c := qt.New(t)
164+
165+
var buf bytes.Buffer
166+
format := printer.JSON
167+
p := printer.NewPrinter(&format)
168+
p.SetResourceOutput(&buf)
169+
170+
org := "planetscale"
171+
db := "planetscale"
172+
branch := "development"
173+
174+
ch := &cmdutil.Helper{
175+
Printer: p,
176+
Config: &config.Config{
177+
Organization: org,
178+
},
179+
Client: func() (*ps.Client, error) {
180+
return &ps.Client{}, nil
181+
},
182+
}
183+
184+
cmd := CreateCmd(ch)
185+
cmd.SetArgs([]string{db, branch, "--enable-auto-apply", "--disable-auto-apply"})
186+
err := cmd.Execute()
187+
188+
c.Assert(err, qt.ErrorMatches, "cannot use both --enable-auto-apply and --disable-auto-apply flags together")
189+
}
190+
67191
func TestDeployRequest_CreateCmdIntoFlag(t *testing.T) {
68192
c := qt.New(t)
69193

internal/cmd/deployrequest/edit.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
// EditCmd is the command for editing preferences on deploy requests.
1515
func EditCmd(ch *cmdutil.Helper) *cobra.Command {
1616
var flags struct {
17-
autoApply string
17+
enable_auto_apply bool
18+
disable_auto_apply bool
1819
}
1920

2021
cmd := &cobra.Command{
@@ -36,17 +37,19 @@ func EditCmd(ch *cmdutil.Helper) *cobra.Command {
3637
return fmt.Errorf("the argument <number> is invalid: %s", err)
3738
}
3839

39-
switch flags.autoApply {
40-
case "enable", "disable":
41-
default:
42-
return fmt.Errorf("--auto-apply accepts only \"enable\" or \"disable\" but got %q", flags.autoApply)
40+
if flags.enable_auto_apply && flags.disable_auto_apply {
41+
return fmt.Errorf("cannot use both --enable-auto-apply and --disable-auto-apply flags together")
42+
}
43+
44+
if !flags.enable_auto_apply && !flags.disable_auto_apply {
45+
return fmt.Errorf("must specify either --enable-auto-apply or --disable-auto-apply")
4346
}
4447

4548
dr, err := client.DeployRequests.AutoApplyDeploy(ctx, &planetscale.AutoApplyDeployRequestRequest{
4649
Organization: ch.Config.Organization,
4750
Database: database,
4851
Number: n,
49-
Enable: flags.autoApply == "enable",
52+
Enable: flags.enable_auto_apply,
5053
})
5154
if err != nil {
5255
switch cmdutil.ErrCode(err) {
@@ -69,6 +72,7 @@ func EditCmd(ch *cmdutil.Helper) *cobra.Command {
6972
},
7073
}
7174

72-
cmd.Flags().StringVar(&flags.autoApply, "auto-apply", "enable", "Update the auto apply setting for a deploy request. Possible values: [enable,disable]")
75+
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.")
76+
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.")
7377
return cmd
7478
}

internal/cmd/deployrequest/edit_test.go

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
ps "github.com/planetscale/planetscale-go/planetscale"
1616
)
1717

18-
func TestDeployRequest_EditCmd(t *testing.T) {
18+
func TestDeployRequest_EditCmdEnableAutoApply(t *testing.T) {
1919
c := qt.New(t)
2020

2121
var buf bytes.Buffer
@@ -52,7 +52,54 @@ func TestDeployRequest_EditCmd(t *testing.T) {
5252
}
5353

5454
cmd := EditCmd(ch)
55-
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10)})
55+
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10), "--enable-auto-apply"})
56+
err := cmd.Execute()
57+
58+
c.Assert(err, qt.IsNil)
59+
c.Assert(svc.AutoApplyFnInvoked, qt.IsTrue)
60+
61+
res := &ps.DeployRequest{Number: number}
62+
c.Assert(buf.String(), qt.JSONEquals, res)
63+
}
64+
65+
func TestDeployRequest_EditCmdDisableAutoApply(t *testing.T) {
66+
c := qt.New(t)
67+
68+
var buf bytes.Buffer
69+
format := printer.JSON
70+
p := printer.NewPrinter(&format)
71+
p.SetResourceOutput(&buf)
72+
73+
org := "planetscale"
74+
db := "planetscale"
75+
number := uint64(10)
76+
enable := false
77+
78+
svc := &mock.DeployRequestsService{
79+
AutoApplyFn: func(ctx context.Context, req *ps.AutoApplyDeployRequestRequest) (*ps.DeployRequest, error) {
80+
c.Assert(req.Number, qt.Equals, number)
81+
c.Assert(req.Database, qt.Equals, db)
82+
c.Assert(req.Organization, qt.Equals, org)
83+
c.Assert(req.Enable, qt.Equals, enable)
84+
85+
return &ps.DeployRequest{Number: number}, nil
86+
},
87+
}
88+
89+
ch := &cmdutil.Helper{
90+
Printer: p,
91+
Config: &config.Config{
92+
Organization: org,
93+
},
94+
Client: func() (*ps.Client, error) {
95+
return &ps.Client{
96+
DeployRequests: svc,
97+
}, nil
98+
},
99+
}
100+
101+
cmd := EditCmd(ch)
102+
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10), "--disable-auto-apply"})
56103
err := cmd.Execute()
57104

58105
c.Assert(err, qt.IsNil)
@@ -61,3 +108,61 @@ func TestDeployRequest_EditCmd(t *testing.T) {
61108
res := &ps.DeployRequest{Number: number}
62109
c.Assert(buf.String(), qt.JSONEquals, res)
63110
}
111+
112+
func TestDeployRequest_EditCmdNoFlags(t *testing.T) {
113+
c := qt.New(t)
114+
115+
var buf bytes.Buffer
116+
format := printer.JSON
117+
p := printer.NewPrinter(&format)
118+
p.SetResourceOutput(&buf)
119+
120+
org := "planetscale"
121+
db := "planetscale"
122+
number := uint64(10)
123+
124+
ch := &cmdutil.Helper{
125+
Printer: p,
126+
Config: &config.Config{
127+
Organization: org,
128+
},
129+
Client: func() (*ps.Client, error) {
130+
return &ps.Client{}, nil
131+
},
132+
}
133+
134+
cmd := EditCmd(ch)
135+
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10)})
136+
err := cmd.Execute()
137+
138+
c.Assert(err, qt.ErrorMatches, "must specify either --enable-auto-apply or --disable-auto-apply")
139+
}
140+
141+
func TestDeployRequest_EditCmdBothFlags(t *testing.T) {
142+
c := qt.New(t)
143+
144+
var buf bytes.Buffer
145+
format := printer.JSON
146+
p := printer.NewPrinter(&format)
147+
p.SetResourceOutput(&buf)
148+
149+
org := "planetscale"
150+
db := "planetscale"
151+
number := uint64(10)
152+
153+
ch := &cmdutil.Helper{
154+
Printer: p,
155+
Config: &config.Config{
156+
Organization: org,
157+
},
158+
Client: func() (*ps.Client, error) {
159+
return &ps.Client{}, nil
160+
},
161+
}
162+
163+
cmd := EditCmd(ch)
164+
cmd.SetArgs([]string{db, strconv.FormatUint(number, 10), "--enable-auto-apply", "--disable-auto-apply"})
165+
err := cmd.Execute()
166+
167+
c.Assert(err, qt.ErrorMatches, "cannot use both --enable-auto-apply and --disable-auto-apply flags together")
168+
}

0 commit comments

Comments
 (0)