<?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="DATA-8174_create_chart_aggregated_rankings:1" author="veixar">
        <query>
            CALL apoc.periodic.iterate(
                "
                    MATCH (sr:SoundRecording)-[aro:AGGREGATED_RANKED_ON]->(c:Chart)
                    RETURN sr, aro, c
                ",
                "
                    MERGE (car:ChartAggregatedRanking:Chartmetric {soundRecordingId: sr.id, chartId: c.id})
                        ON CREATE SET
                            car.mostRecentPosition = aro.mostRecentPosition,
                            car.mostRecentTimestamp = date(aro.mostRecentTimestamp),
                            car.peakPosition = aro.peakPosition,
                            car.peakTimestamp = date(aro.peakTimestamp),
                            car.previousPosition = aro.previousPosition,
                            car.previousTimestamp = date(aro.previousTimestamp),
                            car.firstPosition = aro.firstPosition,
                            car.firstTimestamp = date(aro.firstTimestamp),
                            car.daysOnChart = aro.daysOnChart,
                            car.appearancesOnChart = aro.appearancesOnChart,
                            car.id = apoc.create.uuid(),
                            car.createdAt = datetime()
                    MERGE (sr)-[:HAS_AGGREGATED_CHART_RANKING]->(car)
                    MERGE (car)-[:APPEARS_ON_CHART]->(c)
                ",
                {batchSize:1000, iterateList:true, parallel:false}
            );
        </query>
    </changeset>
</changelog>
