<?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="aakashpatkar">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
            DROP PROCEDURE IF EXISTS sp_get_dms_video_encoding_specs;;

            CREATE DEFINER='theorchard_dd'@'127.0.0.1'
            PROCEDURE sp_get_dms_video_encoding_specs(IN encodingProfileId INT, IN aspectRatio VARCHAR(20), IN res VARCHAR(20), IN framesPerSec VARCHAR(10), IN channelType VARCHAR(20))
            BEGIN
                SELECT id, `format`, filename, extension, aspect_ratio, resolution, fps, video_source, encoding_engine, channel_type,
                    IFNULL(fps, 0) AS sortByFps, IF(resolution = 'ANY', 0, 1) AS sortByRes
                FROM video_encoding_profile
                WHERE encoding_profile_id = encodingProfileId
                    AND aspect_ratio = aspectRatio
                    AND (resolution = res OR resolution = 'ANY')
                    AND (fps = framesPerSec OR fps IS NULL)
                    AND (channel_type = channelType OR channel_type IS NULL)
                ORDER BY sortByRes DESC, sortByFps DESC
                LIMIT 1;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
                <![CDATA[
                DROP PROCEDURE IF EXISTS sp_get_dms_video_encoding_specs;;

                CREATE DEFINER='theorchard_dd'@'127.0.0.1'
                PROCEDURE `sp_get_dms_video_encoding_specs`(IN encodingProfileId INT, IN aspectRatio VARCHAR(20), IN res VARCHAR(20), IN framesPerSec VARCHAR(10))
                BEGIN
                    SELECT id, `format`, filename, extension, aspect_ratio, resolution, fps, video_source, encoding_engine,
                        IFNULL(fps, 0) AS sortByFps, IF(resolution = 'ANY', 0, 1) AS sortByRes
                    FROM video_encoding_profile
                    WHERE encoding_profile_id = encodingProfileId
                        AND aspect_ratio = aspectRatio
                        AND (resolution = res OR resolution = 'ANY')
                        AND (fps = framesPerSec OR fps IS NULL)
                    ORDER BY sortByRes DESC, sortByFps DESC
                    LIMIT 1;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
