|
13 | 13 | from infrahub.core.manager import RelationshipSchema |
14 | 14 | from infrahub.core.query.relationship import RelationshipPeerData |
15 | 15 | from infrahub.core.relationship.model import Relationship |
| 16 | + from infrahub.core.schema import MainSchemaTypes |
| 17 | + from infrahub.core.schema.schema_branch import SchemaBranch |
16 | 18 | from infrahub.database import InfrahubDatabase |
17 | 19 |
|
18 | 20 |
|
@@ -463,37 +465,198 @@ async def get_changelogs(self, primary_changelog: NodeChangelog) -> list[NodeCha |
463 | 465 | These will typically include updates to relationships on other nodes. |
464 | 466 | """ |
465 | 467 | schema_branch = self._db.schema.get_schema_branch(name=self._branch.name) |
466 | | - node_schema = schema_branch.get(name=primary_changelog.node_kind) |
| 468 | + node_schema = schema_branch.get(name=primary_changelog.node_kind, duplicate=False) |
467 | 469 | secondaries: list[NodeChangelog] = [] |
468 | 470 |
|
469 | 471 | for relationship in primary_changelog.relationships.values(): |
470 | | - rel_schema = node_schema.get_relationship(name=relationship.name) |
471 | 472 | if isinstance(relationship, RelationshipCardinalityOneChangelog): |
472 | | - # For now this code only looks at the scenario when a cardinality=one relationship |
473 | | - # is added to a node and it has a cardinality=many relationship coming back from |
474 | | - # another node, it will be expanded to include all variations. |
475 | | - if relationship.peer_status == DiffAction.ADDED: |
476 | | - peer_schema = schema_branch.get(name=str(relationship.peer_kind)) |
477 | | - peer_relation = peer_schema.get_relationship_by_identifier( |
478 | | - id=str(rel_schema.identifier), raise_on_error=False |
| 473 | + secondaries.extend( |
| 474 | + self._parse_cardinality_one_relationship( |
| 475 | + relationship=relationship, |
| 476 | + node_schema=node_schema, |
| 477 | + primary_changelog=primary_changelog, |
| 478 | + schema_branch=schema_branch, |
479 | 479 | ) |
480 | | - if peer_relation: |
481 | | - node_changelog = NodeChangelog( |
482 | | - node_id=str(relationship.peer_id), |
483 | | - node_kind=str(relationship.peer_kind), |
484 | | - display_label="n/a", |
| 480 | + ) |
| 481 | + elif isinstance(relationship, RelationshipCardinalityManyChangelog): |
| 482 | + secondaries.extend( |
| 483 | + self._parse_cardinality_many_relationship( |
| 484 | + relationship=relationship, |
| 485 | + node_schema=node_schema, |
| 486 | + primary_changelog=primary_changelog, |
| 487 | + schema_branch=schema_branch, |
| 488 | + ) |
| 489 | + ) |
| 490 | + |
| 491 | + return secondaries |
| 492 | + |
| 493 | + def _parse_cardinality_one_relationship( |
| 494 | + self, |
| 495 | + relationship: RelationshipCardinalityOneChangelog, |
| 496 | + node_schema: MainSchemaTypes, |
| 497 | + primary_changelog: NodeChangelog, |
| 498 | + schema_branch: SchemaBranch, |
| 499 | + ) -> list[NodeChangelog]: |
| 500 | + secondaries: list[NodeChangelog] = [] |
| 501 | + rel_schema = node_schema.get_relationship(name=relationship.name) |
| 502 | + |
| 503 | + if relationship.peer_status == DiffAction.ADDED: |
| 504 | + peer_schema = schema_branch.get(name=str(relationship.peer_kind), duplicate=False) |
| 505 | + secondaries.extend( |
| 506 | + self._process_added_peers( |
| 507 | + peer_id=str(relationship.peer_id), |
| 508 | + peer_kind=str(relationship.peer_kind), |
| 509 | + peer_schema=peer_schema, |
| 510 | + rel_schema=rel_schema, |
| 511 | + primary_changelog=primary_changelog, |
| 512 | + ) |
| 513 | + ) |
| 514 | + |
| 515 | + elif relationship.peer_status == DiffAction.UPDATED: |
| 516 | + peer_schema = schema_branch.get(name=str(relationship.peer_kind), duplicate=False) |
| 517 | + secondaries.extend( |
| 518 | + self._process_added_peers( |
| 519 | + peer_id=str(relationship.peer_id), |
| 520 | + peer_kind=str(relationship.peer_kind), |
| 521 | + peer_schema=peer_schema, |
| 522 | + rel_schema=rel_schema, |
| 523 | + primary_changelog=primary_changelog, |
| 524 | + ) |
| 525 | + ) |
| 526 | + secondaries.extend( |
| 527 | + self._process_removed_peers( |
| 528 | + peer_schema=peer_schema, |
| 529 | + peer_id=str(relationship.peer_id_previous), |
| 530 | + peer_kind=str(relationship.peer_kind_previous), |
| 531 | + rel_schema=rel_schema, |
| 532 | + primary_changelog=primary_changelog, |
| 533 | + ) |
| 534 | + ) |
| 535 | + |
| 536 | + elif relationship.peer_status == DiffAction.REMOVED: |
| 537 | + peer_schema = schema_branch.get(name=str(relationship.peer_kind_previous), duplicate=False) |
| 538 | + |
| 539 | + secondaries.extend( |
| 540 | + self._process_removed_peers( |
| 541 | + peer_id=str(relationship.peer_id_previous), |
| 542 | + peer_kind=str(relationship.peer_kind_previous), |
| 543 | + peer_schema=peer_schema, |
| 544 | + rel_schema=rel_schema, |
| 545 | + primary_changelog=primary_changelog, |
| 546 | + ) |
| 547 | + ) |
| 548 | + |
| 549 | + return secondaries |
| 550 | + |
| 551 | + def _parse_cardinality_many_relationship( |
| 552 | + self, |
| 553 | + relationship: RelationshipCardinalityManyChangelog, |
| 554 | + node_schema: MainSchemaTypes, |
| 555 | + primary_changelog: NodeChangelog, |
| 556 | + schema_branch: SchemaBranch, |
| 557 | + ) -> list[NodeChangelog]: |
| 558 | + secondaries: list[NodeChangelog] = [] |
| 559 | + rel_schema = node_schema.get_relationship(name=relationship.name) |
| 560 | + |
| 561 | + for peer in relationship.peers: |
| 562 | + if peer.peer_status == DiffAction.ADDED: |
| 563 | + peer_schema = schema_branch.get(name=peer.peer_kind) |
| 564 | + secondaries.extend( |
| 565 | + self._process_added_peers( |
| 566 | + peer_id=peer.peer_id, |
| 567 | + peer_kind=peer.peer_kind, |
| 568 | + peer_schema=peer_schema, |
| 569 | + rel_schema=rel_schema, |
| 570 | + primary_changelog=primary_changelog, |
| 571 | + ) |
| 572 | + ) |
| 573 | + |
| 574 | + elif peer.peer_status == DiffAction.REMOVED: |
| 575 | + peer_schema = schema_branch.get(name=peer.peer_kind) |
| 576 | + secondaries.extend( |
| 577 | + self._process_removed_peers( |
| 578 | + peer_id=peer.peer_id, |
| 579 | + peer_kind=peer.peer_kind, |
| 580 | + peer_schema=peer_schema, |
| 581 | + rel_schema=rel_schema, |
| 582 | + primary_changelog=primary_changelog, |
| 583 | + ) |
| 584 | + ) |
| 585 | + |
| 586 | + return secondaries |
| 587 | + |
| 588 | + def _process_added_peers( |
| 589 | + self, |
| 590 | + peer_id: str, |
| 591 | + peer_kind: str, |
| 592 | + peer_schema: MainSchemaTypes, |
| 593 | + rel_schema: RelationshipSchema, |
| 594 | + primary_changelog: NodeChangelog, |
| 595 | + ) -> list[NodeChangelog]: |
| 596 | + secondaries: list[NodeChangelog] = [] |
| 597 | + peer_relation = peer_schema.get_relationship_by_identifier(id=str(rel_schema.identifier), raise_on_error=False) |
| 598 | + if peer_relation: |
| 599 | + node_changelog = NodeChangelog( |
| 600 | + node_id=peer_id, |
| 601 | + node_kind=peer_kind, |
| 602 | + display_label="n/a", |
| 603 | + ) |
| 604 | + if peer_relation.cardinality == RelationshipCardinality.ONE: |
| 605 | + node_changelog.relationships[peer_relation.name] = RelationshipCardinalityOneChangelog( |
| 606 | + name=peer_relation.name, |
| 607 | + peer_id=primary_changelog.node_id, |
| 608 | + peer_kind=primary_changelog.node_kind, |
| 609 | + ) |
| 610 | + secondaries.append(node_changelog) |
| 611 | + elif peer_relation.cardinality == RelationshipCardinality.MANY: |
| 612 | + node_changelog.relationships[peer_relation.name] = RelationshipCardinalityManyChangelog( |
| 613 | + name=peer_relation.name, |
| 614 | + peers=[ |
| 615 | + RelationshipPeerChangelog( |
| 616 | + peer_id=primary_changelog.node_id, |
| 617 | + peer_kind=primary_changelog.node_kind, |
| 618 | + peer_status=DiffAction.ADDED, |
| 619 | + ) |
| 620 | + ], |
| 621 | + ) |
| 622 | + secondaries.append(node_changelog) |
| 623 | + |
| 624 | + return secondaries |
| 625 | + |
| 626 | + def _process_removed_peers( |
| 627 | + self, |
| 628 | + peer_id: str, |
| 629 | + peer_kind: str, |
| 630 | + peer_schema: MainSchemaTypes, |
| 631 | + rel_schema: RelationshipSchema, |
| 632 | + primary_changelog: NodeChangelog, |
| 633 | + ) -> list[NodeChangelog]: |
| 634 | + secondaries: list[NodeChangelog] = [] |
| 635 | + peer_relation = peer_schema.get_relationship_by_identifier(id=str(rel_schema.identifier), raise_on_error=False) |
| 636 | + if peer_relation: |
| 637 | + node_changelog = NodeChangelog( |
| 638 | + node_id=peer_id, |
| 639 | + node_kind=peer_kind, |
| 640 | + display_label="n/a", |
| 641 | + ) |
| 642 | + if peer_relation.cardinality == RelationshipCardinality.ONE: |
| 643 | + node_changelog.relationships[peer_relation.name] = RelationshipCardinalityOneChangelog( |
| 644 | + name=peer_relation.name, |
| 645 | + peer_id_previous=primary_changelog.node_id, |
| 646 | + peer_kind_previous=primary_changelog.node_kind, |
| 647 | + ) |
| 648 | + secondaries.append(node_changelog) |
| 649 | + elif peer_relation.cardinality == RelationshipCardinality.MANY: |
| 650 | + node_changelog.relationships[peer_relation.name] = RelationshipCardinalityManyChangelog( |
| 651 | + name=peer_relation.name, |
| 652 | + peers=[ |
| 653 | + RelationshipPeerChangelog( |
| 654 | + peer_id=primary_changelog.node_id, |
| 655 | + peer_kind=primary_changelog.node_kind, |
| 656 | + peer_status=DiffAction.REMOVED, |
485 | 657 | ) |
486 | | - if peer_relation.cardinality == RelationshipCardinality.MANY: |
487 | | - node_changelog.relationships[peer_relation.name] = RelationshipCardinalityManyChangelog( |
488 | | - name=peer_relation.name, |
489 | | - peers=[ |
490 | | - RelationshipPeerChangelog( |
491 | | - peer_id=primary_changelog.node_id, |
492 | | - peer_kind=primary_changelog.node_kind, |
493 | | - peer_status=DiffAction.ADDED, |
494 | | - ) |
495 | | - ], |
496 | | - ) |
497 | | - secondaries.append(node_changelog) |
| 658 | + ], |
| 659 | + ) |
| 660 | + secondaries.append(node_changelog) |
498 | 661 |
|
499 | 662 | return secondaries |
0 commit comments