<?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-634_backfill_unlinked_participations:1" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        MATCH (p:Product:Orchard)-[:HAS_SOURCE_ARTIST]->(ra:ReleaseArtist)<-[:CREATED_FROM]-(lp:LabelParticipant)
                        WHERE NOT (lp)-[:PARTICIPATED_IN { participated_as: ra.role }]->(p)
                        RETURN *
                    ",
                    "
                        MERGE (lp)-[pi:PARTICIPATED_IN { participated_as: ra.role }]->(p)
                        ON CREATE SET
                            pi.createdAt = datetime(),
                            pi.createdBy = 'database/ARTARCH-643_backfill_unlinked_participations:1',
                            pi.lastModifiedAt = datetime(),
                            pi.lastModifiedBy = 'database/ARTARCH-643_backfill_unlinked_participations:1'
                    ",
                    {
                        batchSize: 1000,
                        parallel: false,
                        retries: 1
                    }
                )
                YIELD failedOperations, errorMessages, failedBatches, batch
                CALL apoc.util.validate(failedOperations > 0, "At least one sub-operation failed: %s", [reduce(all_errors = "", error IN keys(errorMessages) | all_errors + ", " + error)])
                CALL apoc.util.validate(failedBatches > 0, "At least one batch failed: %s", [reduce(all_errors = "", error IN keys(batch.errors) | all_errors + ", " + error)])
                RETURN 0
            ]]>
        </query>
    </changeset>

    <changeset id="ARTARCH-634_backfill_unlinked_participations:2" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        MATCH (t:Track:Orchard)-[:HAS_SOURCE_ARTIST]->(ta:TrackArtist)<-[:CREATED_FROM]-(lp:LabelParticipant)
                        WHERE NOT (lp)-[:PARTICIPATED_IN { participated_as: ta.type }]->(t)
                        RETURN *
                    ",
                    "
                        MERGE (lp)-[pi:PARTICIPATED_IN { participated_as: ta.type }]->(t)
                        ON CREATE SET
                            pi.createdAt = datetime(),
                            pi.createdBy = 'database/ARTARCH-643_backfill_unlinked_participations:2',
                            pi.lastModifiedAt = datetime(),
                            pi.lastModifiedBy = 'database/ARTARCH-643_backfill_unlinked_participations:2'
                    ",
                    {
                        batchSize: 1000,
                        parallel: false,
                        retries: 1
                    }
                )
                YIELD failedOperations, errorMessages, failedBatches, batch
                CALL apoc.util.validate(failedOperations > 0, "At least one sub-operation failed: %s", [reduce(all_errors = "", error IN keys(errorMessages) | all_errors + ", " + error)])
                CALL apoc.util.validate(failedBatches > 0, "At least one batch failed: %s", [reduce(all_errors = "", error IN keys(batch.errors) | all_errors + ", " + error)])
                RETURN 0
            ]]>
        </query>
    </changeset>

    <changeset id="ARTARCH-634_backfill_unlinked_participations:3" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        MATCH (t:Track:Orchard)-[:HAS_SOURCE_ARTIST]->(tw:TrackWriter)<-[:CREATED_FROM]-(lp:LabelParticipant)
                        WHERE NOT (lp)-[:PARTICIPATED_IN { participated_as: 'track_writer' }]->(t)
                        RETURN *
                    ",
                    "
                        MERGE (lp)-[pi:PARTICIPATED_IN { participated_as: 'track_writer' }]->(t)
                        ON CREATE SET
                            pi.createdAt = datetime(),
                            pi.createdBy = 'database/ARTARCH-643_backfill_unlinked_participations:3',
                            pi.lastModifiedAt = datetime(),
                            pi.lastModifiedBy = 'database/ARTARCH-643_backfill_unlinked_participations:3'
                    ",
                    {
                        batchSize: 1000,
                        parallel: false,
                        retries: 1
                    }
                )
                YIELD failedOperations, errorMessages, failedBatches, batch
                CALL apoc.util.validate(failedOperations > 0, "At least one sub-operation failed: %s", [reduce(all_errors = "", error IN keys(errorMessages) | all_errors + ", " + error)])
                CALL apoc.util.validate(failedBatches > 0, "At least one batch failed: %s", [reduce(all_errors = "", error IN keys(batch.errors) | all_errors + ", " + error)])
                RETURN 0
            ]]>
        </query>
    </changeset>
</changelog>
