<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/var/app/InputFileSchema.xsd">

    <!-- 67 track_writer_neo4j_only -->
    <changeset id="KDH-440-DELETE_track_writer_neo4j_only" author="npatel" neo4j-server="music-graph" run-always="true">
        <precondition>
            <sqlquery>
                <![CDATA[
                    SELECT id as track_writer_id, unique_track_id   
                    FROM facts.prod.track_writer where id in (
                        72304207, 72304198, 72304215, 72304254, 72304191, 72304194, 72304220, 72304189, 72304216, 72304185, 72304250, 72304197, 72304221, 72304200, 72304219, 72304203, 72304218, 72304225, 72304205, 72304183, 72304193, 72304255, 72304211, 72304195, 72304184, 72304259, 72304223, 72304258, 72304209, 72304192, 69026270, 72304199, 72304239, 72304226, 72304249, 72304206, 72304236, 72304224, 72304264, 72304245, 72304261, 72304202, 72304263, 72304228, 72304251, 72304248, 72304208, 72304213, 72304235, 72304256, 72304187, 72304262, 72304186, 72304246, 72304238, 72304214, 72304229, 72304210, 72304237, 72304190, 72304253
                    );
                ]]>
            </sqlquery>
        </precondition>
        
    <!-- same as /lambda-kinesis-to-neo4j/blob/master/lambda/kinesis-to-neo4j/src/logic/art_relations/queries/queries.py#L1469C1-L1469C28 but with event. prefix -->
        <cypherquery>
            <![CDATA[
                MATCH (tw:TrackWriter  {id: toInteger(event.TRACK_WRITER_ID)})
                MATCH (tw)-[cf:CREATED_FROM]-(lp:LabelParticipant)
                OPTIONAL MATCH (lp)-[pit:PARTICIPATED_IN {participated_as: 'track_writer'}]->
                    (t:Track:Orchard {id: toInteger(event.UNIQUE_TRACK_ID)})
                WITH *, [node IN [tw, lp, t] WHERE node IS NOT NULL] AS nodesToLock
                CALL apoc.lock.nodes(nodesToLock)
                OPTIONAL MATCH (lp)-[:CREATED_FROM]-
                    (sameTw:TrackWriter {uniqueTrackId: toInteger(event.UNIQUE_TRACK_ID)})
                WHERE sameTw.id <> toInteger(event.TRACK_WRITER_ID)
                FOREACH (noDup IN CASE WHEN sameTw IS NULL THEN [1] ELSE [] END |
                    DETACH DELETE pit, tw
                )
                FOREACH (dup IN CASE WHEN sameTw IS NOT NULL THEN [1] ELSE [] END |
                    DETACH DELETE tw
                )
            ]]>
        </cypherquery>
    </changeset>
</changelog>
