|
63 | 63 | async def migrate_branch(branch: str, context: InfrahubContext, send_events: bool = True) -> None: |
64 | 64 | await add_tags(branches=[branch]) |
65 | 65 |
|
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() |
70 | 69 |
|
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) |
74 | 71 |
|
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 |
79 | 75 |
|
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 |
84 | 80 |
|
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) |
91 | 86 |
|
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) |
96 | 98 |
|
97 | 99 | if send_events: |
98 | 100 | event_service = await get_event_service() |
|
0 commit comments