<?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="mod_mail_thread-table">
        <createTable tableName="mod_mail_thread">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="mod_mail_thread_pkey"/>
            </column>
            <column name="closed" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="false"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="state" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="channel_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <createIndex indexName="idx_mod_mail_thread_user" tableName="mod_mail_thread">
            <column name="user_in_server_id"/>
        </createIndex>
        <createIndex indexName="idx_mod_mail_thread_channel" tableName="mod_mail_thread">
            <column name="channel_id"/>
        </createIndex>
        <createIndex indexName="idx_mod_mail_thread_user_state" tableName="mod_mail_thread">
            <column name="user_in_server_id"/>
            <column name="state"/>
        </createIndex>
        <addForeignKeyConstraint baseColumnNames="channel_id" baseTableName="mod_mail_thread" constraintName="fk_mod_mail_thread_channel" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="channel" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="user_in_server_id" baseTableName="mod_mail_thread" constraintName="fk_mod_mail_thread_user" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="server_id" baseTableName="mod_mail_thread" constraintName="fk_mod_mail_thread_server" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS mod_mail_thread_update_trigger ON mod_mail_thread;
            CREATE TRIGGER mod_mail_thread_update_trigger BEFORE UPDATE ON mod_mail_thread FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS mod_mail_thread_insert_trigger ON mod_mail_thread;
            CREATE TRIGGER mod_mail_thread_insert_trigger BEFORE INSERT ON mod_mail_thread FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
</databaseChangeLog>