<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.26.xsd" >
    <changeSet author="Sheldan" id="giveaway_participant-table">
        <createTable tableName="giveaway_participant">
            <column name="participant_user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="giveaway_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="won" type="BOOLEAN">
                <constraints nullable="false"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="false"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
        </createTable>
        <addPrimaryKey columnNames="participant_user_in_server_id, giveaway_id, server_id" tableName="giveaway_participant" constraintName="pk_giveaway_participant" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="participant_user_in_server_id" baseTableName="giveaway_participant" constraintName="fk_giveaway_participant_participant"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="user_in_server_id" 
                                 referencedTableName="user_in_server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="giveaway_id, server_id" baseTableName="giveaway_participant" constraintName="fk_giveaway_participant_giveaway"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id, server_id"
                                 referencedTableName="giveaway" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS giveaway_participant_update_trigger ON giveaway_participant;
            CREATE TRIGGER giveaway_participant_update_trigger BEFORE UPDATE ON giveaway_participant FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS giveaway_participant_insert_trigger ON giveaway_participant;
            CREATE TRIGGER giveaway_participant_insert_trigger BEFORE INSERT ON giveaway_participant FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>

</databaseChangeLog>