<?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="n-kumar">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <!-- customer_master_master_id 376 is Amazon Instant Video
                 customer_master_master_id 634 is Amazon Instant Video(UK)
                 distribution_type_id 3 is Video Master
                 territory 3 is United Kingdom(UK)
             -->
            DROP TABLE IF EXISTS `ROLLBACK_mov1308_vendor_dms_master_restriction_03`;;

            CREATE TABLE `ROLLBACK_mov1308_vendor_dms_master_restriction_03`
             SELECT vavc.`vendor_contract_id` 
              FROM vw_active_vendor_contract vavc 
               INNER JOIN vendor_contract_distribution_type vcdt ON vcdt.`vendor_contract_id` = vavc.vendor_contract_id 
               INNER JOIN `vendor_contract` vc ON vc.id = vavc.vendor_contract_id 
               LEFT JOIN
                 (SELECT vdmr.`vendor_contract_id`, (vdmr.`restriction_id`) , (vdmr.`customer_master_master_id`), (vdmr.`distribution_type_id`)
                      FROM `vendor_dms_master_restriction` vdmr
                      WHERE  vdmr.`customer_master_master_id` IN (376)
                      AND vdmr.`distribution_type_id` = 3
                  ) AS amazonvod ON amazonvod.`vendor_contract_id` = vavc.`vendor_contract_id`
               LEFT JOIN
                 (SELECT vdmr.`vendor_contract_id`, (vdmr.`restriction_id`) , (vdmr.`customer_master_master_id`), (vdmr.`distribution_type_id`)
                      FROM `vendor_dms_master_restriction` vdmr
                      WHERE  vdmr.`customer_master_master_id` IN (634)
                      AND vdmr.`distribution_type_id` = 3
                  ) AS amazonuk ON amazonuk.`vendor_contract_id` = vavc.`vendor_contract_id`
                  WHERE FIND_IN_SET(3, vc.`territory_carve_out`) = 0
                  AND  vcdt.`distribution_type_id` = 3 AND amazonvod.restriction_id IS NULL AND amazonuk.restriction_id IS NOT NULL
                  ;; 

            DELETE FROM `vendor_dms_master_restriction` where vendor_contract_id IN(SELECT vendor_contract_id FROM 
            `ROLLBACK_mov1308_vendor_dms_master_restriction_03`) AND distribution_type_id = 3 AND customer_master_master_id= 634;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
                INSERT into `vendor_dms_master_restriction` (customer_master_master_id, distribution_type_id, vendor_contract_id) 
                (SELECT 634, 3, vendor_contract_id FROM `ROLLBACK_mov1308_vendor_dms_master_restriction_03`);;
            </sql>
        </rollback>
    </changeSet>
    <changeSet id="2" author="n-kumar">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <!-- customer_master_master_id 376 is Amazon Instant Video
                 customer_master_master_id 645 is Amazon Instant Video (Germany)
                 distribution_type_id 3 is Video Master
                 territory 4 is Germany
             -->
            DROP TABLE IF EXISTS `ROLLBACK_mov1308_vendor_dms_master_restriction_04`;;

            CREATE TABLE `ROLLBACK_mov1308_vendor_dms_master_restriction_04`
              SELECT vavc.`vendor_contract_id` 
              FROM vw_active_vendor_contract vavc 
               INNER JOIN vendor_contract_distribution_type vcdt ON vcdt.`vendor_contract_id` = vavc.vendor_contract_id 
               INNER JOIN `vendor_contract` vc ON vc.id = vavc.vendor_contract_id 
               LEFT JOIN
                 (SELECT vdmr.`vendor_contract_id`, (vdmr.`restriction_id`) , (vdmr.`customer_master_master_id`), (vdmr.`distribution_type_id`)
                      FROM `vendor_dms_master_restriction` vdmr
                      WHERE  vdmr.`customer_master_master_id` IN (376)
                      AND vdmr.`distribution_type_id` = 3
                  ) AS amazonvod ON amazonvod.`vendor_contract_id` = vavc.`vendor_contract_id`
               LEFT JOIN
                 (SELECT vdmr.`vendor_contract_id`, (vdmr.`restriction_id`) , (vdmr.`customer_master_master_id`), (vdmr.`distribution_type_id`)
                      FROM `vendor_dms_master_restriction` vdmr
                      WHERE  vdmr.`customer_master_master_id` IN (645)
                      AND vdmr.`distribution_type_id` = 3
                  ) AS amazonger ON amazonger.`vendor_contract_id` = vavc.`vendor_contract_id`
                  WHERE FIND_IN_SET(4, vc.`territory_carve_out`) = 0
                  AND  vcdt.`distribution_type_id` = 3 AND amazonvod.restriction_id IS NULL AND amazonger.restriction_id IS NOT NULL
                  ;; 

            DELETE FROM `vendor_dms_master_restriction` where vendor_contract_id IN(SELECT vendor_contract_id FROM
            `ROLLBACK_mov1308_vendor_dms_master_restriction_04`) AND distribution_type_id = 3 AND customer_master_master_id= 645;;
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
                INSERT into `vendor_dms_master_restriction` (customer_master_master_id, distribution_type_id, vendor_contract_id) 
                (SELECT 645, 3, vendor_contract_id FROM `ROLLBACK_mov1308_vendor_dms_master_restriction_04`);;
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
