<?xml version="1.0" encoding="UTF-8"?>
<!--
    The changesets in this file are meant to facilitate automatic filling in
    of releases.vendor_release_identifier and track.vendor_track_identifier
    whenever release_grid or master_grid tables are written to.  Stored procedures
    are used by triggers on these tables in order to avoid slave DB improperly reading
    transaction log from master.

    This facility will be in effect until the time, when outgoing data feeds relying
    on correct product numbers will be switched to use dim_release_grid and
    dim_master_grid dimension tables in Redshift.  At that time, these stored procedures
    and triggers will be replaced with triggers acting in the opposite direction, taking care
    to populate grid tables from releases and track whenever appropriate.
-->
<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="azinger">
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
                
                USE art_relations;;

                DROP TRIGGER IF EXISTS after_insert_release_grid;;
                DROP TRIGGER IF EXISTS after_update_release_grid;;
                DROP PROCEDURE IF EXISTS sp_write_vendor_release_identifier_from_grid;;

                CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE sp_write_vendor_release_identifier_from_grid (
                    IN in_release_id INT UNSIGNED,
                    IN in_upc BIGINT UNSIGNED,
                    IN in_product_no CHAR(14)
                )
                BEGIN
                    UPDATE releases
                    SET vendor_release_identifier = in_product_no
                    WHERE release_id = in_release_id
                    AND upc = in_upc
                    AND (
                        vendor_release_identifier IS NULL OR
                        vendor_release_identifier = ''
                    );
                END;;

                CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_insert_release_grid AFTER INSERT ON release_grid FOR EACH ROW
                BEGIN
                    CALL art_relations.sp_write_vendor_release_identifier_from_grid(
                        NEW.release_id,
                        NEW.upc,
                        NEW.product_no
                    );
                END;;

                CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_update_release_grid AFTER UPDATE ON release_grid FOR EACH ROW
                BEGIN
                    CALL art_relations.sp_write_vendor_release_identifier_from_grid(
                        NEW.release_id,
                        NEW.upc,
                        NEW.product_no
                    );
                END;;

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

                    USE art_relations;;

                    DROP TRIGGER IF EXISTS after_insert_release_grid;;
                    DROP TRIGGER IF EXISTS after_update_release_grid;;
                    DROP PROCEDURE IF EXISTS sp_write_vendor_release_identifier_from_grid;;

                </sql>
            </rollback>
        </changeSet>
        <changeSet id="2" author="azinger">
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

                USE art_relations;;

                DROP TRIGGER IF EXISTS after_insert_master_grid;;
                DROP TRIGGER IF EXISTS after_update_master_grid;;
                DROP PROCEDURE IF EXISTS sp_write_vendor_track_identifier_from_grid;;

                CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE sp_write_vendor_track_identifier_from_grid (
                    IN in_id INT UNSIGNED,
                    IN in_isrc VARCHAR(16),
                    IN in_product_no CHAR(14))
                BEGIN
                    UPDATE track
                    SET vendor_track_identifier = in_product_no
                    WHERE id = in_id
                    AND isrc = in_isrc
                    AND (
                        vendor_track_identifier IS NULL OR
                        vendor_track_identifier = ''
                    );
                END;;

                CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_insert_master_grid AFTER INSERT ON master_grid FOR EACH ROW
                BEGIN
                    CALL art_relations.sp_write_vendor_track_identifier_from_grid (
                        NEW.id,
                        NEW.isrc,
                        NEW.product_no
                    );
                END;;

                CREATE DEFINER='theorchard'@'10.10.%' TRIGGER after_update_master_grid AFTER UPDATE ON master_grid FOR EACH ROW
                BEGIN
                    CALL art_relations.sp_write_vendor_track_identifier_from_grid (
                        NEW.id,
                        NEW.isrc,
                        NEW.product_no
                    );
                END;;

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

                    USE art_relations;;

                    DROP TRIGGER IF EXISTS after_insert_master_grid;;
                    DROP TRIGGER IF EXISTS after_update_master_grid;;
                    DROP PROCEDURE IF EXISTS sp_write_vendor_track_identifier_from_grid;;

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