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

            CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER after_update_subaccount AFTER UPDATE ON subaccount FOR EACH ROW 
            BEGIN
                IF (
                    NEW.subaccount_name != OLD.subaccount_name 
                    OR (ISNULL(NEW.subaccount_name) AND NOT ISNULL(OLD.subaccount_name))
                    OR (NOT ISNULL(NEW.subaccount_name) AND ISNULL(OLD.subaccount_name))
                )
                OR (
                    NEW.vendor_id != OLD.vendor_id
                    OR (ISNULL(NEW.vendor_id) AND NOT ISNULL(OLD.vendor_id))
                    OR (NOT ISNULL(NEW.vendor_id) AND ISNULL(OLD.vendor_id))
                )
                OR (
                    NEW.country_id != OLD.country_id
                    OR (ISNULL(NEW.country_id) AND NOT ISNULL(OLD.country_id))
                    OR (NOT ISNULL(NEW.country_id) AND ISNULL(OLD.country_id))
                )
                THEN
                    CALL cloudsearch.sp_upsert_subaccount(NEW.subaccount_id);
                END IF;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP TRIGGER IF EXISTS after_update_subaccount;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
