Skip to content

fix: default branch creation#338

Merged
savme merged 4 commits into
supabase:mainfrom
nathanparekh:fix/create-default-branch
Jul 10, 2026
Merged

fix: default branch creation#338
savme merged 4 commits into
supabase:mainfrom
nathanparekh:fix/create-default-branch

Conversation

@nathanparekh

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bugfix

What is the current behavior?

The existing createBranch function has a section:

resp, err := client.V1ListAllBranches(ctx, plan.ParentProjectRef.ValueString())
//...
if resp.StatusCode == http.StatusUnprocessableEntity {
 		httpResp, err := client.V1CreateABranchWithResponse(ctx, plan.ParentProjectRef.ValueString(), api.CreateBranchBody{
			BranchName: "Production",
		})
        //...
}

This seems to be intented to create a default "Production" branch if no branches exist for the Supabase project. However, it seems as if the ListAllBranches call returns a StatusOK with an empty list of branches instead of a StatusUnprocessableEntity, which causes this never to fire.

Considering an example .tf file with two resources: the Supabase project resource and a branch resource, then as a result of this bug:

  • the branch resource becomes the default branch for the created project
  • a later terraform destroy would fail when trying to destroy the created branch resource (since it cannot delete the default branch)

Additionally, once that's fixed, the branch that is created is created as a preview -- not as the default branch -- because it's not set explicitly. So the (incorrect) resulting branches are:

  • main (default branch, not in .tf)
  • Production (preview, not in .tf)
  • branch resource (preview)

What is the new behavior?

createBranch() now creates the "Production" branch (now explicitly set as default) if the response is either 422 or 200 with an empty list of branches. The branch resource then becomes an additional branch that can be deleted, as expected.

Additional context

N/A

@JEETDESAI25
JEETDESAI25 self-requested a review July 9, 2026 11:29

@JEETDESAI25 JEETDESAI25 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nathanparekh, thanks for the fix and for adding a test.

This LGTM! The provider was only bootstrapping the Production branch when the branches API returned 422, but the API can now return 200 with an empty list. Handling both cases fixes the lifecycle issue where the Terraform managed branch could become the default branch and then fail on destroy, I confirmed the regression test covers this behavior.

The explicit is_default: true also ensures the bootstrap Production branch becomes default, and the switch to the WithResponse client variant fits the provider's existing patterns.

A follow-up test for the legacy 422 path would be useful, but I don't think it needs to block this PR. @savme

@savme

savme commented Jul 10, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the implementation, @nathanparekh, and for the review, @JEETDESAI25! Merging this now 🚀

@savme
savme merged commit 3677daa into supabase:main Jul 10, 2026
5 checks passed
@nathanparekh
nathanparekh deleted the fix/create-default-branch branch July 10, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants