| 
 | 1 | +from __future__ import annotations  | 
 | 2 | + | 
 | 3 | +from typing import TYPE_CHECKING, Any  | 
 | 4 | + | 
 | 5 | +from infrahub.core.query import Query, QueryType  | 
 | 6 | + | 
 | 7 | +if TYPE_CHECKING:  | 
 | 8 | +    from infrahub.core.branch import Branch  | 
 | 9 | +    from infrahub.core.timestamp import Timestamp  | 
 | 10 | +    from infrahub.database import InfrahubDatabase  | 
 | 11 | + | 
 | 12 | + | 
 | 13 | +class DiffMergeQuery(Query):  | 
 | 14 | +    name = "diff_merge"  | 
 | 15 | +    type = QueryType.WRITE  | 
 | 16 | +    insert_return = False  | 
 | 17 | + | 
 | 18 | +    def __init__(  | 
 | 19 | +        self,  | 
 | 20 | +        node_diff_dicts: dict[str, Any],  | 
 | 21 | +        at: Timestamp,  | 
 | 22 | +        target_branch: Branch,  | 
 | 23 | +        **kwargs: Any,  | 
 | 24 | +    ) -> None:  | 
 | 25 | +        super().__init__(**kwargs)  | 
 | 26 | +        self.node_diff_dicts = node_diff_dicts  | 
 | 27 | +        self.at = at  | 
 | 28 | +        self.target_branch = target_branch  | 
 | 29 | +        self.source_branch_name = self.branch.name  | 
 | 30 | + | 
 | 31 | +    async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:  | 
 | 32 | +        self.params = {  | 
 | 33 | +            "node_diff_dicts": self.node_diff_dicts,  | 
 | 34 | +            "at": self.at.to_string(),  | 
 | 35 | +            "branch_level": self.target_branch.hierarchy_level,  | 
 | 36 | +            "target_branch": self.target_branch.name,  | 
 | 37 | +            "source_branch": self.source_branch_name,  | 
 | 38 | +        }  | 
 | 39 | +        query = """  | 
 | 40 | +UNWIND $node_diff_dicts AS node_diff_map  | 
 | 41 | +CALL {  | 
 | 42 | +    WITH node_diff_map  | 
 | 43 | +    WITH node_diff_map, CASE  | 
 | 44 | +        WHEN node_diff_map.action = "ADDED" THEN "active"  | 
 | 45 | +        WHEN node_diff_map.action = "REMOVED" THEN "deleted"  | 
 | 46 | +        ELSE NULL  | 
 | 47 | +    END AS node_rel_status  | 
 | 48 | +    CALL {  | 
 | 49 | +        // ------------------------------  | 
 | 50 | +        // only make IS_PART_OF updates if node is ADDED or REMOVED  | 
 | 51 | +        // ------------------------------  | 
 | 52 | +        WITH node_diff_map, node_rel_status  | 
 | 53 | +        WITH node_diff_map, node_rel_status  | 
 | 54 | +        WHERE node_rel_status IS NOT NULL  | 
 | 55 | +        MATCH (root:Root)  | 
 | 56 | +        MATCH (n:Node {uuid: node_diff_map.uuid})  | 
 | 57 | +        // ------------------------------  | 
 | 58 | +        // check if IS_PART_OF relationship with node_rel_status already exists on the target branch  | 
 | 59 | +        // ------------------------------  | 
 | 60 | +        CALL {  | 
 | 61 | +            WITH root, n, node_rel_status  | 
 | 62 | +            OPTIONAL MATCH (root)<-[r_root:IS_PART_OF {branch: $target_branch}]-(n)  | 
 | 63 | +            WHERE r_root.status = node_rel_status  | 
 | 64 | +            AND r_root.from <= $at  | 
 | 65 | +            AND (r_root.to >= $at OR r_root.to IS NULL)  | 
 | 66 | +            RETURN r_root  | 
 | 67 | +        }  | 
 | 68 | +        // ------------------------------  | 
 | 69 | +        // set IS_PART_OF.to on source branch and, optionally, target branch  | 
 | 70 | +        // ------------------------------  | 
 | 71 | +        WITH root, r_root, n, node_rel_status  | 
 | 72 | +        CALL {  | 
 | 73 | +            WITH root, n, node_rel_status  | 
 | 74 | +            OPTIONAL MATCH (root)<-[source_r_root:IS_PART_OF {branch: $source_branch, status: node_rel_status}]-(n)  | 
 | 75 | +            WHERE source_r_root.from <= $at AND source_r_root.to IS NULL  | 
 | 76 | +            SET source_r_root.to = $at  | 
 | 77 | +        }  | 
 | 78 | +        WITH root, r_root, n, node_rel_status  | 
 | 79 | +        CALL {  | 
 | 80 | +            WITH root, n, node_rel_status  | 
 | 81 | +            OPTIONAL MATCH (root)<-[target_r_root:IS_PART_OF {branch: $target_branch, status: "active"}]-(n)  | 
 | 82 | +            WHERE node_rel_status = "deleted"  | 
 | 83 | +            AND target_r_root.from <= $at AND target_r_root.to IS NULL  | 
 | 84 | +            SET target_r_root.to = $at  | 
 | 85 | +        }  | 
 | 86 | +        // ------------------------------  | 
 | 87 | +        // create new IS_PART_OF relationship on target_branch  | 
 | 88 | +        // ------------------------------  | 
 | 89 | +        WITH root, r_root, n, node_rel_status  | 
 | 90 | +        WHERE r_root IS NULL  | 
 | 91 | +        CREATE (root)<-[:IS_PART_OF { branch: $target_branch, branch_level: $branch_level, from: $at, status: node_rel_status }]-(n)  | 
 | 92 | +    }  | 
 | 93 | +}  | 
 | 94 | +        """  | 
 | 95 | +        self.add_to_query(query=query)  | 
0 commit comments