<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.liquigraph.org/schema/1.0/liquigraph.xsd">
    <changeset id="PLATFORM-2508-merge-labelprofile-to-insightsprofile:1" author="rsatsangi" run-on-change="true">
        <query>
            <![CDATA[
            MATCH (i:Identity)-[:HAS_PROFILE]->(lp:Profile { profileType: 'LabelProfile' })
            WHERE ('analytics' IN lp.roles OR 'administrator' in lp.roles)
            WITH i, collect(lp) as label_profiles
                MATCH (i:Identity)-[:HAS_PROFILE]->(ip:Profile { profileType: 'InsightsProfile' })
            WITH i, label_profiles, ip
            UNWIND label_profiles as lp
                MATCH (lp)-[lp_rel]->(lp_rel_node)
                    WHERE substring(apoc.rel.type(lp_rel), 0, 8) <> 'DELETED_'
                    AND apoc.rel.type(lp_rel) <> 'HAS_ACCESS_TO'
                    AND NOT (ip)-[:HAS_FOLLOWED|:DELETED_HAS_FOLLOWED]->(lp_rel_node)
                WITH i, ip, lp, lp_rel, lp_rel_node
                CALL apoc.merge.relationship(
                    ip,
                    apoc.rel.type(lp_rel),
                    apoc.any.properties(lp_rel),
                    {
                        createdOn: datetime(),
                        createdBy: 'PLATFORM-2508'
                    },
                    lp_rel_node,
                    {
                        updatedOn: datetime(),
                        updatedBy: 'PLATFORM-2508'
                    }
                ) YIELD rel as ip_merged_rel
                WITH i, ip, lp, lp_rel, lp_rel_node, ip_merged_rel
                CALL apoc.refactor.setType(lp_rel, apoc.text.join(['DELETED_', apoc.rel.type(lp_rel)], '')) YIELD input, output as lp_rel_deleted
                SET lp_rel_deleted.deletedOn = datetime()
                SET lp_rel_deleted.deletedBy = 'PLATFORM-2508'
                SET ip_merged_rel.createdOn = datetime()
                SET ip_merged_rel.createdBy = 'PLATFORM-2508'
                WITH i, ip, lp, lp_rel_node, ip_merged_rel, lp_rel_deleted
            RETURN i.id;
            ]]>
        </query>
        <query>
            <![CDATA[
            MATCH (i:Identity)-[:HAS_PROFILE]->(lp:Profile { profileType: 'LabelProfile' })
            WHERE ('analytics' IN lp.roles OR 'administrator' in lp.roles)
            WITH i, collect(lp) as label_profiles
                MATCH (i:Identity)-[:HAS_PROFILE]->(ip:Profile { profileType: 'InsightsProfile' })
            WITH i, label_profiles, ip
            UNWIND label_profiles as lp
                MATCH (lp)-[lp_rel]->(lp_rel_node)
                    WHERE substring(apoc.rel.type(lp_rel), 0, 8) <> 'DELETED_'
                    AND apoc.rel.type(lp_rel) <> 'HAS_ACCESS_TO'
                    AND (ip)-[:HAS_FOLLOWED|:DELETED_HAS_FOLLOWED]->(lp_rel_node)
                WITH i, ip, lp, lp_rel, lp_rel_node
                CALL apoc.refactor.setType(lp_rel, apoc.text.join(['DELETED_', apoc.rel.type(lp_rel)], '')) YIELD input, output as lp_rel_deleted
                SET lp_rel_deleted.deletedOn = datetime()
                SET lp_rel_deleted.deletedBy = 'PLATFORM-2508'
                WITH *
            RETURN i.id;
            ]]>
        </query>
    </changeset>
</changelog>
