<?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="repost-table">
        <createTable tableName="repost">
            <column name="message_id" type="BIGINT" >
                <constraints nullable="false"/>
            </column>
            <column name="position" type="INTEGER" >
                <constraints nullable="false"/>
            </column>
            <column name="user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="count" type="INTEGER">
                <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="message_id, position, user_in_server_id" tableName="repost" constraintName="pk_repost" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="user_in_server_id" baseTableName="repost" constraintName="fk_repost_server"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="message_id, position" baseTableName="repost" constraintName="fk_repost_posted_image"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="message_id, position"
                                 referencedTableName="posted_image" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="user_in_server_id" baseTableName="repost" constraintName="fk_repost_user_in_server"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="user_in_server_id"
                                 referencedTableName="user_in_server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS repost_update_trigger ON repost;
            CREATE TRIGGER repost_update_trigger BEFORE UPDATE ON repost FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS repost_insert_trigger ON repost;
            CREATE TRIGGER repost_insert_trigger BEFORE INSERT ON repost FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>

</databaseChangeLog>