<?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="101-2" author="jian">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            DROP PROCEDURE IF EXISTS sp_cloudsearch_upsert_subaccounts_by_vendor_id;;

            CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE sp_cloudsearch_upsert_subaccounts_by_vendor_id(IN vendorId INT)
            BEGIN
                DECLARE subaccountId INT DEFAULT 0;
                DECLARE done INT DEFAULT 0;
                DECLARE subaccountCursor CURSOR FOR
                SELECT s.subaccount_id 
                FROM art_relations.subaccount s
                WHERE s.vendor_id = vendorId;
                DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
                
                OPEN subaccountCursor;
                oj: LOOP
                    IF done THEN
                        LEAVE oj;
                    END IF;
                
                    FETCH subaccountCursor INTO subaccountId;
                    CALL cloudsearch.sp_upsert_subaccount(subaccountId);
                END LOOP oj;
                CLOSE subaccountCursor;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                DROP PROCEDURE IF EXISTS sp_cloudsearch_upsert_subaccounts_by_vendor_id;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
