From cd52bcdd09488a12bdd40bac9a7b98fa5a565e61 Mon Sep 17 00:00:00 2001 From: Tomohiko Ozawa Date: Wed, 28 May 2025 16:54:28 +0900 Subject: [PATCH] demote if production = false --- internal/provider/branch_resource.go | 33 ++++++++++++++++------- internal/provider/branch_resource_test.go | 14 ++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/internal/provider/branch_resource.go b/internal/provider/branch_resource.go index 604f1355..0630342a 100644 --- a/internal/provider/branch_resource.go +++ b/internal/provider/branch_resource.go @@ -189,17 +189,30 @@ func (r *branchResource) Create(ctx context.Context, req resource.CreateRequest, return } - // After branch is ready, check if we need to promote it to a production branch - if !data.Production.IsNull() && data.Production.ValueBool() { - res, err := r.client.PromoteBranch(ctx, org.ValueString(), database.ValueString(), name.ValueString()) - if err != nil { - resp.Diagnostics.AddError( - "Unable to promote branch during creation", - fmt.Sprintf("Branch %s could not be promoted to production: %s", name.ValueString(), err), - ) - return + // After the branch is ready, check if we need to promote or demote it + if !data.Production.IsNull() { + if !branch.Production && data.Production.ValueBool() { + res, err := r.client.PromoteBranch(ctx, org.ValueString(), database.ValueString(), name.ValueString()) + if err != nil { + resp.Diagnostics.AddError( + "Unable to promote branch during creation", + fmt.Sprintf("Branch %s could not be promoted to production: %s", name.ValueString(), err), + ) + return + } + branch = res.Branch + } + if branch.Production && !data.Production.ValueBool() { + res, err := r.client.DemoteBranch(ctx, org.ValueString(), database.ValueString(), name.ValueString()) + if err != nil { + resp.Diagnostics.AddError( + "Unable to demote branch during creation", + fmt.Sprintf("Branch %s could not be demoted to production: %s", name.ValueString(), err), + ) + return + } + branch = res.Branch } - branch = res.Branch } data = branchResourceFromClient(ctx, &branch, data.Organization, data.Database, data.SeedData, resp.Diagnostics) diff --git a/internal/provider/branch_resource_test.go b/internal/provider/branch_resource_test.go index 0a9a8ce1..6ff1ba60 100644 --- a/internal/provider/branch_resource_test.go +++ b/internal/provider/branch_resource_test.go @@ -55,6 +55,20 @@ func TestAccBranchResource(t *testing.T) { resource.TestCheckResourceAttr("planetscale_branch.test", "production", "true"), ), }, + // Update in-place to production branch + { + Config: testAccBranchResourceConfigTemplate(map[string]string{ + "organization": testAccOrg, + "database": dbName, + "name": branchName, + "parent_branch": "main", + "production": "false", + }), + ConfigPlanChecks: checkExpectUpdate("planetscale_branch.test"), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("planetscale_branch.test", "production", "false"), + ), + }, }, }) }