<?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="102" author="jian">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            DROP TABLE IF EXISTS `ROLLBACK-vendor_restricted_features`;;
            
            CREATE TABLE `ROLLBACK-vendor_restricted_features`
            SELECT *
            FROM vendor_restricted_features
            GROUP BY vendor_id, feature_id
            HAVING COUNT(*) > 1;;

            DELETE vrf.*
            FROM vendor_restricted_features vrf 
            INNER JOIN `ROLLBACK-vendor_restricted_features` rvrf ON vrf.vendor_restricted_features_id = rvrf.vendor_restricted_features_id;;

            ALTER TABLE vendor_restricted_features
            DROP PRIMARY KEY, 
            ADD PRIMARY KEY (vendor_id, feature_id), 
            ADD UNIQUE INDEX unique_idx (vendor_restricted_features_id);;
        </sql>
        <rollback>
            ALTER TABLE vendor_restricted_features
            DROP PRIMARY KEY, 
            ADD PRIMARY KEY (vendor_restricted_features_id);;

            INSERT INTO vendor_restricted_features (vendor_id, feature_id)
            SELECT vendor_id, feature_id FROM `ROLLBACK-vendor_restricted_features`;;
            
            DROP TABLE IF EXISTS `ROLLBACK-vendor_restricted_features`;;
        </rollback>
    </changeSet>
</databaseChangeLog>
