<?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="PLATFORM-2302_backfill_distinct_names:1" author="npatel" run-on-change="true">
        <query>
            CALL apoc.periodic.iterate(
                "
                    WITH 'mysql_artrelations' as users,
                    '
                        SELECT  vc.auth0_user_id, TRIM(c.contact_first_name) AS fname, TRIM(c.contact_last_name) AS lname
                        FROM vend_contact vc
                        INNER JOIN art_relations.contact c ON c.contact_id = vc.contact_id
                        WHERE
                            auth0_user_id IS NOT NULL
                            AND c.contact_id > 191709
                            AND c.contact_first_name IS NOT NULL AND c.contact_first_name !=  \"\"
                        GROUP BY vc.auth0_user_id
                        HAVING COUNT(DISTINCT(TRIM(c.contact_first_name))) = 1 AND COUNT(DISTINCT(TRIM(c.contact_last_name))) = 1
                    ' as sql
                    CALL apoc.load.jdbc(users, sql)
                    YIELD row
                    RETURN row
                ",
                "
                    MATCH (i:Identity { auth0UserId: row.auth0_user_id })
                    SET i.firstName = row.fname, i.lastName = row.lname
                ",
                {batchSize:1000}
            );
        </query>
    </changeset>
</changelog>
