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

            SET @resource_name = 'lncbulkupload';;
            SET @privilege_name = 'index';;
            SET @permission_desc = 'Access the LNC Bulk Upload tool';;

            -- Create the resource
            INSERT INTO `vendor_resource` (`resource`) VALUES (@resource_name);;
            SET @resource_id = LAST_INSERT_ID();;

            -- Create the permissions
            INSERT INTO vendor_permissions (id, resource_id, privilege_id) VALUES (NULL, @resource_id, NULL);;
            SET @default_perm = LAST_INSERT_ID();;

            -- Assign the permission to all vendor roles
            INSERT INTO vendor_role_permissions(role_id, permission_id, allow)
            VALUES (1, @default_perm, 'Y');;
            INSERT INTO vendor_role_permissions(role_id, permission_id, allow)
            VALUES (2, @default_perm, 'N');;
            INSERT INTO vendor_role_permissions(role_id, permission_id, allow)
            VALUES (3, @default_perm, 'N');;
            INSERT INTO vendor_role_permissions(role_id, permission_id, allow)
            VALUES (4, @default_perm, 'Y');;
            INSERT INTO vendor_role_permissions(role_id, permission_id, allow)
            VALUES (5, @default_perm, 'N');;

        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">

                USE art_relations;;

                SET @resource_name = 'lncbulkupload';;

                delete from vendor_role_permissions where permission_id in
                (
                    select id from vendor_permissions where resource_id in
                    (
                        select id from vendor_resource where resource = @resource_name
                    )
                );;
                delete from vendor_permissions where resource_id = (select id from vendor_resource where resource = @resource_name);;
                delete from vendor_resource where resource = @resource_name;;

            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
