<?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="1" author="npatel" runAlways="true" runOnChange="true">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
                USE `direct_delivery` ;;

                DROP PROCEDURE IF EXISTS `sp_get_direct_delivery_location`;;
                                
                CREATE DEFINER=`theorchard_dd`@`127.0.0.1` PROCEDURE `sp_get_direct_delivery_location`(IN enc_detail_id INT, IN phys_loc_id INT, IN cleanup INT)
                BEGIN
                    IF cleanup = 1 THEN
                        DELETE ddl.* 
                        FROM `direct_delivery`.direct_delivery_location ddl
                        INNER JOIN `direct_delivery`.encoding_queue_detail eqd ON ddl.encoding_queue_detail_id = eqd.encoding_queue_detail_id
                        INNER JOIN `direct_delivery`.storage_drive sd ON sd.storage_drive_id = ddl.storage_drive_id
                        INNER JOIN `direct_delivery`.storage s ON s.storage_id = sd.storage_id
                        WHERE s.physical_location_id <> phys_loc_id
                            AND eqd.encoding_queue_detail_id = enc_detail_id;
                    END IF;
                    SELECT IF(sdaf.initial_folder IS NULL, atp.initial_folder, sdaf.initial_folder) AS initial_folder, 
                        atp.file_type, atp.asset_type_id, atp.folder_structure,  INET_NTOA(s.local_ip) AS storage_ip, 
                        sd.storage_drive_id, s.connection_type, s.domain_name
                    FROM `direct_delivery`.encoding_queue_detail eqd
                    INNER JOIN `direct_delivery`.direct_delivery_location ddl ON ddl.encoding_queue_detail_id = eqd.encoding_queue_detail_id
                    INNER JOIN `direct_delivery`.storage_drive sd ON sd.storage_drive_id = ddl.storage_drive_id
                    INNER JOIN `direct_delivery`.storage s ON s.storage_id = sd.storage_id
                    INNER JOIN `direct_delivery`.asset_type atp ON atp.asset_type_id = ddl.asset_type_id
                    LEFT JOIN `direct_delivery`.storage_drive_asset_folder sdaf ON sdaf.storage_drive_id = sd.storage_drive_id AND ddl.asset_type_id = sdaf.asset_type_id 
                    WHERE eqd.encoding_queue_detail_id = enc_detail_id
                        AND s.physical_location_id = phys_loc_id;
                    END ;;
        ]]>
        </sql>
        
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
            
               USE `direct_delivery` ;;

                DROP PROCEDURE IF EXISTS `sp_get_direct_delivery_location`;;
                                
                CREATE DEFINER=`theorchard_dd`@`127.0.0.1` PROCEDURE `sp_get_direct_delivery_location`(IN enc_detail_id INT, IN phys_loc_id INT, IN cleanup INT)
                BEGIN
                    IF cleanup = 1 THEN
                        DELETE ddl.* 
                        FROM `direct_delivery`.direct_delivery_location ddl
                        INNER JOIN `direct_delivery`.encoding_queue_detail eqd ON ddl.encoding_queue_detail_id = eqd.encoding_queue_detail_id
                        INNER JOIN `direct_delivery`.storage_drive sd ON sd.storage_drive_id = ddl.storage_drive_id
                        INNER JOIN `direct_delivery`.storage s ON s.storage_id = sd.storage_id
                        WHERE s.physical_location_id <> phys_loc_id
                            AND eqd.encoding_queue_detail_id = enc_detail_id;
                    END IF;
                    SELECT IF(sdaf.initial_folder IS NULL, atp.initial_folder, sdaf.initial_folder) AS initial_folder, atp.file_type, atp.asset_type_id, atp.folder_structure, INET_NTOA(s.local_ip) AS storage_ip
                    FROM `direct_delivery`.encoding_queue_detail eqd
                    INNER JOIN `direct_delivery`.direct_delivery_location ddl ON ddl.encoding_queue_detail_id = eqd.encoding_queue_detail_id
                    INNER JOIN `direct_delivery`.storage_drive sd ON sd.storage_drive_id = ddl.storage_drive_id
                    INNER JOIN `direct_delivery`.storage s ON s.storage_id = sd.storage_id
                    INNER JOIN `direct_delivery`.asset_type atp ON atp.asset_type_id = ddl.asset_type_id
                    LEFT JOIN `direct_delivery`.storage_drive_asset_folder sdaf ON sdaf.storage_drive_id = sd.storage_drive_id AND ddl.asset_type_id = sdaf.asset_type_id 
                    WHERE eqd.encoding_queue_detail_id = enc_detail_id
                        AND s.physical_location_id = phys_loc_id;
                    END ;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
    
</databaseChangeLog>
