Skip to content

Commit 74fd90d

Browse files
committed
Wrap into database session
1 parent 6325348 commit 74fd90d

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

backend/infrahub/core/branch/tasks.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,38 @@
6363
async def migrate_branch(branch: str, context: InfrahubContext, send_events: bool = True) -> None:
6464
await add_tags(branches=[branch])
6565

66-
db = await get_database()
67-
log = get_run_logger()
68-
69-
obj = await Branch.get_by_name(db=db, name=branch)
66+
database = await get_database()
67+
async with database.start_session() as db:
68+
log = get_run_logger()
7069

71-
if obj.graph_version == GRAPH_VERSION:
72-
log.info(f"Branch '{obj.name}' is up-to-date")
73-
return
70+
obj = await Branch.get_by_name(db=db, name=branch)
7471

75-
migration_runner = MigrationRunner(branch=obj)
76-
if not migration_runner.has_migrations():
77-
log.info(f"No migrations detected for branch '{obj.name}'")
78-
return
72+
if obj.graph_version == GRAPH_VERSION:
73+
log.info(f"Branch '{obj.name}' has graph version {obj.graph_version}, no migrations to apply")
74+
return
7975

80-
# Branch status will remain as so if the migration process fails
81-
# This will help user to know that a branch is in an invalid state to be used properly and that actions need to be taken
82-
obj.status = BranchStatus.NEED_UPGRADE_REBASE
83-
await obj.save(db=db)
76+
migration_runner = MigrationRunner(branch=obj)
77+
if not migration_runner.has_migrations():
78+
log.info(f"No migrations detected for branch '{obj.name}'")
79+
return
8480

85-
try:
86-
log.info(f"Running migrations for branch '{obj.name}'")
87-
await migration_runner.run(db=db)
88-
except MigrationFailureError as exc:
89-
log.error(f"Failed to run migrations for branch '{obj.name}': {exc.errors}")
90-
return
81+
# Branch status will remain as so if the migration process fails
82+
# This will help user to know that a branch is in an invalid state to be used properly and that actions need to be taken
83+
if obj.status != BranchStatus.NEED_UPGRADE_REBASE:
84+
obj.status = BranchStatus.NEED_UPGRADE_REBASE
85+
await obj.save(db=db)
9186

92-
if obj.status == BranchStatus.NEED_UPGRADE_REBASE:
93-
obj.status = BranchStatus.OPEN
94-
obj.graph_version = GRAPH_VERSION
95-
await obj.save(db=db)
87+
try:
88+
log.info(f"Running migrations for branch '{obj.name}'")
89+
await migration_runner.run(db=db)
90+
except MigrationFailureError as exc:
91+
log.error(f"Failed to run migrations for branch '{obj.name}': {exc.errors}")
92+
return
93+
94+
if obj.status == BranchStatus.NEED_UPGRADE_REBASE:
95+
obj.status = BranchStatus.OPEN
96+
obj.graph_version = GRAPH_VERSION
97+
await obj.save(db=db)
9698

9799
if send_events:
98100
event_service = await get_event_service()

0 commit comments

Comments
 (0)