<?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-5" author="jian">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE direct_delivery;;
            
            DROP PROCEDURE IF EXISTS sp_add_dd_asset_location;;

            CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_add_dd_asset_location(IN assetID INT, IN the_drive INT, IN the_status VARCHAR(50))
            BEGIN
                DECLARE physicalLoc INT;
                DECLARE EXIT HANDLER FOR SQLEXCEPTION
                BEGIN
                    SELECT al.asset_location_id, al.asset_id, al.storage_drive_id, al.status 
                    FROM asset_location al
                    INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                    INNER JOIN storage s ON sd.storage_id = s.storage_id
                    WHERE al.asset_id = assetID AND s.physical_location_id = physicalLoc;
                    ROLLBACK;
                END;

                START TRANSACTION;
                    SELECT s.physical_location_id INTO physicalLoc
                    FROM storage_drive sd
                    INNER JOIN storage s ON s.storage_id = sd.storage_id
                    WHERE sd.storage_drive_id = the_drive;
                    
                    INSERT INTO asset_location (asset_id, storage_drive_id, status, upload_date)
                    SELECT a.asset_id, the_drive, the_status, NOW()
                    FROM asset a
                    LEFT JOIN (asset_location al 
                        INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                        INNER JOIN storage s ON  sd.storage_id = s.storage_id
                    ) ON al.asset_id = a.asset_id
                        AND s.physical_location_id = physicalLoc
                    WHERE a.asset_id = assetID 
                        AND al.asset_id IS NULL;
                    
                    IF ROW_COUNT() > 0 THEN
                        SELECT LAST_INSERT_ID() as asset_location_id, 1 AS success;
                    ELSE
                        SELECT al.asset_location_id, al.asset_id, al.storage_drive_id, al.status, 0 AS success
                        FROM asset_location al
                        INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                        INNER JOIN storage s ON sd.storage_id = s.storage_id
                        WHERE al.asset_id = assetID and s.physical_location_id = physicalLoc;
                    END IF;
                COMMIT;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE direct_delivery;;

                DROP PROCEDURE IF EXISTS sp_add_dd_asset_location;;

                CREATE DEFINER='theorchard_dd'@'127.0.0.1' PROCEDURE sp_add_dd_asset_location(IN assetID INT, IN the_drive INT, IN the_status VARCHAR(50))
                BEGIN
                    DECLARE physicalLoc INT;

                    START TRANSACTION;
                        SELECT s.physical_location_id INTO physicalLoc
                        FROM storage_drive sd
                        INNER JOIN storage s ON s.storage_id = sd.storage_id
                        WHERE sd.storage_drive_id = the_drive;
                        
                        INSERT INTO asset_location (asset_id, storage_drive_id, status, upload_date)
                        SELECT a.asset_id, the_drive, the_status, NOW()
                        FROM asset a
                        LEFT JOIN (asset_location al 
                            INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                            INNER JOIN storage s ON  sd.storage_id = s.storage_id
                        ) ON al.asset_id = a.asset_id
                            AND s.physical_location_id = physicalLoc
                        WHERE a.asset_id = assetID 
                            AND al.asset_id IS NULL;
                        
                        IF ROW_COUNT() > 0 THEN
                            SELECT LAST_INSERT_ID() as asset_location_id, 1 AS success;
                        ELSE
                            SELECT al.asset_location_id, al.asset_id, al.storage_drive_id, al.status, 0 AS success
                            FROM asset_location al
                            INNER JOIN storage_drive sd ON sd.storage_drive_id = al.storage_drive_id
                            INNER JOIN storage s ON sd.storage_id = s.storage_id
                            WHERE al.asset_id = assetID and s.physical_location_id = physicalLoc;
                        END IF;
                    COMMIT;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
