<?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="suggestion_vote-table">
        <createTable tableName="suggestion_vote">
            <column name="voter_user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="suggestion_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="decision" type="VARCHAR(255)">
                <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="voter_user_in_server_id, suggestion_id, server_id" tableName="suggestion_vote" constraintName="pk_suggestion_vote" validate="false"/>
        <addForeignKeyConstraint baseColumnNames="suggestion_id, server_id" baseTableName="suggestion_vote" constraintName="fk_suggestion_vote_suggestion"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="id, server_id" referencedTableName="suggestion" validate="false"/>
        <addForeignKeyConstraint baseColumnNames="voter_user_in_server_id" baseTableName="suggestion_vote" constraintName="fk_suggestion_vote_voter"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="false"/>
        <sql>
            DROP TRIGGER IF EXISTS suggestion_vote_update_trigger ON suggestion_vote;
            CREATE TRIGGER suggestion_vote_update_trigger BEFORE UPDATE ON suggestion_vote FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS suggestion_vote_insert_trigger ON suggestion_vote;
            CREATE TRIGGER suggestion_vote_insert_trigger BEFORE INSERT ON suggestion_vote FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
        <sql>
            ALTER TABLE suggestion_vote ADD CONSTRAINT check_suggestion_vote_state CHECK (decision IN ('AGREE','DISAGREE'));
        </sql>
    </changeSet>
</databaseChangeLog>