Skip to content

Commit ec7ca5f

Browse files
fatih-acarclaude
andcommitted
fix(upgrade): preserve status of merged branches during infrahub upgrade
Skip terminal branches (MERGED, DELETING) when marking branches as NEED_UPGRADE_REBASE. Previously infrahub upgrade reopened every merged branch by overwriting its status, ignoring that MERGED branches are meant to be read-only / deleted from a user perspective. Closes #9103 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 5bf1f8c commit ec7ca5f

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

backend/infrahub/cli/db.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,11 @@ async def migrate_database(
439439

440440

441441
async def mark_branches_needing_rebase(db: InfrahubDatabase) -> list[Branch]:
442-
branches = [b for b in await Branch.get_list(db=db) if b.name not in [registry.default_branch, GLOBAL_BRANCH_NAME]]
442+
branches = [
443+
b
444+
for b in await Branch.get_list(db=db)
445+
if b.name not in [registry.default_branch, GLOBAL_BRANCH_NAME] and not b.is_terminal
446+
]
443447
if not branches:
444448
return []
445449

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from infrahub.cli.db import mark_branches_needing_rebase
2+
from infrahub.core.branch import Branch
3+
from infrahub.core.branch.enums import BranchStatus
4+
from infrahub.core.graph import GRAPH_VERSION
5+
from infrahub.core.initialization import create_branch
6+
from infrahub.database import InfrahubDatabase
7+
8+
9+
async def test_mark_branches_needing_rebase_skips_terminal_branches(
10+
db: InfrahubDatabase, default_branch: Branch
11+
) -> None:
12+
open_branch = await create_branch(branch_name="open-branch", db=db)
13+
open_branch.graph_version = GRAPH_VERSION - 1
14+
await open_branch.save(db=db)
15+
16+
merged_branch = await create_branch(branch_name="merged-branch", db=db)
17+
merged_branch.graph_version = GRAPH_VERSION - 1
18+
merged_branch.status = BranchStatus.MERGED
19+
await merged_branch.save(db=db)
20+
21+
deleting_branch = await create_branch(branch_name="deleting-branch", db=db)
22+
deleting_branch.graph_version = GRAPH_VERSION - 1
23+
deleting_branch.status = BranchStatus.DELETING
24+
await deleting_branch.save(db=db)
25+
26+
flagged = await mark_branches_needing_rebase(db=db)
27+
28+
flagged_names = {b.name for b in flagged}
29+
assert "open-branch" in flagged_names
30+
assert "merged-branch" not in flagged_names
31+
assert "deleting-branch" not in flagged_names
32+
33+
refreshed_merged = await Branch.get_by_name(name="merged-branch", db=db)
34+
refreshed_deleting = await Branch.get_by_name(name="deleting-branch", db=db, ignore_deleting=False)
35+
assert refreshed_merged.status == BranchStatus.MERGED
36+
assert refreshed_deleting.status == BranchStatus.DELETING

changelog/9103.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Stop `infrahub upgrade` from overwriting the status of merged or deleting branches. Branches in a terminal state (`MERGED`, `DELETING`) are now skipped, so they no longer reappear as `NEED_UPGRADE_REBASE` after an upgrade.

0 commit comments

Comments
 (0)