<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="10104" author="jian">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE direct_delivery;;
            
            DROP PROCEDURE IF EXISTS `sp_get_batch_info`;;

            CREATE PROCEDURE `sp_get_batch_info`(IN batch_id INT)
            BEGIN
            SELECT db.dms_master_master_id, db.order_type, eqd.upc, db.remote_folder, eqd.encoding_queue_detail_id, eq.meta_update,
                db.third_party_credential_set_id
            FROM encoding_queue_detail eqd
            INNER JOIN encoding_queue eq ON eq.encoding_queue_id = eqd.encoding_queue_id
            INNER JOIN delivery_batch_detail dbd ON eqd.encoding_queue_detail_id = dbd.encoding_queue_detail_id
            INNER JOIN delivery_batch db ON db.delivery_batch_id = dbd.delivery_batch_id
            WHERE dbd.delivery_batch_id = batch_id
              AND eqd.status = 'delivered';
            END;;

            GRANT EXECUTE ON PROCEDURE direct_delivery.sp_get_batch_info TO 'lambda-cl-batch'@'%';;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE direct_delivery;;

                DROP PROCEDURE IF EXISTS `sp_get_batch_info`;;

                CREATE PROCEDURE `sp_get_batch_info`(IN batch_id INT)
                BEGIN
                SELECT db.dms_master_master_id, db.order_type, eqd.upc, db.remote_folder, eqd.encoding_queue_detail_id, eq.meta_update
                FROM encoding_queue_detail eqd
                INNER JOIN encoding_queue eq ON eqd.encoding_queue_id = eqd.encoding_queue_id
                INNER JOIN delivery_batch_detail dbd ON eqd.encoding_queue_detail_id = dbd.encoding_queue_detail_id
                INNER JOIN delivery_batch db ON db.delivery_batch_id = dbd.delivery_batch_id
                WHERE dbd.delivery_batch_id = batch_id
                  AND eqd.status = 'delivered';
                END;;

                GRANT EXECUTE ON PROCEDURE direct_delivery.sp_get_batch_info TO 'lambda-cl-batch'@'%';;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
