<?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[

            ALTER TABLE physical_location 
            ADD COLUMN location_type ENUM('drive', 's3') DEFAULT 'drive' NULL COMMENT 'What type of phy location it is. A drive or a S3.' ;;
            
            INSERT INTO physical_location (physical_location_id, physical_location, location_type) VALUES ( 8, 'amazon s3', 's3');; 

            ALTER TABLE storage 
            ADD COLUMN bucket_name VARCHAR(65) DEFAULT NULL COMMENT 'Name of S3 bucket.' ,
            CHANGE connection_type connection_type ENUM('ftp', 'sftp', 'local', 's3') DEFAULT 'ftp' NULL COMMENT 'Connection type used to connect to the storage machine from a remote machine. Possible values are FTP,  SFTP,  Local, S3' ;; 
            
            INSERT INTO storage (storage_name, local_ip, remote_ip, physical_location_id, state, bucket_name, connection_type) 
            VALUES 
            ( 's3 storage', NULL, NULL, '8', 'RW', 'prod-asset-storage', 's3');; 
            SET @storage_id = LAST_INSERT_ID();;
 
            INSERT INTO storage_drive(storage_id, drive_location, remote_initial_dir) 
            VALUES 
            ( @storage_id, NULL, NULL);;
            SET @storage_drive_id = LAST_INSERT_ID();;

            INSERT INTO storage_drive_asset_folder( storage_drive_id, asset_type_id, initial_folder) 
            VALUES 
            ( @storage_drive_id, '500', 'captions/');;

        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                
                DELETE sdaf
                FROM storage_drive_asset_folder sdaf 
                INNER JOIN storage_drive sd ON sdaf.storage_drive_id = sd.storage_drive_id
                INNER JOIN storage s ON s.storage_id = sd.storage_id
                WHERE s.physical_location_id = 8;;

                DELETE sd
                FROM storage_drive sd 
                INNER JOIN storage s ON s.storage_id = sd.storage_id
                WHERE s.physical_location_id = 8;;

                DELETE s FROM storage s WHERE s.physical_location_id = 8;;

                ALTER TABLE storage 
                CHANGE connection_type connection_type ENUM('ftp', 'sftp', 'local') DEFAULT 'ftp' NULL COMMENT 'Connection type used to connect to the storage machine from a remote machine. Possible values are FTP,  SFTP,  Local' ,
                DROP COLUMN bucket_name;; 
                
                DELETE pl FROM physical_location pl WHERE pl.physical_location_id = 8;;
                
                ALTER TABLE physical_location DROP COLUMN location_type;;
                
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
