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

            DROP TRIGGER IF EXISTS `after_update_product_physical`;;

            CREATE DEFINER=`theorchard`@`10.10.%` TRIGGER `after_update_product_physical` AFTER UPDATE ON `product_physical` 
            FOR EACH ROW BEGIN
                INSERT INTO art_relations_log.product_physical_log SET
                    log_timestamp    = NOW(),
                    log_action       = 'update',
                    id               = NEW.id,
                    release_id       = NEW.release_id,
                    packaging_id     = NEW.packaging_id,
                    exclusive_for    = NEW.exclusive_for,
                    initial_stock    = NEW.initial_stock,
                    box_lot          = NEW.box_lot,
                    pricing          = NEW.pricing,
                    end_date         = NEW.end_date,
                    embargo_date     = NEW.embargo_date,
                    discount         = NEW.discount,
                    individual       = NEW.individual,
                    explicit         = NEW.explicit,
                    exportable       = NEW.exportable,
                    units_per_set    = NEW.units_per_set,
                    wholesale_price  = NEW.wholesale_price,
                    pline            = NEW.pline,
                    meeting_notes    = NEW.meeting_notes,
                    hmv_notes        = NEW.hmv_notes;
                CALL art_relations.sp_changed_product_physical_wholesale_price(NEW.release_id, OLD.wholesale_price, NEW.wholesale_price);
                IF (OLD.wholesale_price <> NEW.wholesale_price) THEN
                    CALL art_relations.sp_insert_product_physical_reports_change_history(
                    NEW.release_id, 'wholesale_price');
                END IF;
            END;;
        ]]>

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

                DROP TRIGGER IF EXISTS `after_update_product_physical`;;

                CREATE DEFINER=`theorchard`@`10.10.%` TRIGGER `after_update_product_physical` AFTER UPDATE ON `product_physical` 
                FOR EACH ROW BEGIN
                    INSERT INTO art_relations_log.product_physical_log SET
                        log_timestamp    = NOW(),
                        log_action       = 'update',
                        id               = NEW.id,
                        release_id       = NEW.release_id,
                        packaging_id     = NEW.packaging_id,
                        exclusive_for    = NEW.exclusive_for,
                        initial_stock    = NEW.initial_stock,
                        box_lot          = NEW.box_lot,
                        pricing          = NEW.pricing,
                        end_date         = NEW.end_date,
                        embargo_date     = NEW.embargo_date,
                        discount         = NEW.discount,
                        individual       = NEW.individual,
                        explicit         = NEW.explicit,
                        exportable       = NEW.exportable,
                        units_per_set    = NEW.units_per_set,
                        wholesale_price  = NEW.wholesale_price,
                        pline            = NEW.pline,
                        meeting_notes    = NEW.meeting_notes,
                        hmv_notes        = NEW.hmv_notes;
                    CALL art_relations.sp_changed_product_physical_wholesale_price(NEW.release_id, OLD.wholesale_price, NEW.wholesale_price);
                END;;
            ]]>

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