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

            DROP TRIGGER IF EXISTS `before_update_track`;;

            CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER `before_update_track` BEFORE UPDATE ON `track`
                FOR EACH ROW BEGIN
                    IF(NEW.length_seconds != OLD.length_seconds
                        OR (ISNULL(NEW.length_seconds) AND NOT ISNULL(OLD.length_seconds))
                        OR (NOT ISNULL(NEW.length_seconds) AND ISNULL(OLD.length_seconds))
                        OR NEW.length_minute != OLD.length_minute
                        OR (ISNULL(NEW.length_minute) AND NOT ISNULL(OLD.length_minute))
                        OR (NOT ISNULL(NEW.length_minute) AND ISNULL(OLD.length_minute))
                    )
                THEN
                    CALL art_relations.sp_get_track_offer_type(NEW.length_minute, NEW.length_seconds,NEW.offer_type, @return_offer_type);
                    SET NEW.offer_type = @return_offer_type;
            END IF;
            END;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

            USE art_relations;;

            DROP TRIGGER IF EXISTS `before_update_track`;;

            CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER `before_update_track` BEFORE UPDATE ON `track`
                FOR EACH ROW BEGIN
                IF(NEW.length_minute != OLD.length_minute OR NEW.length_seconds != OLD.length_seconds)
					THEN
						CALL art_relations.sp_get_track_offer_type(NEW.length_minute, NEW.length_seconds,NEW.offer_type, @return_offer_type);
						SET NEW.offer_type = @return_offer_type;
					END IF;
                END;;
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
