<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/var/app/InputFileSchema.xsd">
    <changeset id="DS-3644-create-constraint-on-label_participant-uuid:1" author="ibolshakov" run-always="true">
        <cypherquery>
          <![CDATA[
            CALL apoc.periodic.iterate(
              'MATCH (t:Track:Orchard) return t',
              '
                CALL apoc.lock.nodes([t])
                OPTIONAL MATCH (t)<-[rel:PARTICIPATED_IN]-(lp:LabelParticipant)
                OPTIONAL MATCH (t)-[:HAS_SOURCE_ARTIST]->(originalDataSource)
                WHERE
                    EXISTS((lp)-[:CREATED_FROM]->(originalDataSource))
                AND (
                        (
                            rel.participated_as = "track_writer"
                        AND
                            originalDataSource:TrackWriter
                        )
                    OR
                        (
                            rel.participated_as <> "track_writer"
                        AND
                            originalDataSource:TrackArtist
                        AND
                            originalDataSource.type = rel.participated_as
                        )
                )
                WITH
                    rel,
                    min(originalDataSource.id) AS orderInRole,
                    CASE rel.participated_as
                        WHEN "performer"    THEN 1
                        WHEN "featuring"    THEN 2
                        WHEN "remixer"      THEN 3
                        WHEN "producer"     THEN 4
                        WHEN "track_writer" THEN 5
                        ELSE                     6
                    END AS roleValue
                ORDER BY roleValue, orderInRole
                WITH collect(rel) AS sequence
                UNWIND range(0, size(sequence)-1) AS sequenceNumber
                WITH sequence[sequenceNumber] AS rel, sequenceNumber
                SET rel.sequenceNumber = sequenceNumber
              ',
              {batchSize: 30, iterateList: false, parallel: true, concurrency: 5}
            )
            YIELD total
            RETURN total
          ]]>
        </cypherquery>
    </changeset>
</changelog>
