<?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="101-4" author="jian">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE direct_delivery;;

            DROP PROCEDURE IF EXISTS sp_populate_sync_queue;;

            CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_populate_sync_queue(IN source_physical_loc INT, IN dest_physical_loc INT, IN asset_type_ids TEXT, IN lmt INT)
            BEGIN
                SET @stmtStr := '';
                SELECT CONCAT("SELECT al.asset_id, ",source_physical_loc," AS source_location_id, ",dest_physical_loc," AS dest_location_id, a.asset_type_id, al.asset_location_id,
                        IF(",dest_physical_loc,"=4, 9, IF(a.asset_type_id IN (3, 10, 358, 501, 502, 503, 504, 562, 563), 0, IF(MIN(eq.priority), MIN(eq.priority), 9))) AS rating
                    FROM asset_location al
                    INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                    INNER JOIN storage s ON s.storage_id = sd.storage_id
                    INNER JOIN asset a ON a.asset_id = al.asset_id
                    LEFT JOIN (asset_location al2
                       INNER JOIN storage_drive sd2 ON sd2.storage_drive_id = al2.storage_drive_id
                       INNER JOIN storage s2 ON s2.storage_id = sd2.storage_id AND s2.physical_location_id = ", dest_physical_loc, ") ON al2.asset_id = al.asset_id
                    LEFT JOIN encoding_queue_detail eqd ON eqd.upc = a.upc
                        AND eqd.status IN ('new', 'ready_to_encode', 'audio_missing', 'image_missing', 'video_missing')
                    LEFT JOIN encoding_queue eq ON eq.encoding_queue_id = eqd.encoding_queue_id
                    WHERE al.status = 'Y'
                        AND a.asset_type_id IN (", asset_type_ids, ")
                        AND s.physical_location_id = ", source_physical_loc, "
                        AND (al2.asset_id IS NULL OR al2.status = 'N')
                        AND a.link_for_asset_id IS NULL
                        AND al.upload_date < '2023-09-01'
                    GROUP BY al.asset_id, al2.storage_drive_id, al.storage_drive_id
                    ORDER BY rating
                    LIMIT ", lmt)
                    INTO @stmtStr;
                PREPARE stmt FROM @stmtStr;
                EXECUTE stmt;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE direct_delivery;;

                DROP PROCEDURE IF EXISTS sp_populate_sync_queue;;

                CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_populate_sync_queue(IN source_physical_loc INT, IN dest_physical_loc INT, IN asset_type_ids TEXT, IN lmt INT)
                BEGIN
                    SET @stmtStr := '';
                    SELECT CONCAT("SELECT al.asset_id, ",source_physical_loc," AS source_location_id, ",dest_physical_loc," AS dest_location_id, a.asset_type_id, al.asset_location_id,
                            IF(",dest_physical_loc,"=4, 9, IF(a.asset_type_id IN (3, 10, 358, 501, 502, 503, 504, 562, 563), 0, IF(MIN(eq.priority), MIN(eq.priority), 9))) AS rating
                        FROM asset_location al
                        INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                        INNER JOIN storage s ON s.storage_id = sd.storage_id
                        INNER JOIN asset a ON a.asset_id = al.asset_id
                        LEFT JOIN (asset_location al2
                           INNER JOIN storage_drive sd2 ON sd2.storage_drive_id = al2.storage_drive_id
                           INNER JOIN storage s2 ON s2.storage_id = sd2.storage_id AND s2.physical_location_id = ", dest_physical_loc, ") ON al2.asset_id = al.asset_id
                        LEFT JOIN encoding_queue_detail eqd ON eqd.upc = a.upc
                            AND eqd.status IN ('new', 'ready_to_encode', 'audio_missing', 'image_missing', 'video_missing')
                        LEFT JOIN encoding_queue eq ON eq.encoding_queue_id = eqd.encoding_queue_id
                        WHERE al.status = 'Y'
                            AND a.asset_type_id IN (", asset_type_ids, ")
                            AND s.physical_location_id = ", source_physical_loc, "
                            AND (al2.asset_id IS NULL OR al2.status = 'N')
                            AND a.link_for_asset_id IS NULL
                        GROUP BY al.asset_id, al2.storage_drive_id, al.storage_drive_id
                        ORDER BY rating
                        LIMIT ", lmt)
                        INTO @stmtStr;
                    PREPARE stmt FROM @stmtStr;
                    EXECUTE stmt;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
