<?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="mdadhania">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            DROP TABLE IF EXISTS `ROLLBACK_MOV-1654_vendor_restricted_features`;;

            CREATE TABLE `ROLLBACK_MOV-1654_vendor_restricted_features`
                SELECT vrf.* 
                FROM vendor_restricted_features AS vrf 
                INNER JOIN vendor AS v ON v.vendor_id = vrf.vendor_id
                WHERE v.assigned_to IN (919,1064,1068,1084);;

            DELETE vrf.* 
                FROM vendor_restricted_features AS vrf 
                INNER JOIN vendor AS v ON v.vendor_id = vrf.vendor_id
                WHERE vrf.feature_id IN (3,6,9,11) AND v.assigned_to IN (919,1064,1068,1084);;

            INSERT IGNORE INTO vendor_restricted_features (vendor_id, feature_id)
                SELECT v.vendor_id, f.feature_id
                FROM vendor AS v, features AS f
                WHERE v.assigned_to IN (919,1064,1068,1084) 
                    AND f.feature_id IN (1,2,4,5,7,10,12);;

        </sql>
        <rollback>
            DELETE vrf.* 
                FROM vendor_restricted_features AS vrf 
                INNER JOIN vendor AS v ON v.vendor_id = vrf.vendor_id
                WHERE v.assigned_to IN (919,1064,1068,1084);;
            
            INSERT INTO vendor_restricted_features SELECT * FROM `ROLLBACK_MOV-1654_vendor_restricted_features`;;

            DROP TABLE IF EXISTS `ROLLBACK_MOV-1654_vendor_restricted_features`;;
        </rollback>
    </changeSet>
</databaseChangeLog>
