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

        <!--Insert new asset type for Trailer Subtitles itt-->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure) VALUES ('trailer_subtitles_itt','directory','.itt','trailer_itt/{upc(0,6)}/{upc(6,3)}/{upc}');;

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

        <!--retrieve asset type id of an already existing stored trailer asset type -->
            SELECT @existingIttAssetTypeId:=asset_type_id FROM asset_type WHERE asset='subtitles_itt';;

        <!--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, @newIttAssetTypeId, initial_folder FROM storage_drive_asset_folder 
            WHERE asset_type_id=@existingIttAssetTypeId;;

        <!--Insert new asset type for Trailer Subtitle srt-->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure) VALUES ('trailer_subtitles_srt','directory','.srt','trailer_srt/{upc(0,6)}/{upc(6,3)}/{upc}');;

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

        <!--retrieve asset type id of an already existing stored trailer asset type -->
            SELECT @existingSrtAssetTypeId:=asset_type_id FROM asset_type WHERE asset='subtitles_srt';;

        <!--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, @newSrtAssetTypeId, initial_folder FROM storage_drive_asset_folder 
            WHERE asset_type_id=@existingSrtAssetTypeId;;

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

            <!--rollback for Trailer Subtitle itt-->
            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 = 'trailer_subtitles_itt';;

            DELETE FROM asset_type WHERE asset = 'trailer_subtitles_itt';;
			
            <!--rollback for Trailer Subtitle srt-->
            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 = 'trailer_subtitles_srt';;

            DELETE FROM asset_type WHERE asset = 'trailer_subtitles_srt';;

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