Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions internal/provider/branch_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions internal/provider/branch_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
})
}
Expand Down
Loading