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

            DROP TRIGGER IF EXISTS after_update_release;;

            CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_update_release AFTER UPDATE 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 = 'update',
                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
                ;
                
                CALL art_relations.sp_changed_releases_release_date_deletion_status(NEW.release_id, OLD.release_date, NEW.release_date, OLD.deletions, NEW.deletions);
            END;;
        ]]>
        </sql>
        <rollback>
          <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
          <![CDATA[

            DROP TRIGGER IF EXISTS after_update_release;;
            
            CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_update_release AFTER UPDATE 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 = 'update',
                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
                ;
            END;;
          ]]>
          </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
