<?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-571_cleanup_subaccounts:1" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        MATCH (sa:Subaccount { id: '0' })-[rel]-()
                        RETURN rel
                    ",
                    "
                        DELETE rel
                    ",
                    {
                        batchSize: 10000,
                        // Parallel: false is prone to deadlocking, so true with concurrency set to 0 causes
                        // See https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1768
                        parallel: true,
                        // This parameter has an OBOE - setting it to 0 will cause 1 thread to run.
                        // See https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1782
                        concurrency: 0,
                        retries: 0
                    }
                )
                YIELD failedOperations, errorMessages
                CALL apoc.util.validate(failedOperations > 0, "At least one sub-operation failed: %s", [reduce(all_errors = "", error IN keys(errorMessages) | all_errors + ", " + error)])
                RETURN 0
            ]]>
        </query>
    </changeset>

    <changeset id="ARTARCH-571_cleanup_subaccounts:2" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                MATCH (sa:Subaccount { id: "0" })
                DELETE sa
            ]]>
        </query>
    </changeset>

    <changeset id="ARTARCH-571_cleanup_subaccounts:3" author="owright" run-on-change="true">
        <query>
            <![CDATA[
                CALL apoc.periodic.iterate(
                    "
                        MATCH (sa:Subaccount)
                        WHERE sa.id <> toInteger(sa.id)
                        RETURN sa
                    ",
                    "
                        MERGE (targetSa:Subaccount:SubAccount:Label {
                            id: toInteger(sa.id)
                        })

                        WITH targetSa, sa
                        CALL apoc.refactor.mergeNodes(
                            [targetSa, sa],
                            {
                                properties: 'discard',
                                mergeRels: true
                            }
                        )
                        YIELD node
                        RETURN node
                    ",
                    {
                        batchSize: 1000,
                        // Parallel: false is prone to deadlocking, so true with concurrency set to 0 causes
                        // See https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1768
                        parallel: true,
                        // This parameter has an OBOE - setting it to 0 will cause 1 thread to run.
                        // See https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/1782
                        concurrency: 0,
                        retries: 0
                    }
                )
                YIELD failedOperations, errorMessages
                CALL apoc.util.validate(failedOperations > 0, "At least one sub-operation failed: %s", [reduce(all_errors = "", error IN keys(errorMessages) | all_errors + ", " + error)])
                RETURN 0
            ]]>
        </query>
    </changeset>
</changelog>
