<?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="101-3" author="jian">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            DROP TRIGGER IF EXISTS after_update_vendor;;

            CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER after_update_vendor AFTER UPDATE ON vendor FOR EACH ROW 
            BEGIN
                IF (
                    NEW.company != OLD.company 
                    OR (ISNULL(NEW.company) AND NOT ISNULL(OLD.company))
                    OR (NOT ISNULL(NEW.company) AND ISNULL(OLD.company))
                )
                OR (
                    NEW.owner != OLD.owner
                    OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                    OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                )
                OR (
                    NEW.assigned_to != OLD.assigned_to
                    OR (ISNULL(NEW.assigned_to) AND NOT ISNULL(OLD.assigned_to))
                    OR (NOT ISNULL(NEW.assigned_to) AND ISNULL(OLD.assigned_to))
                )
                OR (
                    (
                        NEW.status != OLD.status
                        OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                        OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                    )
                    AND NEW.status IN ('signed', 'deletion')
                )
                OR (
                    NEW.label_identifier != OLD.label_identifier
                    OR (ISNULL(NEW.label_identifier) AND NOT ISNULL(OLD.label_identifier))
                    OR (NOT ISNULL(NEW.label_identifier) AND ISNULL(OLD.label_identifier))
                )
                THEN
                    CALL art_relations.sp_cloudsearch_upsert_subaccounts_by_vendor_id(NEW.vendor_id);
                    CALL art_relations.sp_cloudsearch_upsert_releases_by_vendor_id(NEW.vendor_id);
                    CALL art_relations.sp_cloudsearch_upsert_artists_by_vendor_id(NEW.vendor_id);
                    CALL cloudsearch.sp_upsert_vendor(NEW.vendor_id);
                END IF;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP TRIGGER IF EXISTS after_update_vendor;;

                CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER after_update_vendor AFTER UPDATE ON vendor FOR EACH ROW 
                BEGIN
                    IF (
                        NEW.company != OLD.company 
                        OR (ISNULL(NEW.company) AND NOT ISNULL(OLD.company))
                        OR (NOT ISNULL(NEW.company) AND ISNULL(OLD.company))
                    )
                    OR (
                        NEW.owner != OLD.owner
                        OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                        OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                    )
                    OR (
                        (
                            NEW.status != OLD.status
                            OR (ISNULL(NEW.owner) AND NOT ISNULL(OLD.owner))
                            OR (NOT ISNULL(NEW.owner) AND ISNULL(OLD.owner))
                        )
                        AND NEW.status IN ('signed', 'deletion')
                    )
                    OR (
                        NEW.label_identifier != OLD.label_identifier
                        OR (ISNULL(NEW.label_identifier) AND NOT ISNULL(OLD.label_identifier))
                        OR (NOT ISNULL(NEW.label_identifier) AND ISNULL(OLD.label_identifier))
                    )
                    THEN
                        CALL art_relations.sp_cloudsearch_upsert_subaccounts_by_vendor_id(NEW.vendor_id);
                        CALL art_relations.sp_cloudsearch_upsert_releases_by_vendor_id(NEW.vendor_id);
                        CALL art_relations.sp_cloudsearch_upsert_artists_by_vendor_id(NEW.vendor_id);
                        CALL cloudsearch.sp_upsert_vendor(NEW.vendor_id);
                    END IF;
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
