<?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="DATA-8988_fix_lp_without_gp:1" author="veixar" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        MATCH (lp:LabelParticipant)
                        WHERE EXISTS(lp.spotifyId) AND NOT (lp)<-[:REPRESENTS]-(:GlobalParticipant)
                        RETURN lp
                    ",
                    "
                        WITH *, 'database/DATA-8988_fix_lp_without_gp:1' AS by
                        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 = by
                        MERGE (gp)-[gpToLp:REPRESENTS]->(lp)
                            ON CREATE SET
                                gpToLp.createdAt = datetime(),
                                gpToLp.createdBy = by
                    ",
                    {batchSize:1000, iterateList:true, parallel:true, concurrency: 5}
                )
                YIELD total, failedOperations, errorMessages
                CALL apoc.util.validate(failedOperations > 0, "At least one sub-operation failed: %s", [reduce(all_errors = "", error IN keys(errorMessages) | all_errors + ", " + error)])
                RETURN total;
            ]]>
        </query>
    </changeset>
</changelog>
