Skip to content

Commit c975dfe

Browse files
committed
Add --rebase-branches flag to upgrade cmd
1 parent a4ebc6f commit c975dfe

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

backend/infrahub/cli/db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ async def rebase_and_migrate_branches(db: InfrahubDatabase, current_graph_versio
438438
for branch in branches:
439439
migrations = [
440440
m
441-
for m in await detect_migration_to_run(current_graph_version=branch.graph_version or current_graph_version)
441+
for m in await detect_migration_to_run(current_graph_version=branch.graph_version or 0)
442442
if isinstance(m, MigrationWithRebase)
443443
]
444444
rprint(

backend/infrahub/cli/upgrade.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ async def upgrade_cmd(
5151
ctx: typer.Context,
5252
config_file: str = typer.Argument("infrahub.toml", envvar="INFRAHUB_CONFIG"),
5353
check: bool = typer.Option(False, help="Check the state of the system without upgrading."),
54+
rebase_branches: bool = typer.Option(False, help="Rebase and apply migrations to branches if required."),
5455
) -> None:
5556
"""Upgrade Infrahub to the latest version."""
5657

@@ -93,12 +94,6 @@ async def upgrade_cmd(
9394
await initialize_internal_schema()
9495
await update_core_schema(db=dbdriver, initialize=False)
9596

96-
if not await rebase_and_migrate_branches(db=dbdriver, current_graph_version=root_node.graph_version):
97-
# A migration failed, stop the upgrade process
98-
rprint("Upgrade cancelled due to branch rebase and migration failure.")
99-
await dbdriver.close()
100-
return
101-
10297
# -------------------------------------------
10398
# Upgrade Internal Objects, generated and managed by Infrahub
10499
# -------------------------------------------
@@ -114,6 +109,17 @@ async def upgrade_cmd(
114109
await setup_deployments(client=client)
115110
await trigger_configure_all()
116111

112+
# -------------------------------------------
113+
# Perform branch rebase and apply migrations to them
114+
# -------------------------------------------
115+
if rebase_branches and not await rebase_and_migrate_branches(
116+
db=dbdriver, current_graph_version=root_node.graph_version
117+
):
118+
# A migration failed, stop the upgrade process
119+
rprint("Upgrade cancelled due to branch rebase and migration failure.")
120+
await dbdriver.close()
121+
return
122+
117123
await dbdriver.close()
118124

119125

backend/infrahub/core/migrations/shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ async def process_branch(self, db: InfrahubDatabase, branch: Branch) -> Migratio
275275
async def execute_against_branch(
276276
self, db: InfrahubDatabase, branch: Branch, skip_rebase: bool = False
277277
) -> MigrationResult:
278+
initialize_lock()
278279
await registry.schema.load_schema(db=db, branch=branch)
279280

280281
if not skip_rebase:
@@ -288,7 +289,6 @@ async def execute_against_branch(
288289
async def execute(self, db: InfrahubDatabase) -> MigrationResult:
289290
from infrahub.core.initialization import initialization
290291

291-
initialize_lock()
292292
await initialization(db=db)
293293

294294
return await self.execute_against_branch(db=db, branch=registry.get_branch_from_registry(), skip_rebase=True)

0 commit comments

Comments
 (0)