<?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="ssavva">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE art_relations;;

            CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE sp_subaccount_artist_to_label_participant(
                IN current_project_id INT,
                IN current_release_subaccount_id INT
            )
            BEGIN
                DECLARE release_vendor_id INT DEFAULT NULL;
                DECLARE release_artist_name VARCHAR(255) DEFAULT NULL;
                DECLARE found_participant_id INT DEFAULT NULL;

                IF current_release_subaccount_id IS NOT NULL THEN

                    # Get vendor, sub-account and related artist for the given release
                    SELECT p.vendor_id, ai.name
                    INTO release_vendor_id, release_artist_name
                    FROM project p
                    INNER JOIN artist_info ai on p.artist_id = ai.artist_id
                    WHERE p.project_id = current_project_id;

                    # Find corresponding label participant by name if exists
                    SELECT id
                    INTO found_participant_id
                    FROM label_participant
                    WHERE vendor_id = release_vendor_id
                      AND subaccount_id <=> current_release_subaccount_id
                      AND TRIM(name) = TRIM(release_artist_name);

                    # If participant doesn't exist then create a new one
                    IF found_participant_id IS NULL THEN
                        INSERT INTO label_participant (name, vendor_id, subaccount_id)
                        VALUES (TRIM(release_artist_name), release_vendor_id, current_release_subaccount_id);
                    END IF;

                END IF;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE art_relations;;

                DROP PROCEDURE IF EXISTS sp_subaccount_artist_to_label_participant;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
    <changeSet id="2" author="ssavva">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE art_relations;;

            DROP TRIGGER IF EXISTS after_insert_release;;

            CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_insert_release
            AFTER INSERT ON releases
            FOR EACH ROW
            BEGIN
                INSERT INTO `cloudsearch`.`releases` (release_id, last_updated)
                VALUES (NEW.release_id, NOW())
                ON DUPLICATE KEY UPDATE last_updated = NEW.last_updated;

                INSERT INTO `art_relations_log`.`releases_log` SET
                    `log_timestamp` = NOW(),
                    `log_action` = 'insert',
                    `release_id` = NEW.`release_id`,
                    `last_updated` = NEW.`last_updated`,
                    `upc` = NEW.`upc`,
                    `display_upc` = NEW.`display_upc`,
                    `artist_id` = NEW.`artist_id`,
                    `subaccount_id` = NEW.`subaccount_id`,
                    `vendor_release_identifier` = NEW.`vendor_release_identifier`,
                    `release_name` = NEW.`release_name`,
                    `label` = NEW.`label`,
                    `description` = NEW.`description`,
                    `release_date` = NEW.`release_date`,
                    `listprice` = NEW.`listprice`,
                    `sellprice` = NEW.`sellprice`,
                    `display` = NEW.`display`,
                    `deletions` = NEW.`deletions`,
                    `distribution` = NEW.`distribution`,
                    `applfees` = NEW.`applfees`,
                    `referral_id` = NEW.`referral_id`,
                    `date_added_REMOVE` = NEW.`date_added_REMOVE`,
                    `genre_id` = NEW.`genre_id`,
                    `format` = NEW.`format`,
                    `distribution_format_id` = NEW.`distribution_format_id`,
                    `orig_release_year` = NEW.`orig_release_year`,
                    `promocode` = NEW.`promocode`,
                    `c_line` = NEW.`c_line`,
                    `manufacturer_upc` = NEW.`manufacturer_upc`,
                    `album_download` = NEW.`album_download`,
                    `download_price` = NEW.`download_price`,
                    `priority` = NEW.`priority`,
                    `territory` = NEW.`territory`,
                    `sale_start_date` = NEW.`sale_start_date`,
                    `vod_start_date` = NEW.`vod_start_date`,
                    `original_release_date` = NEW.`original_release_date`,
                    `vendor_catalog_number` = NEW.`vendor_catalog_number`,
                    `language_id` = NEW.`language_id`,
                    `promobox_no` = NEW.`promobox_no`,
                    `additional_id` = NEW.`additional_id`,
                    `dracatalog` = NEW.`dracatalog`,
                    `channel_id` = NEW.`channel_id`,
                    `product_type_id` = NEW.`product_type_id`,
                    `trackdown_black_list` = NEW.`trackdown_black_list`,
                    `not_for_distribution` = NEW.`not_for_distribution`,
                    `europe_public_domain` = NEW.`europe_public_domain`,
                    `karaoke` = NEW.`karaoke`,
                    `unknown_artist` = NEW.`unknown_artist`,
                    `new_release` = NEW.`new_release`,
                    `product_format` = NEW.`product_format`,
                    `product_code` = NEW.`product_code`,
                    `special_instructions` = NEW.`special_instructions`,
                    `promos_number` = NEW.`promos_number`,
                    `promo_upc` = NEW.`promo_upc`,
                    `sync_only` = NEW.`sync_only`,
                    `country_of_origin` = NEW.`country_of_origin`,
                    `short_synopsis` = NEW.`short_synopsis`,
                    `keywords` = NEW.`keywords`,
                    `production_co` = NEW.`production_co`,
                    `preorder_date` = NEW.`preorder_date`,
                    `amd_id` = NEW.`amd_id`,
                    `wholesale_price_tier` = NEW.`wholesale_price_tier`,
                    `season` = NEW.`season`,
                    `episode` = NEW.`episode`,
                    `production_number` = NEW.`production_number`,
                    `network` = NEW.`network`,
                    `genre_note` = NEW.`genre_note`,
                    `physical_release_date` = NEW.`physical_release_date`,
                    `theatrical_release_date` = NEW.`theatrical_release_date`,
                    `original_digital_release_date` = NEW.`original_digital_release_date`,
                    `est` = NEW.`est`,
                    `vod` = NEW.`vod`,
                    `rsd_comment` = NEW.`rsd_comment`,
                    `int_mkt_manager` = NEW.`int_mkt_manager`,
                    `tms_id` = NEW.`tms_id`,
                    `brand` = NEW.`brand`,
                    `digital_only` = NEW.`digital_only`,
                    `product_subtype_id` = NEW.`product_subtype_id`,
                    `version` = NEW.`version`,
                    `ingestion_completed` = NEW.`ingestion_completed`,
                    `release_status` = NEW.`release_status`,
                    `meta_language` = NEW.`meta_language`,
                    `itunes_previewable` = NEW.`itunes_previewable`,
                    `subtitle_language_id` = NEW.`subtitle_language_id`,
                    `compilation` = NEW.`compilation`,
                    `project_id` = NEW.`project_id`,
                    `p_line` = NEW.`p_line`
                    ;

                CALL sp_subaccount_artist_to_label_participant(NEW.project_id, NEW.subaccount_id);

            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE art_relations;;

                DROP TRIGGER IF EXISTS after_insert_release;;

                CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_insert_release
                AFTER INSERT ON releases
                FOR EACH ROW
                BEGIN
                    INSERT INTO `cloudsearch`.`releases` (release_id, last_updated)
                    VALUES (NEW.release_id, NOW())
                    ON DUPLICATE KEY UPDATE last_updated = NEW.last_updated;

                    INSERT INTO `art_relations_log`.`releases_log` SET
                        `log_timestamp` = NOW(),
                        `log_action` = 'insert',
                        `release_id` = NEW.`release_id`,
                        `last_updated` = NEW.`last_updated`,
                        `upc` = NEW.`upc`,
                        `display_upc` = NEW.`display_upc`,
                        `artist_id` = NEW.`artist_id`,
                        `subaccount_id` = NEW.`subaccount_id`,
                        `vendor_release_identifier` = NEW.`vendor_release_identifier`,
                        `release_name` = NEW.`release_name`,
                        `label` = NEW.`label`,
                        `description` = NEW.`description`,
                        `release_date` = NEW.`release_date`,
                        `listprice` = NEW.`listprice`,
                        `sellprice` = NEW.`sellprice`,
                        `display` = NEW.`display`,
                        `deletions` = NEW.`deletions`,
                        `distribution` = NEW.`distribution`,
                        `applfees` = NEW.`applfees`,
                        `referral_id` = NEW.`referral_id`,
                        `date_added_REMOVE` = NEW.`date_added_REMOVE`,
                        `genre_id` = NEW.`genre_id`,
                        `format` = NEW.`format`,
                        `distribution_format_id` = NEW.`distribution_format_id`,
                        `orig_release_year` = NEW.`orig_release_year`,
                        `promocode` = NEW.`promocode`,
                        `c_line` = NEW.`c_line`,
                        `manufacturer_upc` = NEW.`manufacturer_upc`,
                        `album_download` = NEW.`album_download`,
                        `download_price` = NEW.`download_price`,
                        `priority` = NEW.`priority`,
                        `territory` = NEW.`territory`,
                        `sale_start_date` = NEW.`sale_start_date`,
                        `vod_start_date` = NEW.`vod_start_date`,
                        `original_release_date` = NEW.`original_release_date`,
                        `vendor_catalog_number` = NEW.`vendor_catalog_number`,
                        `language_id` = NEW.`language_id`,
                        `promobox_no` = NEW.`promobox_no`,
                        `additional_id` = NEW.`additional_id`,
                        `dracatalog` = NEW.`dracatalog`,
                        `channel_id` = NEW.`channel_id`,
                        `product_type_id` = NEW.`product_type_id`,
                        `trackdown_black_list` = NEW.`trackdown_black_list`,
                        `not_for_distribution` = NEW.`not_for_distribution`,
                        `europe_public_domain` = NEW.`europe_public_domain`,
                        `karaoke` = NEW.`karaoke`,
                        `unknown_artist` = NEW.`unknown_artist`,
                        `new_release` = NEW.`new_release`,
                        `product_format` = NEW.`product_format`,
                        `product_code` = NEW.`product_code`,
                        `special_instructions` = NEW.`special_instructions`,
                        `promos_number` = NEW.`promos_number`,
                        `promo_upc` = NEW.`promo_upc`,
                        `sync_only` = NEW.`sync_only`,
                        `country_of_origin` = NEW.`country_of_origin`,
                        `short_synopsis` = NEW.`short_synopsis`,
                        `keywords` = NEW.`keywords`,
                        `production_co` = NEW.`production_co`,
                        `preorder_date` = NEW.`preorder_date`,
                        `amd_id` = NEW.`amd_id`,
                        `wholesale_price_tier` = NEW.`wholesale_price_tier`,
                        `season` = NEW.`season`,
                        `episode` = NEW.`episode`,
                        `production_number` = NEW.`production_number`,
                        `network` = NEW.`network`,
                        `genre_note` = NEW.`genre_note`,
                        `physical_release_date` = NEW.`physical_release_date`,
                        `theatrical_release_date` = NEW.`theatrical_release_date`,
                        `original_digital_release_date` = NEW.`original_digital_release_date`,
                        `est` = NEW.`est`,
                        `vod` = NEW.`vod`,
                        `rsd_comment` = NEW.`rsd_comment`,
                        `int_mkt_manager` = NEW.`int_mkt_manager`,
                        `tms_id` = NEW.`tms_id`,
                        `brand` = NEW.`brand`,
                        `digital_only` = NEW.`digital_only`,
                        `product_subtype_id` = NEW.`product_subtype_id`,
                        `version` = NEW.`version`,
                        `ingestion_completed` = NEW.`ingestion_completed`,
                        `release_status` = NEW.`release_status`,
                        `meta_language` = NEW.`meta_language`,
                        `itunes_previewable` = NEW.`itunes_previewable`,
                        `subtitle_language_id` = NEW.`subtitle_language_id`,
                        `compilation` = NEW.`compilation`,
                        `project_id` = NEW.`project_id`,
                        `p_line` = NEW.`p_line`
                        ;

                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
