<?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">

        <!-- 338 Dish MPEG2 Transport Stream .ts -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_video_338","file",".ts","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_V338.ts",3);;
            SELECT @newVid338:=LAST_INSERT_ID();;
        <!-- 339 Bell MXF.mxf -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_video_339","file",".mxf","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_V339.mxf",3);;
            SELECT @newVid339:=LAST_INSERT_ID();;
        <!-- 341 29.97 Broadcast Feature.mov -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_video_341","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_V341.mov",3);;
            SELECT @newVid341:=LAST_INSERT_ID();;
        <!-- 343 Clean Asset Feature.mov -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_video_343","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_V343.mov",3);;
            SELECT @newVid343:=LAST_INSERT_ID();;
        <!-- 345 4K / UHD Feature.mov -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_video_345","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_V345.mov",3);;
            SELECT @newVid345:=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 s.storage_drive_id, a.asset_type_id, s.initial_folder 
			FROM asset_type a
			INNER JOIN storage_drive_asset_folder s ON s.asset_type_id=@existingAssetTypeId
			WHERE a.asset_type_id IN (@newVid338, @newVid339, @newVid341, @newVid343, @newVid345)
		);;

        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            
        <!-- 50 rows affected for feature  -->
            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 IN ( "stored_video_338", "stored_video_339", "stored_video_341", "stored_video_343", "stored_video_345");;
        <!-- 5 rows affected  for feature  -->
            DELETE FROM asset_type 
            WHERE asset IN ( "stored_video_338", "stored_video_339", "stored_video_341", "stored_video_343", "stored_video_345");;

            </sql>
        </rollback>
    </changeSet>
    
    <changeSet id="2" author="npatel">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <!-- 340 Bell MXF Trailer.mxf -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_trailer_340","file",".mxf","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_T340.mxf",97);;
            SELECT @newVid340:=LAST_INSERT_ID();;
        <!-- 342 29.97 Broadcast Trailer.mov -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_trailer_342","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_T342.mov",97);;
            SELECT @newVid342:=LAST_INSERT_ID();;
        <!-- 344 Clean Asset Trailer.mov -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_trailer_344","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_T344.mov",97);;
            SELECT @newVid344:=LAST_INSERT_ID();;
        <!-- 346 4K / UHD Trailer.mov -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("stored_trailer_346","file",".mov","{upc(0,6)}/{upc(6,3)}/{upc}_{cd}_{track_id}_T346.mov",97);;
            SELECT @newVid346:=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 video format to the newly created one -->
		INSERT INTO storage_drive_asset_folder (storage_drive_id,asset_type_id,initial_folder)
		( SELECT s.storage_drive_id, a.asset_type_id, s.initial_folder 
			FROM asset_type a
			INNER JOIN storage_drive_asset_folder s ON s.asset_type_id=@existingAssetTypeId
			WHERE a.asset_type_id IN (@newVid340, @newVid342, @newVid344, @newVid346)
		);;

        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

        <!-- 40 rows affected for 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 IN ( "stored_trailer_340", "stored_trailer_342", "stored_trailer_344", "stored_trailer_346");;
        <!-- 4 rows affected  for trailer   -->
            DELETE FROM asset_type WHERE asset IN ( "stored_trailer_340", "stored_trailer_342", "stored_trailer_344", "stored_trailer_346");;

            </sql>
        </rollback>
    </changeSet>
    
    
    <changeSet id="3" author="npatel">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <!-- 347 23.98 Caption File.scc -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("caption_2398", "directory", ".scc","scc/{upc(0,6)}/{upc(6,3)}/{upc}", NULL);;
            SELECT @newCap2398:=LAST_INSERT_ID();;
        <!-- 29.97 DF Caption File (hour 1)  -->
            INSERT INTO asset_type (asset,file_type,extension,folder_structure,parent_asset_type_id) VALUES 
            ("df_caption_2997", "directory", ".scc", "scc/{upc(0,6)}/{upc(6,3)}/{upc}", NULL);;
            SELECT @newCap2997:=LAST_INSERT_ID();;
            
        <!-- retrieve storage drive id for S3. There is only 1 drive -->
            SELECT @existingDriveId:=sd.storage_drive_id
            FROM storage_drive sd 
            INNER JOIN `storage` s ON sd.storage_id = s.storage_id
            WHERE s.physical_location_id = 8;;   
            
        <!-- 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) VALUES
            (@existingDriveId, @newCap2398, 'caption_2398/' ),
            (@existingDriveId, @newCap2997, 'df_caption_2997/') ;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <!-- 2 rows affected for caption  -->
            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 IN ( "caption_2398", "df_caption_2997");;
        <!-- 2 rows affected  for caption  -->
            DELETE FROM asset_type WHERE asset IN ( "caption_2398", "df_caption_2997");;
       
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
