Skip to content

Commit 26c84d1

Browse files
authored
fixes to allow getting a diff for a single branch (#5842)
1 parent 438b823 commit 26c84d1

File tree

5 files changed

+81
-13
lines changed

5 files changed

+81
-13
lines changed

backend/infrahub/core/diff/model/path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ class EnrichedDiffRootMetadata(BaseSummary):
409409
from_time: Timestamp
410410
to_time: Timestamp
411411
uuid: str
412-
partner_uuid: str
413412
tracking_id: TrackingId
413+
partner_uuid: str | None = field(default=None)
414414
exists_on_database: bool = field(default=False)
415415

416416
def __hash__(self) -> int:

backend/infrahub/core/diff/query/save.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
4242
}
4343
WITH DISTINCT diff_root AS diff_root
4444
WITH collect(diff_root) AS diff_roots
45+
WHERE SIZE(diff_roots) = 2
4546
CALL {
4647
WITH diff_roots
4748
WITH diff_roots[0] AS base_diff_node, diff_roots[1] AS branch_diff_node

backend/infrahub/core/diff/repository/deserializer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def _deserialize_diff_root(self, root_node: Neo4jNode) -> EnrichedDiffRoot:
164164
def build_diff_root_metadata(cls, root_node: Neo4jNode) -> EnrichedDiffRootMetadata:
165165
from_time = Timestamp(str(root_node.get("from_time")))
166166
to_time = Timestamp(str(root_node.get("to_time")))
167+
partner_uuid = cls._get_str_or_none_property_value(node=root_node, property_name="partner_uuid")
167168
tracking_id_str = str(root_node.get("tracking_id"))
168169
tracking_id = deserialize_tracking_id(tracking_id_str=tracking_id_str)
169170
return EnrichedDiffRootMetadata(
@@ -172,7 +173,7 @@ def build_diff_root_metadata(cls, root_node: Neo4jNode) -> EnrichedDiffRootMetad
172173
from_time=from_time,
173174
to_time=to_time,
174175
uuid=str(root_node.get("uuid")),
175-
partner_uuid=str(root_node.get("partner_uuid")),
176+
partner_uuid=partner_uuid,
176177
tracking_id=tracking_id,
177178
num_added=int(root_node.get("num_added", 0)),
178179
num_updated=int(root_node.get("num_updated", 0)),

backend/infrahub/core/diff/repository/repository.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,23 @@ async def get_pairs(
154154
diff_branch_names=[base_branch_name],
155155
max_depth=max_depth,
156156
batch_size_limit=batch_size_limit,
157-
diff_ids=[d.partner_uuid for d in diffs_by_uuid.values()],
157+
diff_ids=[d.partner_uuid for d in diffs_by_uuid.values() if d.partner_uuid],
158158
)
159159
diffs_by_uuid.update({bbr.uuid: bbr for bbr in base_branch_roots})
160-
return [
161-
EnrichedDiffs(
162-
base_branch_name=base_branch_name,
163-
diff_branch_name=diff_branch_name,
164-
base_branch_diff=diffs_by_uuid[dbr.partner_uuid],
165-
diff_branch_diff=dbr,
160+
diff_pairs = []
161+
for dbr in diff_branch_roots:
162+
if dbr.partner_uuid is None:
163+
continue
164+
base_branch_diff = diffs_by_uuid[dbr.partner_uuid]
165+
diff_pairs.append(
166+
EnrichedDiffs(
167+
base_branch_name=base_branch_name,
168+
diff_branch_name=diff_branch_name,
169+
base_branch_diff=base_branch_diff,
170+
diff_branch_diff=dbr,
171+
)
166172
)
167-
for dbr in diff_branch_roots
168-
]
173+
return diff_pairs
169174

170175
async def hydrate_diff_pair(
171176
self,
@@ -325,7 +330,7 @@ async def get_diff_pairs_metadata(
325330
roots_by_id = {root.uuid: root for root in empty_roots}
326331
pairs: list[EnrichedDiffsMetadata] = []
327332
for branch_root in empty_roots:
328-
if branch_root.base_branch_name == branch_root.diff_branch_name:
333+
if branch_root.base_branch_name == branch_root.diff_branch_name or branch_root.partner_uuid is None:
329334
continue
330335
base_root = roots_by_id[branch_root.partner_uuid]
331336
pairs.append(

backend/tests/unit/core/diff/test_coordinator.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from infrahub.core.diff.calculator import DiffCalculator
99
from infrahub.core.diff.combiner import DiffCombiner
1010
from infrahub.core.diff.coordinator import DiffCoordinator
11-
from infrahub.core.diff.model.path import BranchTrackingId, EnrichedDiffRootMetadata
11+
from infrahub.core.diff.model.path import BranchTrackingId, EnrichedDiffRootMetadata, NameTrackingId
1212
from infrahub.core.diff.repository.repository import DiffRepository
1313
from infrahub.core.initialization import create_branch
1414
from infrahub.core.manager import NodeManager
@@ -240,3 +240,64 @@ async def test_unrelated_changes_skip_some_expensive_operations(
240240
)
241241
assert no_changes_diff.from_time == no_changes_diff_metadata.from_time == diff_with_data.from_time
242242
assert no_changes_diff.to_time == no_changes_diff_metadata.to_time
243+
244+
async def test_diff_on_default_branch_only(
245+
self,
246+
db: InfrahubDatabase,
247+
default_branch: Branch,
248+
person_john_main: Node,
249+
person_jane_main: Node,
250+
person_alfred_main: Node,
251+
car_camry_main: Node,
252+
car_accord_main: Node,
253+
) -> None:
254+
branch = await create_branch(db=db, branch_name="branch1")
255+
256+
updated_person = await NodeManager.get_one(db=db, id=person_john_main.id)
257+
updated_person.height.value = 200
258+
await updated_person.save(db=db)
259+
260+
new_person = await Node.init(db=db, schema="TestPerson", branch=default_branch)
261+
await new_person.new(db=db, name="Jeff", height=170)
262+
await new_person.save(db=db)
263+
264+
deleted_person = await NodeManager.get_one(db=db, id=person_alfred_main.id)
265+
await deleted_person.delete(db=db)
266+
267+
updated_car = await NodeManager.get_one(db=db, id=car_accord_main.id)
268+
await updated_car.owner.update(db=db, data=new_person)
269+
await updated_car.save(db=db)
270+
271+
from_time = Timestamp(branch.get_branched_from())
272+
to_time = Timestamp()
273+
name = str(uuid4())
274+
diff_coordinator = await self.get_wrapped_diff_coordinator(db=db, branch=default_branch)
275+
component_registry = get_component_registry()
276+
diff_repository = await component_registry.get_component(DiffRepository, db=db, branch=default_branch)
277+
main_diff_metadata = await diff_coordinator.create_or_update_arbitrary_timeframe_diff(
278+
base_branch=default_branch, diff_branch=default_branch, from_time=from_time, to_time=to_time, name=name
279+
)
280+
main_diff = await diff_repository.get_one(diff_branch_name=default_branch.name, diff_id=main_diff_metadata.uuid)
281+
282+
assert main_diff.base_branch_name == default_branch.name
283+
assert main_diff.diff_branch_name == default_branch.name
284+
assert main_diff.from_time == from_time
285+
assert main_diff.to_time == to_time
286+
assert main_diff.tracking_id == NameTrackingId(name=name)
287+
assert len(main_diff.nodes) == 4
288+
nodes_by_id = {n.uuid: n for n in main_diff.nodes}
289+
assert set(nodes_by_id.keys()) == {updated_person.id, new_person.id, deleted_person.id, updated_car.id}
290+
new_person_diff = nodes_by_id[new_person.id]
291+
assert new_person_diff.action is DiffAction.ADDED
292+
deleted_person_diff = nodes_by_id[deleted_person.id]
293+
assert deleted_person_diff.action is DiffAction.REMOVED
294+
updated_car_diff = nodes_by_id[updated_car.id]
295+
assert updated_car_diff.action is DiffAction.UPDATED
296+
assert updated_car_diff.attributes == set()
297+
rel_diffs = {(r.name, r.action) for r in updated_car_diff.relationships}
298+
assert rel_diffs == {("owner", DiffAction.UPDATED)}
299+
updated_person_diff = nodes_by_id[updated_person.id]
300+
rel_diffs = {(r.name, r.action) for r in updated_person_diff.relationships}
301+
assert rel_diffs == {("cars", DiffAction.UPDATED)}
302+
attr_diffs = {(a.name, a.action) for a in updated_person_diff.attributes}
303+
assert attr_diffs == {("height", DiffAction.UPDATED)}

0 commit comments

Comments
 (0)