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..4da2906f 100644 --- a/internal/provider/branch_resource_test.go +++ b/internal/provider/branch_resource_test.go @@ -59,6 +59,56 @@ func TestAccBranchResource(t *testing.T) { }) } +// TestAccBranchResource_WithSeedData tests the creation of a branch with the seed data. +func TestAccBranchResource_SeedData(t *testing.T) { + dbName := acctest.RandomWithPrefix("testacc-branch-db") + branchName := acctest.RandomWithPrefix("branch") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Initial creation with required fields + { + Config: testAccBranchResourceConfigTemplate(map[string]string{ + "organization": testAccOrg, + "database": dbName, + "name": branchName, + "parent_branch": "main", + "seed_data": "last_successful_backup", + }), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("planetscale_branch.test", "name", branchName), + resource.TestCheckResourceAttr("planetscale_branch.test", "parent_branch", "main"), + resource.TestCheckResourceAttr("planetscale_branch.test", "production", "true"), + ), + }, + // ImportState testing + { + ResourceName: "planetscale_branch.test", + ImportStateId: fmt.Sprintf("%s,%s,%s", testAccOrg, dbName, branchName), + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"updated_at", "schema_last_updated_at"}, + }, + // Update in-place to development 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"), + ), + }, + }, + }) +} + // TestAccBranchResource_ProductionCreate tests the creation of a branch with // the production flag set to true. func TestAccBranchResource_ProductionCreate(t *testing.T) { @@ -149,6 +199,7 @@ resource "planetscale_branch" "test" { name = "{{.name}}" parent_branch = "{{.parent_branch}}" {{if .production}}production = {{.production}}{{end}} + {{if .seed_data}}seed_data = "{{.seed_data}}"{{end}} } ` t := template.Must(template.New("config").Parse(tmpl))