<?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="mdrake">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        USE `direct_delivery`;;
        DROP TRIGGER IF EXISTS `after_update_dms_delivery_spec`;;

        CREATE DEFINER = 'theorchard_dd'@'%' TRIGGER after_update_dms_delivery_spec AFTER UPDATE ON dms_delivery_spec
        FOR EACH ROW
        BEGIN
        INSERT INTO `direct_delivery_log`.`dms_delivery_spec_log` SET 
          `log_timestamp` = now(),
          `log_action` = 'update',
          `dms_delivery_spec_id` = NEW.`dms_delivery_spec_id` ,
          `dms_master_master_id` = NEW.`dms_master_master_id`,
          `order_type` = NEW.`order_type`,
          `domain_name` = NEW.`domain_name`,
          `user_name` = NEW.`user_name`,
          `password` = NEW.`password`,
          `port` = NEW.`port`,
          `remote_initial_dir` = NEW.`remote_initial_dir`,
          `connection_type` = NEW.`connection_type` ,
          `num_per_batch` = NEW.`num_per_batch`,
          `batch_delivery` = NEW.`batch_delivery`,
          `batch_foldername` = NEW.`batch_foldername`,
          `manifest_filename` = NEW.`manifest_filename`,
          `delivery_complete_file` = NEW.`delivery_complete_file`,
          `confirm_manifest_delivery` = NEW.`confirm_manifest_delivery`,
          `manifest_format`  = NEW.`manifest_format`,
          `connection_limit`  = NEW.`connection_limit`,
          `authenticate_type`  = NEW.`authenticate_type`,
          `sftp_transfer_mode`  = NEW.`sftp_transfer_mode`,
          `ftp_pasv_mode` = NEW.`ftp_pasv_mode`,
          `encoding`  = NEW.`encoding`,
          `delivery` = NEW. `delivery`,
          `md5_fingerprint`  = NEW.`md5_fingerprint`,
          `known_host`  = NEW.`known_host`,
          `private_key_file` = NEW.`private_key_file`,
          `max_num_batches` = NEW.`max_num_batches`,
          `verify_delivery` = NEW.`verify_delivery`;
        END;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            USE `direct_delivery`;;
            DROP TRIGGER IF EXISTS `after_update_dms_delivery_spec`;;
            </sql>
        </rollback>
    </changeSet>    
    <changeSet id="2" author="mdrake">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        USE `direct_delivery`;;
        DROP TRIGGER IF EXISTS `after_insert_dms_delivery_spec`;;

        CREATE DEFINER = 'theorchard_dd'@'%' TRIGGER after_insert_dms_delivery_spec AFTER INSERT ON dms_delivery_spec
        FOR EACH ROW
        BEGIN
        INSERT INTO `direct_delivery_log`.`dms_delivery_spec_log` SET 
          `log_timestamp` = now(),
          `log_action` = 'insert',
          `dms_delivery_spec_id` = NEW.`dms_delivery_spec_id` ,
          `dms_master_master_id` = NEW.`dms_master_master_id`,
          `order_type` = NEW.`order_type`,
          `domain_name` = NEW.`domain_name`,
          `user_name` = NEW.`user_name`,
          `password` = NEW.`password`,
          `port` = NEW.`port`,
          `remote_initial_dir` = NEW.`remote_initial_dir`,
          `connection_type` = NEW.`connection_type` ,
          `num_per_batch` = NEW.`num_per_batch`,
          `batch_delivery` = NEW.`batch_delivery`,
          `batch_foldername` = NEW.`batch_foldername`,
          `manifest_filename` = NEW.`manifest_filename`,
          `delivery_complete_file` = NEW.`delivery_complete_file`,
          `confirm_manifest_delivery` = NEW.`confirm_manifest_delivery`,
          `manifest_format`  = NEW.`manifest_format`,
          `connection_limit`  = NEW.`connection_limit`,
          `authenticate_type`  = NEW.`authenticate_type`,
          `sftp_transfer_mode`  = NEW.`sftp_transfer_mode`,
          `ftp_pasv_mode` = NEW.`ftp_pasv_mode`,
          `encoding`  = NEW.`encoding`,
          `delivery` = NEW. `delivery`,
          `md5_fingerprint`  = NEW.`md5_fingerprint`,
          `known_host`  = NEW.`known_host`,
          `private_key_file`  = NEW.`private_key_file`,
          `max_num_batches`  = NEW.`max_num_batches`,
          `verify_delivery` = NEW.`verify_delivery`;
        END;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            USE `direct_delivery`;;
            DROP TRIGGER IF EXISTS `after_insert_dms_delivery_spec`;;
            </sql>
        </rollback>
    </changeSet>
     <changeSet id="3" author="mdrake">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        USE `direct_delivery`;;
        DROP TRIGGER IF EXISTS `after_delete_dms_delivery_spec`;;
        
        CREATE DEFINER = 'theorchard_dd'@'%' TRIGGER after_delete_dms_delivery_spec AFTER DELETE ON dms_delivery_spec
        FOR EACH ROW
        BEGIN
        INSERT INTO `direct_delivery_log`.`dms_delivery_spec_log` SET 
          `log_timestamp` = now(),
          `log_action` = 'delete',
          `dms_delivery_spec_id` = OLD.`dms_delivery_spec_id` ,
          `dms_master_master_id` = OLD.`dms_master_master_id`,
          `order_type` = OLD.`order_type`,
          `domain_name` = OLD.`domain_name`,
          `user_name` = OLD.`user_name`,
          `password` = OLD.`password`,
          `port` = OLD.`port`,
          `remote_initial_dir` = OLD.`remote_initial_dir`,
          `connection_type` = OLD.`connection_type` ,
          `num_per_batch` = OLD.`num_per_batch`,
          `batch_delivery` = OLD.`batch_delivery`,
          `batch_foldername` = OLD.`batch_foldername`,
          `manifest_filename` = OLD.`manifest_filename`,
          `delivery_complete_file` = OLD.`delivery_complete_file`,
          `confirm_manifest_delivery` = OLD.`confirm_manifest_delivery`,
          `manifest_format`  = OLD.`manifest_format`,
          `connection_limit`  = OLD.`connection_limit`,
          `authenticate_type`  = OLD.`authenticate_type`,
          `sftp_transfer_mode`  = OLD.`sftp_transfer_mode`,
          `ftp_pasv_mode` = OLD.`ftp_pasv_mode`,
          `encoding`  = OLD.`encoding`,
          `delivery` = OLD. `delivery`,
          `md5_fingerprint`  = OLD.`md5_fingerprint`,
          `known_host`  = OLD.`known_host`,
          `private_key_file`  = OLD.`private_key_file`,
          `max_num_batches`  = OLD.`max_num_batches`,
          `verify_delivery` = OLD.`verify_delivery`;
        END;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            USE `direct_delivery`;;
            DROP TRIGGER IF EXISTS `after_delete_dms_delivery_spec`;;
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>