<?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="NR-180_nr_participants:1" author="rshield" run-on-change="true">
        <precondition>
            <sqlquery>
                <![CDATA[
                    SELECT
                        raca.compound_artist_id AS EXTERNALID,
                        COALESCE(an.name, ra.name) AS NAME,
                        COALESCE(split(ra.spotify_id, ':')[2], '') AS SPOTIFYID,
                        ra.itunes_id AS APPLEMUSICID
                        FROM awal_sax.sax.record_artist_comp_artists raca
                        INNER JOIN awal_sax.sax.recording_artists ra
                        ON raca.compound_artist_id = ra.id
                        INNER JOIN awal_sax.sax.recording_artists ra2
                        ON raca.artist_id = ra2.id
                        LEFT JOIN awal_sax.sax.artist_names an
                        ON raca.name_id = an.id
                        WHERE ra.spotify_id IS NOT NULL
                        GROUP BY 1,2,3,4
                        ORDER BY raca.compound_artist_id;
                ]]>
            </sqlquery>
        </precondition>
        <cypherquery>
            <![CDATA[
            MERGE(nrp:NrParticipant {externalId: event.EXTERNALID})
            ON CREATE SET
                nrp.id = apoc.create.uuid(),
                nrp.name = event.NAME,
                nrp.appleMusicId = event.APPLEMUSICID,
                nrp.spotifyId = event.SPOTIFYID,
                nrp.createdBy = 'database/NR-180_nr_participants',
                nrp.createdAt = datetime()
            ON MATCH SET
                nrp.name = event.NAME,
                nrp.appleMusicId = event.APPLEMUSICID,
                nrp.spotifyId = event.SPOTIFYID,
                nrp.lastModifiedAt = datetime(),
                nrp.lastModifiedBy = 'database/NR-180_nr_participants'
            MERGE (gp:GlobalParticipant {spotifyId: event.SPOTIFYID})
            ON CREATE SET
                gp.id = apoc.create.uuid(),
                gp.name = event.NAME,
                gp.appleMusicId = event.APPLEMUSICID,
                gp.spotifyId = event.SPOTIFYID,
                gp.createdBy = 'database/NR-180_nr_participants',
                gp.createdAt = datetime()
            ON MATCH SET
                gp.lastModifiedAt = datetime(),
                gp.lastModifiedBy = 'database/NR-180_nr_participants'
            MERGE (gp)-[r:REPRESENTS]->(nrp)
            ON CREATE SET
                r.createdBy = 'database/NR-180_nr_participants',
                r.createdAt = datetime()
            ON MATCH SET
                r.lastModifiedAt = datetime(),
                r.lastModifiedBy = 'database/NR-180_nr_participants'
            ]]>
        </cypherquery>
    </changeset>
    <changeset id="NR-180_nr_participants:2" author="rshield" run-on-change="true">
        <precondition>
            <sqlquery>
                <![CDATA[
                    SELECT
                        raca.compound_artist_id AS EXTERNALID,
                        COALESCE(an.name, ra.name) AS NAME,
                        ra.itunes_id AS APPLEMUSICID
                        FROM awal_sax.sax.record_artist_comp_artists raca
                        INNER JOIN awal_sax.sax.recording_artists ra
                        ON raca.compound_artist_id = ra.id
                        INNER JOIN awal_sax.sax.recording_artists ra2
                        ON raca.artist_id = ra2.id
                        LEFT JOIN awal_sax.sax.artist_names an
                        ON raca.name_id = an.id
                        WHERE ra.spotify_id IS NULL
                        GROUP BY 1,2,3
                        ORDER BY raca.compound_artist_id;
                ]]>
            </sqlquery>
        </precondition>
        <cypherquery>
            <![CDATA[
            MERGE(nrp:NrParticipant {externalId: event.EXTERNALID})
            ON CREATE SET
                nrp.id = apoc.create.uuid(),
                nrp.name = event.NAME,
                nrp.appleMusicId = event.APPLEMUSICID,
                nrp.createdBy = 'database/NR-180_nr_participants',
                nrp.createdAt = datetime()
            ON MATCH SET
                nrp.name = event.NAME,
                nrp.appleMusicId = event.APPLEMUSICID,
                nrp.lastModifiedAt = datetime(),
                nrp.lastModifiedBy = 'database/NR-180_nr_participants'
            ]]>
        </cypherquery>
    </changeset>
</changelog>