<?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="n-kumar">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

        <!--Insert new asset type for Hoopla 336 trailer-->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES ("stored_trailer_336","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_T336.mov",97);;

        <!--retrieve newly inserted asset type id-->
            SELECT @newAssetTypeId:=LAST_INSERT_ID();;

        <!--retrieve asset type id of an already existing stored trailer asset type -->
            SELECT @existingAssetTypeId:=asset_type_id FROM asset_type WHERE asset="stored_trailer_320";;

        <!--Copying identical storage locations for an existing stored trailer format to the newly created one -->
            INSERT INTO storage_drive_asset_folder(storage_drive_id,asset_type_id,initial_folder)
            SELECT storage_drive_id, @newAssetTypeId, initial_folder FROM storage_drive_asset_folder 
            WHERE asset_type_id=@existingAssetTypeId;;

        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

            <!--rollback for Hoopla 336 trailer-->
            DELETE sf.* FROM storage_drive_asset_folder sf 
            INNER JOIN asset_type a ON a.asset_type_id = sf.asset_type_id WHERE a.asset = "stored_trailer_336";;

            DELETE FROM asset_type WHERE asset = "stored_trailer_336";;

            </sql>
        </rollback>
    </changeSet>
    <changeSet id="2" author="n-kumar">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

        <!--Insert new asset type for Hoopla 336 video-->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES ("stored_video_336","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_V336.mov",3);;

        <!--retrieve newly inserted asset type id-->
            SELECT @newAssetTypeId:=LAST_INSERT_ID();;

        <!--retrieve asset type id of an already existing stored video asset type -->
            SELECT @existingAssetTypeId:=asset_type_id FROM asset_type WHERE asset="stored_video_320";;

        <!--Copying identical storage locations for an existing stored video format to the newly created one -->
            INSERT INTO storage_drive_asset_folder(storage_drive_id,asset_type_id,initial_folder)
            SELECT storage_drive_id, @newAssetTypeId, initial_folder FROM storage_drive_asset_folder 
            WHERE asset_type_id=@existingAssetTypeId;;

        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

            <!--rollback for Hoopla 336 video-->
            DELETE sf.* FROM storage_drive_asset_folder sf
            INNER JOIN asset_type a ON a.asset_type_id = sf.asset_type_id WHERE a.asset = "stored_video_336";;

            DELETE FROM asset_type WHERE asset = "stored_video_336";;

            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
