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

            CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_get_asset_by_phy_loc(IN assetID INT, IN phys_loc_id INT)
            BEGIN
                SELECT upc, cd, track_id, clip_number, asset_type_id, original_physical_location, asset_location_id, link_for_asset_id
                FROM asset a
                LEFT JOIN (
                    SELECT al.*
                    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 
                    WHERE al.asset_id = assetID AND  s.physical_location_id = phys_loc_id
                ) AS al ON al.asset_id = a.`asset_id`
                WHERE a.asset_id = assetID;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP PROCEDURE IF EXISTS sp_get_asset_by_phy_loc;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
