<?xml version="1.0" encoding="UTF-8"?>
<changelog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:/var/app/InputFileSchema.xsd">
    <changeset
         id="KDH-532_backfill_dd-search-batch-prod:1"
         author="npatel"
         run-always="true"
         topic="event.backfill.ddsearch.os"
         snowflake-account="orchard"
     >
    <!--  Note: property is_duplicate is missing so wont reset this property. -->
    <!--  Batch 1:  23349254 Rows from encoding_queue_detail_id >= 0 and encoding_queue_detail_id < 78000000 -->
    <!--  Batch 2:  26998994 Rows from encoding_queue_detail_id >= 78000000 and encoding_queue_detail_id < 105000000 -->
    <!--  Batch 3:  34999000 Rows from encoding_queue_detail_id >= 105000000 and encoding_queue_detail_id < 140000000 -->
    <!--  Batch 4:  34993764 Rows from encoding_queue_detail_id >= 140000000 and encoding_queue_detail_id < 175000000 -->
    <!--  Batch 5:  33622988 Rows from encoding_queue_detail_id >= 175000000 and encoding_queue_detail_id < 210000000 -->
    <!--  Batch 6:  33126133 Rows from encoding_queue_detail_id >= 210000000 and encoding_queue_detail_id < 245000000 failed partial -->
    <!--  Batch 7:  32292775 Rows from encoding_queue_detail_id >= 233595000 and encoding_queue_detail_id < 270000000 -->
    <!--  Batch 11:  33999524 Rows from encoding_queue_detail_id >= 345000000 and encoding_queue_detail_id < 379000000 -->
    <!--  Batch 12:  35999534 Rows from encoding_queue_detail_id >= 379000000 and encoding_queue_detail_id < 415000000 -->
    
        <precondition>
            <sqlquery>
                <![CDATA[
                    with all_data as (
                        select
                            eo.orchadmin_user_id as user_id,
                            TO_CHAR(CONVERT_TIMEZONE('America/New_York', 'UTC', eo.entry_date), 'YYYY-MM-DDTHH24:MI:SS') as created_at,
                            eq.priority as priority,
                            eq.encoder_id as encoder_id,
                            eq.encoding_queue_id as encoding_queue_id,
                            eq.encoding_order_id as order_id,
                            eq.encoding_order_type as order_type,
                            eq.meta_update = 'Y' as meta_update,
                            eqd.dms_master_master_id as store_id,
                            eqd.upc as upc,
                            eqd.encoding_queue_detail_id as encoding_queue_detail_id,
                            eqd.status as status,
                            TO_CHAR(CONVERT_TIMEZONE('America/New_York', 'UTC', eqd.encoding_started), 'YYYY-MM-DDTHH24:MI:SS') as encoding_started,
                            TO_CHAR(CONVERT_TIMEZONE('America/New_York', 'UTC', eqd.encoding_ended), 'YYYY-MM-DDTHH24:MI:SS') as encoding_ended,
                            eqd.error_log as error_log,
                            TO_CHAR(CONVERT_TIMEZONE('America/New_York', 'UTC', eqd.delivery_started), 'YYYY-MM-DDTHH24:MI:SS') as delivery_started,
                            TO_CHAR(CONVERT_TIMEZONE('America/New_York', 'UTC', eqd.delivery_ended), 'YYYY-MM-DDTHH24:MI:SS') as delivery_ended,
                            dds.encoding as encoding,
                            dds.delivery as delivery,
                            r.release_id as product_id,
                            r.display_upc as display_upc,
                        from ORCHARD_APP_REPORTING_V2.REPORTSDD_DIRECT_DELIVERY.ENCODING_QUEUE_DETAIL eqd
                        inner join ORCHARD_APP_REPORTING_V2.REPORTSDD_DIRECT_DELIVERY.ENCODING_QUEUE eq on eqd.encoding_queue_id = eq.encoding_queue_id
                        inner join ORCHARD_APP_REPORTING_V2.ART_RELATIONS_PROD_ART_RELATIONS.ENCODING_ORDER eo on eo.encoding_order_id = eq.encoding_order_id
                        inner join ORCHARD_APP_REPORTING_V2.REPORTSDD_DIRECT_DELIVERY.dms_delivery_spec dds  on dds.dms_master_master_id = eqd.dms_master_master_id AND dds.order_type = eq.encoding_order_type
                        inner join ORCHARD_APP_REPORTING_V2.ART_RELATIONS_PROD_ART_RELATIONS.RELEASES r on r.upc = eqd.upc
                        where
                            encoding_queue_detail_id >= 379000000 and encoding_queue_detail_id < 415000000
                    )
                    SELECT
                        TO_NUMBER(all_data.encoding_queue_detail_id) AS KEY,
                        OBJECT_CONSTRUCT(
                            'user_id',      TO_NUMBER(all_data.user_id),
                            'created_at',   TO_VARCHAR(all_data.created_at),
                            'priority',     TO_NUMBER(all_data.priority),
                            'encoder_id',   TO_NUMBER(all_data.encoder_id),
                            'order_id',     TO_NUMBER(all_data.order_id),
                            'order_type',   TO_VARCHAR(all_data.order_type),
                            'meta_update',  TO_BOOLEAN(all_data.meta_update),
                            'store_id',     TO_NUMBER(all_data.store_id),
                            'upc',          TO_NUMBER(all_data.upc),
                            'encoding_queue_id',        TO_NUMBER(all_data.encoding_queue_id),
                            'encoding_queue_detail_id', TO_NUMBER(all_data.encoding_queue_detail_id),
                            'status',           TO_VARCHAR(all_data.status),
                            'encoding_started', TO_VARCHAR(all_data.encoding_started),
                            'encoding_ended',   TO_VARCHAR(all_data.encoding_ended),
                            'error_log',        TO_VARCHAR(all_data.error_log),
                            'delivery_started', TO_VARCHAR(all_data.delivery_started),
                            'delivery_ended',   TO_VARCHAR(all_data.delivery_ended),
                            'encoding',     TO_VARCHAR(all_data.encoding),
                            'delivery',     TO_VARCHAR(all_data.delivery),
                            'product_id',   TO_NUMBER(all_data.product_id),
                            'display_upc',  TO_VARCHAR(all_data.display_upc)
                        ) AS VALUE
                    FROM all_data
                ]]>
            </sqlquery>
        </precondition>
    </changeset>
    
</changelog>
