<?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="YT-246_backfill_missing_channels:1" author="ayermakov">
        <query>
            CALL apoc.periodic.iterate('
              WITH "
                SELECT DISTINCT
                  v.channel_id,
                  c.channel_display_name,
                  c.description,
                  c.published_at,
                  c.subscriber_count,
                  c.thumbnail_url,
                  c.view_count,
                  c.comment_count,
                  c.video_count,
                  yc.artist_id,
                  yc.vendor_id as label_id
                FROM facts.prod.staging_raw_youtube_video_report v
                LEFT JOIN facts.prod.dim_youtube_channel_names c
                  ON v.channel_id = c.channel_id
                LEFT JOIN orchard_app_reporting.art_relations.youtube_channel yc
                  ON v.channel_id = yc.youtube_channel_id
                WHERE isrc IS NOT NULL
                  AND content_type = \'PARTNER_UPLOADED\' AND licensor = \'theorchard\'
              " AS sql
              CALL apoc.load.jdbc("snowflake_orchard", sql) YIELD row RETURN row
              ',
              '
              WITH row
                    MERGE (c:Channel:YouTube {channelId: row.CHANNEL_ID})
                        ON CREATE SET
                            c.id = apoc.create.uuid(),
                            c.name = row.CHANNEL_DISPLAY_NAME,
                            c.description = row.DESCRIPTION,
                            c.publishedAt = datetime(row.PUBLISHED_AT),
                            c.subscriberCount = row.SUBSCRIBER_COUNT,
                            c.thumbnailUrl = row.THUMBNAIL_URL,
                            c.viewCount = row.VIEW_COUNT,
                            c.commentCount = row.COMMENT_COUNT,
                            c.videoCount = row.VIDEO_COUNT,
                            c.artistId = row.ARTIST_ID,
                            c.labelId = row.LABEL_ID,
                            c.createdAt = datetime(),
                            c.modifiedAt = datetime()
                ',
                {batchSize:1000, iterateList:true, parallel:false}
            );
        </query>
    </changeset>
</changelog>

