<?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-503_backfill_data:1" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT vendor_id, company, name, is_distributor, status, label_identifier
                            FROM vendor
                            WHERE vendor_id in (34146, 34143, 34138, 34133, 34126, 34125)
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (v:Vendor:Label:Orchard {id: toInteger(row.vendor_id)})
                            ON CREATE SET v.uuid = apoc.create.uuid()
                            SET v.name = TRIM(coalesce(row.company, row.name)),
                                v.isDistributor = row.is_distributor,
                                v.status = row.status,
                                v.vendorId = toInteger(row.vendor_id),
                                v.labelIdentifier = row.label_identifier,
                                v:Label:Orchard
                    ",
                    {batchSize:1000}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:2" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT subaccount_id, subaccount_name, vendor_id
                            FROM subaccount
                            WHERE subaccount_id in (54132)
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (sa:SubAccount:Subaccount:Label { id: toInteger(row.subaccount_id) })
                            SET  sa.name = TRIM(row.subaccount_name)
                        WITH *
                        MERGE (v:Vendor:Label:Orchard {id: toInteger(row.vendor_id) })
                        MERGE (v)-[:OWNS]->(sa)
                    ",
                    {batchSize:1000}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:3" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT project_id, vendor_id, subaccount_id, artist_id
                            FROM project
                            WHERE project_id in (4436717, 4436671, 4436620, 4436596, 4435762)
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (pr:Project {id: toInteger(row.project_id)})
                            SET pr.vendorId = toInteger(row.vendor_id),
                                pr.subaccountId = toInteger(row.subaccount_id),
                                pr.artistId = toInteger(row.artist_id),
                                pr.updated_at = datetime()
                        MERGE (v:Vendor:Label:Orchard {id: pr.vendorId})
                        MERGE (pr)-[:BELONGS_TO]-(v)
                        FOREACH (isSubAccount IN
                                            CASE WHEN pr.subaccountId <> 0 THEN [1] ELSE [] END |
                            MERGE (sa:SubAccount:Subaccount:Label {id: pr.subaccountId})
                            MERGE (pr)-[:BELONGS_TO]-(sa)
                            MERGE (v)-[:OWNS]->(sa)
                        )
                        FOREACH (isArtistInfo IN CASE WHEN pr.artistId <> 0 THEN [1] ELSE [] END |
                            MERGE (ai:ArtistInfo {id: pr.artistId})
                            MERGE (pr)-[:HAS_ARTIST]->(ai)
                        )
                        WITH pr, v
                        MATCH (pr)-[:HAS_ARTIST]->(ai)
                        WHERE ai.name IS NOT NULL
                        MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                            ON CREATE SET i.id = 1
                            ON MATCH SET i.id = i.id + 1
                        MERGE (lp:LabelParticipant:Orchard {
                            vendorId: pr.vendorId,
                            subaccountId: pr.subaccountId,
                            name: trim(apoc.text.regreplace(ai.name, '[ ]{2,}', ' '))
                        })
                        ON CREATE SET
                            lp.id = i.id,
                            lp.uuid = apoc.create.uuid(),
                            lp.createdAt = datetime(),
                            lp.modifiedAt = datetime()
                        MERGE (lp)-[:CREATED_FROM]->(ai)
                        MERGE (pr)-[:CREATED_FOR_PARTICIPANT]->(lp)
                        MERGE (v)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        FOREACH (hasSubAccount IN
                                        CASE WHEN NOT (lp.subaccountId = 0) THEN [1] ELSE [] END |
                            MERGE (sa:SubAccount:Subaccount:Label {id: lp.subaccountId})
                            MERGE (sa)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        )
                    ",
                    {batchSize:1000}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:4" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT ai.artist_id, ai.name, ai.vendor_id, v.is_distributor != \"Y\"
                            FROM artist_info ai
                            INNER JOIN vendor v ON ai.vendor_id = v.vendor_id
                            WHERE ai.artist_id in (2267574, 2267429, 2267424, 2267316, 2267314, 2266369, 2266370, 2266371, 2266368, 2266372, 2265303, 2266222, 2264474, 2264473, 2264472, 2264316, 2264314, 2264315, 2264014)
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (ai:ArtistInfo {id: toInteger(row.artist_id)})
                            SET ai.name = row.name,
                                ai.vendorId = toInteger(row.vendor_id),
                                ai.updated_at = datetime()
                        WITH ai, row
                        MERGE (v:Vendor:Label:Orchard {id: ai.vendorId})
                        MERGE (v)-[:HAS_ARTIST]->(ai)
                        FOREACH (notDistr IN CASE WHEN NOT row.is_distributor THEN [1] ELSE [] END |
                            MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                                ON CREATE SET i.id = 1
                                ON MATCH SET i.id = i.id + 1
                            MERGE (lp:LabelParticipant:Orchard {
                                name: trim(apoc.text.regreplace(ai.name, '[ ]{2,}', ' ')),
                                vendorId: toInteger(ai.vendorId),
                                subaccountId: 0
                            })
                                ON CREATE SET
                                    lp.id = i.id,
                                    lp.uuid = apoc.create.uuid(),
                                    lp.createdAt = datetime(),
                                    lp.modifiedAt = datetime()
                            MERGE (lp)-[cf:CREATED_FROM]->(ai)
                                ON CREATE SET cf.created_at = datetime()
                            MERGE (v)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        )
                        WITH ai
                        MATCH (ai)<-[:HAS_ARTIST]-(pr:Project)
                        WHERE pr.vendorId IS NOT NULL AND pr.subaccountId IS NOT NULL
                            AND NOT (pr)-[:CREATED_FOR_PARTICIPANT]->(:LabelParticipant)
                        MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                            ON CREATE SET i.id = 1
                            ON MATCH SET i.id = i.id + 1
                        MERGE (lp:LabelParticipant:Orchard {
                            vendorId: pr.vendorId,
                            subaccountId: pr.subaccountId,
                            name: trim(apoc.text.regreplace(ai.name, '[ ]{2,}', ' '))
                        })
                        ON CREATE SET
                            lp.id = i.id,
                            lp.uuid = apoc.create.uuid(),
                            lp.createdAt = datetime(),
                            lp.modifiedAt = datetime()
                        MERGE (pr)-[:CREATED_FOR_PARTICIPANT]->(lp)
                    ",
                    {batchSize:1}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:5" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT ra.release_artist_id, ra.release_id, ra.artist_name, ra.role, p.vendor_id, p.subaccount_id
                            FROM release_artist ra
                            INNER JOIN releases r on ra.release_id = r.release_id
                            INNER JOIN project p on r.project_id = p.project_id
                            WHERE ra.release_id IS NOT NULL
                            AND ra.release_artist_id IN (6378257, 6378233, 6378208, 6378209, 6378110, 6378111, 6377307, 6377308, 6377178, 6377179, 6377066, 6377067, 6377064, 6374943, 6374939, 6374938)
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (ra:ReleaseArtist {id: toInteger(row.release_artist_id)})
                            SET ra.productId = toInteger(row.release_id),
                                ra.name = row.artist_name,
                                ra.role = row.role,
                                ra.updated_at = datetime()
                        MERGE (p:Product:Orchard {id: ra.productId})
                        MERGE (p)-[:HAS_SOURCE_ARTIST]->(ra)
                        MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                          ON CREATE SET i.id = 1
                          ON MATCH SET i.id = i.id + 1
                        MERGE (
                          lp:LabelParticipant:Orchard {
                              name: trim(apoc.text.regreplace(ra.name, '[ ]{2,}', ' ')),
                              vendorId: row.vendor_id,
                              subaccountId: row.subaccount_id
                          }
                        )
                            ON CREATE SET
                                lp.id = i.id,
                                lp.uuid = apoc.create.uuid(),
                                lp.createdAt = datetime(),
                                lp.modifiedAt = datetime()
                        MERGE (lp)-[:PARTICIPATED_IN {participated_as: ra.role}]->(p)
                        MERGE (lp)-[cf:CREATED_FROM]->(ra)
                          ON CREATE SET cf.created_at = datetime()
                        MERGE (v:Vendor:Label:Orchard {id: lp.vendorId})
                        MERGE (v)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        FOREACH (hasSubAccount IN
                                        CASE WHEN NOT (lp.subaccountId = 0) THEN [1] ELSE [] END |
                            MERGE (sa:SubAccount:Subaccount:Label {id: lp.subaccountId})
                            MERGE (sa)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        )
                    ",
                    {batchSize:1}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:6" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT id, track_id, name, type
                            FROM track_artist ta
                            WHERE track_id IS NOT NULL
                            AND ta.id IN (89534426, 89534361, 89534196, 89533964, 89533965, 89533518, 89529523, 89529471, 89529464, 89529468, 89529469, 89529462, 89529461, 89529470, 89529465, 89529467, 89529463, 89529460, 89529466, 89529445, 89529422, 89529427, 89529426, 89529420, 89529423, 89529424, 89529431, 89529428, 89529429, 89529421, 89529430, 89529432, 89529425, 89529221, 89529216, 89529068, 89528958, 89528960, 89528965, 89528956, 89528964, 89528952, 89528953, 89528959, 89528962, 89528957, 89528954, 89528963, 89528961, 89528955, 89528944, 89528940, 89528945, 89528938, 89528943, 89528939, 89528947, 89528942, 89528948, 89528949, 89528946, 89528950, 89528951, 89528941, 89528867, 89528868, 89528579, 89528615, 89528580, 89528576, 89528583, 89528582, 89528584, 89528607, 89528608, 89528581, 89528612, 89528611, 89528614, 89528605, 89528610, 89528609, 89528616, 89528613, 89528577, 89528574, 89528578, 89528575, 89520017, 89518499, 89518498)
                            ORDER BY id
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (ta:TrackArtist {id: toInteger(row.id)})
                            SET ta.trackId = toInteger(row.track_id),
                                ta.name = row.name,
                                ta.type = row.type,
                                ta.updated_at = datetime()
                        WITH *
                        MATCH (t:Track:Orchard {id: ta.trackId})
                        MATCH (p:Product:Orchard)-[:INCLUDES]->(t)
                        MATCH (pr:Project)-[:INCLUDES]->(p)
                        WHERE exists(pr.subaccountId)
                        MERGE (t)-[:HAS_SOURCE_ARTIST]->(ta)
                        MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                            ON CREATE SET i.id = 1
                            ON MATCH SET i.id = i.id + 1
                        MERGE (
                            lp:LabelParticipant:Orchard {
                                name: trim(apoc.text.regreplace(ta.name, '[ ]{2,}', ' ')),
                                vendorId: pr.vendorId,
                                subaccountId: pr.subaccountId
                            }
                        )
                            ON CREATE SET
                                lp.id = i.id,
                                lp.uuid = apoc.create.uuid(),
                                lp.createdAt = datetime(),
                                lp.modifiedAt = datetime()
                        MERGE (lp)-[tp:PARTICIPATED_IN {participated_as: ta.type}]->(t)
                        MERGE (lp)-[cf:CREATED_FROM]->(ta)
                            ON CREATE SET cf.created_at = datetime()
                        MERGE (v:Vendor:Label:Orchard {id: lp.vendorId})
                        MERGE (v)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        FOREACH (hasSubAccount IN
                                        CASE WHEN not (lp.subaccountId = 0) THEN [1] ELSE [] END |
                            MERGE (sa:SubAccount:Subaccount:Label {id: lp.subaccountId})
                            MERGE (sa)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        )
                    ",
                    {batchSize:1}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:7" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT track_writer_id, unique_track_id, writer_name
                            FROM track_writer tw
                            WHERE unique_track_id IS NOT NULL
                            AND tw.track_writer_id IN (61365310, 61365307, 61365309, 61365308, 61361424, 61361437, 61361430, 61361463, 61361456, 61361457, 61361443, 61361421, 61361450, 61361462, 61361419, 61361449, 61361416, 61361451, 61361427, 61361439, 61361447, 61361442, 61361423, 61361428, 61361417, 61361458, 61361414, 61361434, 61361429, 61361438, 61361432, 61361415, 61361452, 61361448, 61361435, 61361445, 61361422, 61361453, 61361460, 61361441, 61361433, 61361418, 61361412, 61361461, 61361446, 61361440, 61361413, 61361426, 61361455, 61361436, 61361459, 61361431, 61361454, 61361444, 61361425, 61361420, 61361252, 61361251, 61361141, 61361025, 61361034, 61361033, 61361040, 61361022, 61361029, 61361042, 61361032, 61361031, 61361018, 61361019, 61361041, 61361036, 61361023, 61361045, 61361027, 61361037, 61361035, 61361039, 61361038, 61361024, 61361026, 61361021, 61361030, 61361020, 61361043, 61361044, 61361028, 61360951, 61360949, 61360948, 61360950, 61360834, 61360724, 61360721, 61360736, 61360722, 61360727, 61360723, 61360728, 61360731, 61360729, 61360718, 61360738, 61360730, 61360735, 61360734, 61360725, 61360733, 61360720, 61360717, 61360719, 61360726, 61360732, 61360737, 61351258)
                            ORDER BY track_writer_id
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (tw:TrackWriter {id: toInteger(row.track_writer_id)})
                            SET tw.uniqueTrackId = toInteger(row.unique_track_id),
                                tw.writerName = row.writer_name,
                                tw.updated_at = datetime()
                        WITH tw
                        MATCH (t:Track:Orchard {id: tw.uniqueTrackId})
                        MATCH (p:Product:Orchard)-[r:INCLUDES]->(t)
                        MATCH (pr:Project)-[r1:INCLUDES]->(p)
                        WHERE exists(pr.subaccountId)
                        MERGE (t)-[:HAS_SOURCE_ARTIST]->(tw)
                        MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                            ON CREATE SET i.id = 1
                            ON MATCH SET i.id = i.id + 1
                        MERGE (
                            lp:LabelParticipant:Orchard {
                                name: trim(apoc.text.regreplace(tw.writerName, '[ ]{2,}', ' ')),
                                vendorId: pr.vendorId,
                                subaccountId: pr.subaccountId
                            }
                        )
                            ON CREATE SET
                                lp.id = i.id,
                                lp.uuid = apoc.create.uuid(),
                                lp.createdAt = datetime(),
                                lp.modifiedAt = datetime()
                        MERGE (lp)-[:PARTICIPATED_IN {participated_as: 'track_writer'}]->(t)
                        MERGE (lp)-[cf:CREATED_FROM]->(tw)
                            ON CREATE SET cf.created_at = datetime()
                        MERGE (v:Vendor:Label:Orchard {id: lp.vendorId})
                        MERGE (v)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        FOREACH (hasSubAccount IN CASE WHEN not (lp.subaccountId = 0) THEN [1] ELSE [] END |
                            MERGE (sa:SubAccount:Subaccount:Label {id: lp.subaccountId})
                            MERGE (sa)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        )
                    ",
                    {batchSize:1}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:8" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT id, vendor_id, subaccount_id, orchard_artist_name, store_id, store_artist_id
                            FROM participant_identifier
                            WHERE id IN (2207321, 2207325, 2207361, 2207377)
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (pi:ParticipantIdentifier {id: toInteger(row.id)})
                            SET pi.vendorId = toInteger(row.vendor_id),
                                pi.subaccountId = toInteger(row.subaccount_id),
                                pi.name = row.orchard_artist_name,
                                pi.storeId = toInteger(row.store_id),
                                pi.storeArtistId = row.store_artist_id,
                                pi.updated_at = datetime()
                        MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                            ON CREATE SET i.id = 1
                            ON MATCH SET i.id = i.id + 1
                        MERGE (lp:LabelParticipant:Orchard {
                            name: row.orchard_artist_name,
                            vendorId: row.vendor_id,
                            subaccountId: row.subaccount_id
                        })
                            ON CREATE SET
                                lp.id = i.id,
                                lp.uuid = apoc.create.uuid(),
                                lp.createdAt = datetime(),
                                lp.modifiedAt = datetime()
                            SET lp.appleMusicId = CASE WHEN row.store_artist_id = '' THEN NULL ELSE toInteger(row.store_artist_id) END
                        MERGE (lp)-[cf:CREATED_FROM]->(pi)
                            ON CREATE SET cf.created_at = datetime()
                        MERGE (v:Vendor:Label:Orchard {id: lp.vendorId})
                        MERGE (v)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        FOREACH (hasSubAccount IN
                                        CASE WHEN lp.subaccountId <> 0 THEN [1] ELSE [] END |
                            MERGE (sa:SubAccount:Subaccount:Label {id: lp.subaccountId})
                            MERGE (sa)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        )
                    ",
                    {batchSize:1}
                );
            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-503_backfill_data:9" author="savva" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        WITH 'mysql_artrelations' as url_alias,
                        '
                            SELECT id, vendor_id, subaccount_id, orchard_artist_name, store_id, store_artist_id
                            FROM participant_identifier
                            WHERE id IN (2207320, 2207324, 2207362, 2207378)
                        ' as sql
                        CALL apoc.load.jdbc(url_alias, sql)
                        YIELD row
                        RETURN row
                    ",
                    "
                        MERGE (pi:ParticipantIdentifier {id: toInteger(row.id)})
                            SET pi.vendorId = toInteger(row.vendor_id),
                                pi.subaccountId = toInteger(row.subaccount_id),
                                pi.name = row.orchard_artist_name,
                                pi.storeId = toInteger(row.store_id),
                                pi.storeArtistId = row.store_artist_id,
                                pi.updated_at = datetime()
                        MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                            ON CREATE SET i.id = 1
                            ON MATCH SET i.id = i.id + 1
                        MERGE (lp:LabelParticipant:Orchard {
                            name: row.orchard_artist_name,
                            vendorId: row.vendor_id,
                            subaccountId: row.subaccount_id
                        })
                            ON CREATE SET
                                lp.id = i.id,
                                lp.uuid = apoc.create.uuid(),
                                lp.createdAt = datetime(),
                                lp.modifiedAt = datetime()
                            SET lp.spotifyId = CASE WHEN row.store_artist_id = '' THEN NULL ELSE row.store_artist_id END
                        MERGE (lp)-[cf:CREATED_FROM]->(pi)
                            ON CREATE SET cf.created_at = datetime()
                        WITH *
                        OPTIONAL MATCH (lp)-[r:REPRESENTS]-(oldGp:GlobalParticipant {spotifyId: row.old_store_artist_id})
                        OPTIONAL MATCH (gp:GlobalParticipant {spotifyId: row.store_artist_id})
                        FOREACH (oldGpExists IN CASE WHEN r IS NOT NULL THEN [1] ELSE [] END |
                            DELETE r
                        )
                        FOREACH (gpExists IN CASE WHEN gp IS NOT NULL THEN [1] ELSE [] END |
                            MERGE (gp)-[:REPRESENTS]->(lp)
                        )
                        MERGE (v:Vendor:Label:Orchard {id: lp.vendorId})
                        MERGE (v)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        FOREACH (hasSubAccount IN CASE WHEN lp.subaccountId <> 0 THEN [1] ELSE [] END |
                            MERGE (sa:SubAccount:Subaccount:Label {id: lp.subaccountId})
                            MERGE (sa)-[:HAS_LABEL_PARTICIPANT]->(lp)
                        )
                    ",
                    {batchSize:1}
                );
            ]]>
        </query>
    </changeset>
</changelog>
