<?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-262_backfill_from_art_relations:1" author="savva" run-on-change="true">
        <query>
            CALL apoc.log.info("Starting run of artist_info ingest at %s", [datetime()]);
        </query>
        <query>
            CALL apoc.periodic.iterate(
                "
                    WITH 'mysql_artrelations' as url_alias,
                    '
                        SELECT ai.artist_id, ai.name, ai.vendor_id
                        FROM artist_info ai
                        INNER JOIN vendor v ON ai.vendor_id = v.vendor_id
                        WHERE ai.vendor_id IS NOT NULL AND v.is_distributor != \"Y\"
                    ' 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()
                    MERGE (v:Vendor {id: ai.vendorId})
                    MERGE (v)-[:HAS_ARTIST]->(ai)
                    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()
                    MERGE (lp)-[cf:CREATED_FROM]->(ai)
                        ON CREATE SET cf.created_at = datetime()
                ",
                {batchSize:1000}
            );
        </query>
        <query>
            CALL apoc.log.info("Finished artist_info ingest at %s", [datetime()]);
        </query>
    </changeset>
    <changeset id="ARTARCH-262_backfill_from_art_relations:2" author="savva" run-on-change="true">
        <query>
            CALL apoc.log.info("Starting run of project ingest at %s", [datetime()]);
        </query>
        <query>
            CALL apoc.periodic.iterate(
                "
                    WITH 'mysql_artrelations' as url_alias,
                    '
                        SELECT project_id, vendor_id, subaccount_id, artist_id
                        FROM project
                        WHERE vendor_id IS NOT NULL
                    ' 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 {id: pr.vendorId})
                    MERGE (pr)-[:BELONGS_TO]->(v)
                    FOREACH (isSubAccount IN CASE WHEN not (pr.subaccountId = 0) THEN [1] ELSE [] END |
                        MERGE (sa:SubAccount {id: pr.subaccountId})
                        MERGE (pr)-[:BELONGS_TO]->(sa)
                        MERGE (v)-[:OWNS]->(sa)
                    )
                    FOREACH (isArtistInfo IN CASE WHEN not (pr.artistId = 0) THEN [1] ELSE [] END |
                        MERGE (ai:ArtistInfo {id: pr.artistId})
                        MERGE (pr)-[:HAS_ARTIST]->(ai)
                    )
                ",
                {batchSize:1000}
            );
        </query>
        <query>
            CALL apoc.log.info("Finished project ingest at %s", [datetime()]);
        </query>
    </changeset>
    <changeset id="ARTARCH-262_backfill_from_art_relations:3" author="savva" run-on-change="true">
        <query>
            CALL apoc.log.info("Starting run of releases ingest at %s", [datetime()]);
        </query>
        <query>
            CALL apoc.periodic.iterate(
                "
                    WITH 'mysql_artrelations' as url_alias,
                    '
                        SELECT release_id, upc, display_upc, project_id, artist_id, deletions
                        FROM releases
                        WHERE project_id IS NOT NULL
                    ' as sql
                    CALL apoc.load.jdbc(url_alias, sql)
                    YIELD row
                    RETURN row",
                "
                    MERGE (p:Product:Orchard {id: toInteger(row.release_id)})
                        SET p.projectId = toInteger(row.project_id),
                            p.upc = row.upc,
                            p.displayUpc = row.display_upc,
                            p.deletions = (row.deletions = 'Y'),
                            p.updated_at = datetime()
                    MERGE (pr:Project {id: p.projectId})
                    MERGE (pr)-[r:INCLUDES]->(p)
                ",
                {batchSize:1000}
            );
        </query>
        <query>
            CALL apoc.log.info("Finished releases ingest at %s", [datetime()]);
        </query>
    </changeset>
    <changeset id="ARTARCH-262_backfill_from_art_relations:4" author="savva" run-on-change="true">
        <query>
            CALL apoc.log.info("Starting run of release_artist ingest at %s", [datetime()]);
        </query>
        <query>
            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
                    ' 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: toInteger(row.vendor_id),
                            subaccountId: toInteger(row.subaccount_id)
                        }
                    )
                        ON CREATE SET
                            lp.id = i.id,
                            lp.uuid = apoc.create.uuid()
                    MERGE (lp)-[:PARTICIPATED_IN {participated_as: ra.role}]->(p)
                    MERGE (lp)-[cf:CREATED_FROM]->(ra)
                        ON CREATE SET cf.created_at = datetime()
                ",
                {batchSize:1000}
            );
        </query>
        <query>
            CALL apoc.log.info("Finished release_artist ingest at %s", [datetime()]);
        </query>
    </changeset>
    <changeset id="ARTARCH-262_backfill_from_art_relations:5" author="savva" run-on-change="true">
        <query>
            CALL apoc.log.info("Starting run of participant_identifier for apple ingest at %s", [datetime()]);
        </query>
        <query>
            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 store_id = 1
                    ' as sql
                    CALL apoc.load.jdbc(url_alias, sql)
                    YIELD row
                    RETURN row
                ",
                "
                    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(row.orchard_artist_name, '[ ]{2,}', ' ')),
                            vendorId: toInteger(row.vendor_id),
                            subaccountId: toInteger(row.subaccount_id)
                        }
                    )
                        ON CREATE SET
                            lp.id = i.id,
                            lp.uuid = apoc.create.uuid()
                    SET lp.appleMusicId = toInteger(row.store_artist_id)
                ",
                {batchSize:1000}
            );
        </query>
        <query>
            CALL apoc.log.info("Finished participant_identifier for apple ingest at %s", [datetime()]);
        </query>
    </changeset>
    <changeset id="ARTARCH-262_backfill_from_art_relations:6" author="savva" run-on-change="true">
        <query>
            CALL apoc.log.info("Starting run of participant_identifier for spotify ingest at %s", [datetime()]);
        </query>
        <query>
            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 store_id = 286
                    ' as sql
                    CALL apoc.load.jdbc(url_alias, sql)
                    YIELD row
                    RETURN row
                ",
                "
                    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(row.orchard_artist_name, '[ ]{2,}', ' ')),
                            vendorId: toInteger(row.vendor_id),
                            subaccountId: toInteger(row.subaccount_id)
                        }
                    )
                        ON CREATE SET
                            lp.id = i.id,
                            lp.uuid = apoc.create.uuid()
                    SET lp.spotifyId = row.store_artist_id
                ",
                {batchSize:1000}
            );
        </query>
        <query>
            CALL apoc.log.info("Finished participant_identifier for spotify ingest at %s", [datetime()]);
        </query>
    </changeset>
</changelog>
