<?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-614_participant_identifier_cleanup:1" author="savva" run-on-change="true">
        <query>
            WITH
              'mysql_artrelations' as url_alias,
              '
                  SELECT id
                  FROM participant_identifier
              ' as sql
            CALL apoc.load.jdbc(url_alias, sql)
            YIELD row
            MATCH (pi:ParticipantIdentifier {id: row.id})
                SET pi.synced = true;
        </query>
    </changeset>
    <changeset id="ARTARCH-614_participant_identifier_cleanup:2" author="savva" run-on-change="true">
        <query>
            CALL apoc.periodic.iterate(
                "
                    MATCH (pi:ParticipantIdentifier)
                    WHERE pi.synced IS NULL
                    RETURN pi
                ",
                "
                    DETACH DELETE pi
                ",
                {batchSize:1000, iterateList:true, parallel:false}
            );
        </query>
    </changeset>
    <changeset id="ARTARCH-614_participant_identifier_cleanup:3" author="savva" run-on-change="true">
        <query>
            <![CDATA[

                WITH range(0, 21) AS items, 100000 AS step
                UNWIND items AS i
                WITH step AS limit, i * step AS offset
                CALL apoc.periodic.iterate(
                "
                  WITH
                    'mysql_artrelations' as url_alias,
                    '
                        SELECT *
                        FROM participant_identifier
                        ORDER BY id
                        LIMIT ' + $limit + ' OFFSET ' + $offset
                    as sql
                  CALL apoc.load.jdbc(url_alias, sql)
                  YIELD row
                  return row
                ",
                "
                    WITH collect(row) AS rows
                    UNWIND range(0, size(rows) - 1) AS rowNumber
                    WITH rowNumber, size(rows) AS totalRows, rows[rowNumber] AS row
                    MERGE (pi:ParticipantIdentifier {id: toInteger(row.id)})
                      ON CREATE SET
                          pi.createdAt = datetime(),
                          pi.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                      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.lastModifiedAt = datetime(),
                          pi.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    WITH *, trim(row.store_artist_id) as store_artist_id_trimmed
                    MERGE (i:IncrementId {nodeName: 'LabelParticipant'})
                      ON CREATE SET i.id = 1
                      ON MATCH SET i.id = i.id + 1
                    MERGE (lp:LabelParticipant:Orchard {
                      normalizedName: toLower(trim(apoc.text.regreplace(row.orchard_artist_name, '[ ]+', ''))),
                      vendorId: row.vendor_id,
                      subaccountId: row.subaccount_id
                    })
                      ON CREATE SET
                          lp.id = i.id - totalRows + rowNumber + 1,
                          lp.uuid = apoc.create.uuid(),
                          lp.createdAt = datetime(),
                          lp.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                          lp.lastModifiedAt = datetime(),
                          lp.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                          lp.name = trim(apoc.text.regreplace(row.orchard_artist_name, '[ ]{2,}', ' '))
                    MERGE (lp)-[cf:CREATED_FROM]->(pi)
                      ON CREATE SET
                          cf.createdAt = datetime(),
                          cf.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                          cf.lastModifiedAt = datetime(),
                          cf.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    WITH *
                    FOREACH (store IN CASE WHEN row.store_id = 1 AND lp.appleMusicId IS NULL THEN [1] ELSE [] END |
                      SET lp.appleMusicId = CASE WHEN store_artist_id_trimmed = '' THEN NULL ELSE toInteger(store_artist_id_trimmed) END,
                          lp.lastModifiedAt = datetime(),
                          lp.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    )
                    FOREACH (store IN CASE WHEN row.store_id = 286 AND store_artist_id_trimmed =~ '[a-zA-Z0-9]+' AND lp.spotifyId IS NULL THEN [1] ELSE [] END |
                      SET lp.spotifyId = store_artist_id_trimmed,
                          lp.lastModifiedAt = datetime(),
                          lp.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    )
                    FOREACH (store IN CASE WHEN row.store_id = 286 AND store_artist_id_trimmed =~ '[a-zA-Z0-9]+' THEN [1] ELSE [] END |
                      MERGE (gp:GlobalParticipant {spotifyId: store_artist_id_trimmed})
                          ON CREATE SET
                              gp.id = apoc.create.uuid(),
                              gp.createdAt = datetime(),
                              gp.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                              gp.lastModifiedAt = datetime(),
                              gp.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                              gp.name = lp.name
                      MERGE (gp)-[r:REPRESENTS]->(lp)
                          ON CREATE SET
                              r.createdAt = datetime(),
                              r.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                              r.lastModifiedAt = datetime(),
                              r.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    )
                    WITH *
                    OPTIONAL MATCH (pi)<-[:CREATED_FROM]-(lp)-[oldR:REPRESENTS]-(oldGp:GlobalParticipant) WHERE pi.storeId = 286 AND oldGp.spotifyId <> store_artist_id_trimmed
                    DELETE oldR
                    MERGE (v:Vendor:Label:Orchard {id: lp.vendorId})
                      ON CREATE SET
                          v.createdAt = datetime(),
                          v.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                          v.lastModifiedAt = datetime(),
                          v.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    MERGE (v)-[hlp:HAS_LABEL_PARTICIPANT]->(lp)
                      ON CREATE SET
                          hlp.createdAt = datetime(),
                          hlp.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                          hlp.lastModifiedAt = datetime(),
                          hlp.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    FOREACH (hasSubAccount IN
                                  CASE WHEN lp.subaccountId <> 0 THEN [1] ELSE [] END |
                      MERGE (sa:SubAccount:Subaccount:Label {id: lp.subaccountId})
                          ON CREATE SET
                              sa.createdAt = datetime(),
                              sa.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                              sa.lastModifiedAt = datetime(),
                              sa.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                      MERGE (sa)-[shlp:HAS_LABEL_PARTICIPANT]->(lp)
                          ON CREATE SET
                              shlp.createdAt = datetime(),
                              shlp.createdBy = 'database/ARTARCH-614_participant_identifier_cleanup:3',
                              shlp.lastModifiedAt = datetime(),
                              shlp.lastModifiedBy = 'database/ARTARCH-614_participant_identifier_cleanup:3'
                    )
                ",
                    {
                        batchSize: 10000,
                        iterateList: true,
                        parallel: true,
                        concurrency: 1,
                        retries: 1,
                        params: {limit: limit, offset: offset}
                    }
                )
                YIELD failedOperations, errorMessages
                // For some reason calling apoc.util.validate directly doesn't actually fail the batch inside of apoc.periodic.iterate, but this does
                CALL apoc.do.when(
                    failedOperations > 0,
                    'CALL apoc.util.validate(true, "At least one sub-operation failed: %s", [reduce(all_errors = "", error IN keys(errorMessages) | all_errors + ", " + error)]) RETURN 0',
                    '',
                    { errorMessages: errorMessages }
                )
                YIELD value AS _
                RETURN 0;

            ]]>
        </query>
    </changeset>
    <changeset id="ARTARCH-614_participant_identifier_cleanup:4" author="savva" run-on-change="true">
        <query>
            CALL apoc.periodic.iterate(
                "
                    MATCH (pi:ParticipantIdentifier)
                    RETURN pi
                ",
                "
                    SET pi.synced = NULL
                ",
                {batchSize:1000, iterateList:true, parallel:false}
            );
        </query>
    </changeset>
</changelog>
