<?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="ARTARCH-600_fix_gsr_isrcs:1" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                // Find all GSRs with abnormal ISRCs, group them by normalized ISRC
                MATCH (gsr:GlobalSoundRecording)
                WHERE gsr.isrc <> toUpper(gsr.isrc)
                WITH toUpper(gsr.isrc) AS isrc, collect(gsr) AS gsrs

                // Find a normalized ISRC node if it exists, and add it to the list
                OPTIONAL MATCH (gsr:GlobalSoundRecording { isrc: isrc })
                WITH isrc, CASE WHEN gsr IS NULL THEN gsrs ELSE [gsr] + gsrs END AS gsrs

                // Merge together GSRs
                CALL apoc.refactor.mergeNodes(
                    gsrs,
                    {
                        properties: "discard",
                        mergeRels: true
                    }
                )
                YIELD node
                // Finally, make sure that we end up with the normalized ISRC on the node
                SET node.isrc = isrc;
            ]]>
        </query>
    </changeset>
</changelog>
