Skip to content

Commit 5371040

Browse files
committed
Add tests for migration runner
1 parent eb8bbb3 commit 5371040

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

backend/infrahub/core/branch/tasks.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
from infrahub.workflows.catalogue import (
5050
BRANCH_CANCEL_PROPOSED_CHANGES,
5151
BRANCH_MERGE_POST_PROCESS,
52-
BRANCH_MIGRATE,
5352
DIFF_REFRESH_ALL,
5453
DIFF_UPDATE,
5554
GIT_REPOSITORIES_CREATE_BRANCH,

backend/infrahub/core/migrations/runner.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING, Sequence
44

5+
from infrahub.core import registry
6+
from infrahub.core.constants import GLOBAL_BRANCH_NAME
57
from infrahub.core.migrations.graph import MIGRATIONS
68

79
from .exceptions import MigrationFailureError
@@ -14,6 +16,9 @@
1416

1517
class MigrationRunner:
1618
def __init__(self, branch: Branch) -> None:
19+
if branch.name in (registry.default_branch, GLOBAL_BRANCH_NAME):
20+
raise ValueError("MigrationRunner cannot be used to apply migration on default branches")
21+
1722
self.branch = branch
1823
self.applicable_migrations = self._get_applicable_migrations()
1924

backend/infrahub/graphql/types/branch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import TYPE_CHECKING, Any
44

5-
from graphene import Boolean, Field, String
5+
from graphene import Boolean, Field, Int, String
66

77
from infrahub.core.branch import Branch
88
from infrahub.core.constants import GLOBAL_BRANCH_NAME
@@ -21,6 +21,7 @@ class BranchType(InfrahubObjectType):
2121
origin_branch = String(required=False)
2222
branched_from = String(required=False)
2323
status = InfrahubBranchStatus(required=True)
24+
graph_version = Int(required=False)
2425
created_at = String(required=False)
2526
sync_with_git = Boolean(required=False)
2627
is_default = Boolean(required=False)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
from infrahub.core.branch.models import Branch
8+
from infrahub.core.constants import GLOBAL_BRANCH_NAME
9+
from infrahub.core.initialization import create_branch
10+
from infrahub.core.migrations.runner import MigrationRunner
11+
12+
if TYPE_CHECKING:
13+
from infrahub.database import InfrahubDatabase
14+
15+
16+
def test_instantiation(default_branch: Branch) -> None:
17+
assert MigrationRunner(branch=Branch(name="foo"))
18+
19+
with pytest.raises(ValueError):
20+
MigrationRunner(branch=default_branch)
21+
22+
with pytest.raises(ValueError):
23+
MigrationRunner(branch=Branch(name=GLOBAL_BRANCH_NAME))
24+
25+
26+
async def test_has_migrations(default_branch: Branch, db: InfrahubDatabase) -> None:
27+
branch = await create_branch(db=db, branch_name="foo")
28+
29+
runner = MigrationRunner(branch=branch)
30+
assert not runner.has_migrations()
31+
32+
branch.graph_version = None
33+
runner = MigrationRunner(branch=branch)
34+
await branch.save(db=db)
35+
assert runner.has_migrations()
36+
37+
branch.graph_version = 40
38+
runner = MigrationRunner(branch=branch)
39+
await branch.save(db=db)
40+
assert runner.has_migrations()
41+
42+
43+
async def test_applicable_migrations(default_branch: Branch, db: InfrahubDatabase) -> None:
44+
branch = await create_branch(db=db, branch_name="foo")
45+
46+
runner = MigrationRunner(branch=branch)
47+
assert not runner.applicable_migrations
48+
49+
branch.graph_version = None
50+
runner = MigrationRunner(branch=branch)
51+
await branch.save(db=db)
52+
assert runner.applicable_migrations
53+
assert [m.name for m in runner.applicable_migrations] == ["043_backfill_hfid_display_label_in_db"]
54+
55+
branch.graph_version = 40
56+
runner = MigrationRunner(branch=branch)
57+
await branch.save(db=db)
58+
assert runner.applicable_migrations
59+
assert [m.name for m in runner.applicable_migrations] == ["043_backfill_hfid_display_label_in_db"]

0 commit comments

Comments
 (0)