Skip to content

fix(planetscale): delete the branch an apply created when it fails before its deploy request - #963

Draft
aparajon wants to merge 1 commit into
mainfrom
armand/ps-branch-leak-on-early-failure
Draft

fix(planetscale): delete the branch an apply created when it fails before its deploy request#963
aparajon wants to merge 1 commit into
mainfrom
armand/ps-branch-leak-on-early-failure

Conversation

@aparajon

@aparajon aparajon commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Why this matters

Branch teardown rides on AutoDeleteBranch, which is set on the deploy request. An apply that fails while preparing the branch — the ordinary failure, DDL the engine refuses — never gets that far, so the branch is stranded with nothing to reclaim it. Branches are quota'd, so the strand does not surface where it was created: it surfaces later as a branch-creation error on an unrelated schema change.

What it does

The apply owns the branch it created until the deploy request takes over, and deletes it on the way out of a failure. An operator-supplied branch is never touched. The delete runs on its own deadline, detached from the apply's context, so a cancelled apply still cleans up; a branch that cannot be deleted is logged at error level with the identifiers needed to remove it by hand.

🤖 Generated with Claude Code

…fore its deploy request

Branch teardown rides on AutoDeleteBranch, which is set on the deploy
request. An apply that fails while preparing the branch — the ordinary
failure, DDL the engine refuses — never gets that far, so the branch is
stranded with nothing to reclaim it. Branches are quota'd, so the strand
eventually surfaces as a branch-creation error on an unrelated schema
change.

The apply now owns the branch it created until the deploy request takes
over, and deletes it on the way out of a failure. An operator-supplied
branch is never touched. The delete runs on its own deadline, detached
from the apply's context, so a cancelled apply still cleans up; a branch
that cannot be deleted is logged at error level with the identifiers
needed to remove it by hand.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings August 7, 2026 19:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a PlanetScale operational leak where an apply that fails before creating a deploy request can strand the newly-created branch (because AutoDeleteBranch is only set on the deploy request). It introduces explicit cleanup ownership for SchemaBot-created branches until the deploy request exists.

Changes:

  • Add an Engine helper to delete a SchemaBot-owned branch on apply failure using a detached, bounded-timeout context.
  • Track “owned branch” lifetime inside Apply() and trigger cleanup via a deferred failure handler until deploy-request creation transfers ownership.
  • Add a focused test covering both SchemaBot-created branch cleanup and the “operator-supplied branch must not be touched” case.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/engine/planetscale/branch_cleanup.go Adds bounded, detached branch deletion helper with operator-actionable logging on failure.
pkg/engine/planetscale/branch_cleanup_test.go Tests that applies delete only the branches they create when failing pre–deploy request, and never delete operator-supplied branches.
pkg/engine/planetscale/apply.go Tracks branch ownership and defers cleanup on failure until deploy request creation transfers teardown responsibility.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants