<?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="myron">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE art_relations;;
            DROP PROCEDURE IF EXISTS sp_claim_isrcs_for_use;;
            CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE `sp_claim_isrcs_for_use`(IN isrc_count INT)
            BEGIN
                DECLARE done INT DEFAULT 0;
                DECLARE counter INT DEFAULT 0;
                DECLARE last_total INT DEFAULT 0;
                DECLARE isrcs TEXT DEFAULT '';
                DECLARE the_isrc VARCHAR(14) DEFAULT '';    
                DECLARE get_isrcs CURSOR FOR  
            SELECT isrc
            FROM isrcs
            WHERE `status` = "unused" AND MID(isrc, 6, 2) = RIGHT(YEAR(CURDATE()), 2)
            LIMIT 10 FOR UPDATE;
                DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; 
            START TRANSACTION;
                    SELECT GET_LOCK('generate_isrcs',300) INTO @lock_result;
                    SET isrcs = "''";
                    OPEN get_isrcs;
            ged: LOOP
             SET done = 0;
                        FETCH get_isrcs INTO the_isrc;
            IF done THEN
            IF last_total = counter THEN
            LEAVE ged;
            END IF;
            SET last_total = counter;
            CLOSE get_isrcs;
             OPEN get_isrcs;
            ITERATE ged;
            END IF; 
            UPDATE isrcs SET `status` = "used" WHERE isrc = the_isrc AND `status` = "unused";
            SET isrcs = CONCAT(isrcs, ",'",the_isrc,"'");
            SET counter = counter+1;
            IF(counter>=isrc_count) THEN
            LEAVE ged;
            END IF;
            END LOOP ged;
            CLOSE get_isrcs;
            SET @stmtStr := '';
            SELECT CONCAT('SELECT isrc
            FROM isrcs
            WHERE isrc IN (',isrcs,')') INTO @stmtStr;
            PREPARE stmt FROM @stmtStr;
            EXECUTE stmt;
            COMMIT;
            DO RELEASE_LOCK('generate_isrcs');
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE art_relations;;
                DROP PROCEDURE IF EXISTS sp_claim_isrcs_for_use;;
                CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE `sp_claim_isrcs_for_use`(IN isrc_count INT)
                BEGIN
                    DECLARE done INT DEFAULT 0;
                    DECLARE counter INT DEFAULT 0;
                    DECLARE last_total INT DEFAULT 0;
                    DECLARE isrcs TEXT DEFAULT '';
                    DECLARE the_isrc VARCHAR(14) DEFAULT '';    
                    DECLARE get_isrcs CURSOR FOR  
                SELECT isrc
                FROM isrcs
                WHERE `status` = "unused" LIMIT 10 FOR UPDATE;
                    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1; 
                START TRANSACTION;
                        SELECT GET_LOCK('generate_isrcs',300) INTO @lock_result;
                        SET isrcs = "''";
                        OPEN get_isrcs;
                ged: LOOP
                 SET done = 0;
                            FETCH get_isrcs INTO the_isrc;
                IF done THEN
                IF last_total = counter THEN
                LEAVE ged;
                END IF;
                SET last_total = counter;
                CLOSE get_isrcs;
                 OPEN get_isrcs;
                ITERATE ged;
                END IF; 
                UPDATE isrcs SET `status` = "used" WHERE isrc = the_isrc AND `status` = "unused";
                SET isrcs = CONCAT(isrcs, ",'",the_isrc,"'");
                SET counter = counter+1;
                IF(counter>=isrc_count) THEN
                LEAVE ged;
                END IF;
                END LOOP ged;
                CLOSE get_isrcs;
                SET @stmtStr := '';
                SELECT CONCAT('SELECT isrc
                FROM isrcs
                WHERE isrc IN (',isrcs,')') INTO @stmtStr;
                PREPARE stmt FROM @stmtStr;
                EXECUTE stmt;
                COMMIT;
                DO RELEASE_LOCK('generate_isrcs');
                END;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
