<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://www.liquigraph.org/schema/1.0/liquigraph.xsd">
    <changeset id="ARTARCH-654_repair_lp_gp_relations:1" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                MATCH (gp:GlobalParticipant)-[rel:REPRESENTS]->(lp:LabelParticipant)
                WHERE gp.spotifyId <> lp.spotifyId OR lp.spotifyId IS NULL
                DELETE rel

                MERGE (gp)-[deletedRepresents:DELETED_REPRESENTS]->(lp)
                SET
                    deletedRepresents.deletedAt = datetime(),
                    deletedRepresents.deletedBy = 'database/ARTARCH-654_repair_lp_gp_relations:1'
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-654_repair_lp_gp_relations:2" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                MATCH (lp:LabelParticipant)
                WHERE EXISTS(lp.spotifyId) AND NOT EXISTS((:GlobalParticipant { spotifyId: lp.spotifyId })-[:REPRESENTS]->(lp))

                MERGE (gp:GlobalParticipant {spotifyId: lp.spotifyId})
                    ON CREATE SET
                        gp.id = apoc.create.uuid(),
                        gp.name = lp.name,
                        gp.appleMusicId = lp.appleMusicId,
                        gp.createdAt = datetime(),
                        gp.createdBy = 'database/ARTARCH-654_repair_lp_gp_relations:2'
                MERGE (gp)-[gpToLp:REPRESENTS]->(lp)
                    ON CREATE SET
                        gpToLp.createdAt = datetime(),
                        gpToLp.createdBy = 'database/ARTARCH-654_repair_lp_gp_relations:2'
            ]]>
        </query>
    </changeset>
</changelog>
