<?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="ssavva">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE art_relations;;

            CREATE DEFINER='theorchard'@'10.10.%' PROCEDURE sp_release_artist_to_label_participant(
                IN new_artist_name VARCHAR(255),
                IN current_release_id INT,
                OUT participant_id INT)
            BEGIN

                DECLARE release_vendor_id INT DEFAULT NULL;
                DECLARE release_subaccount_id INT DEFAULT NULL;
                DECLARE found_participant_id INT DEFAULT NULL;

                # Get vendor and sub-account for the given release
                SELECT p.vendor_id, r.subaccount_id
                INTO release_vendor_id, release_subaccount_id
                FROM releases r
                INNER JOIN project p ON r.project_id = p.project_id
                WHERE r.release_id = current_release_id;

                # Find corresponding label participant by name if exists
                SELECT id
                INTO found_participant_id
                FROM label_participant
                WHERE vendor_id = release_vendor_id
                  AND subaccount_id <=> release_subaccount_id
                  AND TRIM(name) = TRIM(new_artist_name);

                # If participant doesn't exist then create a new one
                IF found_participant_id IS NULL THEN
                    INSERT INTO label_participant (name, vendor_id, subaccount_id)
                    VALUES (TRIM(new_artist_name), release_vendor_id, release_subaccount_id);

                    SET found_participant_id = LAST_INSERT_ID();
                END IF;

                # Return participant id
                SET participant_id = found_participant_id;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE art_relations;;

                DROP PROCEDURE IF EXISTS sp_release_artist_to_label_participant;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
    <changeSet id="2" author="ssavva">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
        <![CDATA[
            USE art_relations;;

            CREATE DEFINER='theorchard'@'10.10.%' TRIGGER before_insert_release_artist BEFORE INSERT ON release_artist
            FOR EACH ROW
            BEGIN
                DECLARE participant_id INT;
                CALL sp_release_artist_to_label_participant(NEW.artist_name, NEW.release_id, participant_id);
                SET NEW.label_participant_id = participant_id;
            END;;
        ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
                USE art_relations;;

                DROP TRIGGER IF EXISTS before_insert_release_artist;;
            ]]>
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
