Skip to content

Commit cd52bcd

Browse files
committed
demote if production = false
1 parent 2265315 commit cd52bcd

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

internal/provider/branch_resource.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,30 @@ func (r *branchResource) Create(ctx context.Context, req resource.CreateRequest,
189189
return
190190
}
191191

192-
// After branch is ready, check if we need to promote it to a production branch
193-
if !data.Production.IsNull() && data.Production.ValueBool() {
194-
res, err := r.client.PromoteBranch(ctx, org.ValueString(), database.ValueString(), name.ValueString())
195-
if err != nil {
196-
resp.Diagnostics.AddError(
197-
"Unable to promote branch during creation",
198-
fmt.Sprintf("Branch %s could not be promoted to production: %s", name.ValueString(), err),
199-
)
200-
return
192+
// After the branch is ready, check if we need to promote or demote it
193+
if !data.Production.IsNull() {
194+
if !branch.Production && data.Production.ValueBool() {
195+
res, err := r.client.PromoteBranch(ctx, org.ValueString(), database.ValueString(), name.ValueString())
196+
if err != nil {
197+
resp.Diagnostics.AddError(
198+
"Unable to promote branch during creation",
199+
fmt.Sprintf("Branch %s could not be promoted to production: %s", name.ValueString(), err),
200+
)
201+
return
202+
}
203+
branch = res.Branch
204+
}
205+
if branch.Production && !data.Production.ValueBool() {
206+
res, err := r.client.DemoteBranch(ctx, org.ValueString(), database.ValueString(), name.ValueString())
207+
if err != nil {
208+
resp.Diagnostics.AddError(
209+
"Unable to demote branch during creation",
210+
fmt.Sprintf("Branch %s could not be demoted to production: %s", name.ValueString(), err),
211+
)
212+
return
213+
}
214+
branch = res.Branch
201215
}
202-
branch = res.Branch
203216
}
204217

205218
data = branchResourceFromClient(ctx, &branch, data.Organization, data.Database, data.SeedData, resp.Diagnostics)

internal/provider/branch_resource_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ func TestAccBranchResource(t *testing.T) {
5555
resource.TestCheckResourceAttr("planetscale_branch.test", "production", "true"),
5656
),
5757
},
58+
// Update in-place to production branch
59+
{
60+
Config: testAccBranchResourceConfigTemplate(map[string]string{
61+
"organization": testAccOrg,
62+
"database": dbName,
63+
"name": branchName,
64+
"parent_branch": "main",
65+
"production": "false",
66+
}),
67+
ConfigPlanChecks: checkExpectUpdate("planetscale_branch.test"),
68+
Check: resource.ComposeAggregateTestCheckFunc(
69+
resource.TestCheckResourceAttr("planetscale_branch.test", "production", "false"),
70+
),
71+
},
5872
},
5973
})
6074
}

0 commit comments

Comments
 (0)