<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
                   xmlns:pro="http://www.liquibase.org/xml/ns/pro"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext dbchangelog.xsd
    http://www.liquibase.org/xml/ns/pro dbchangelog.xsd" >
    <changeSet author="Sheldan" id="warning-table">
        <createTable tableName="warning">
            <column name="id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="false"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="decay_date" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="decayed" type="BOOLEAN">
                <constraints nullable="false"/>
            </column>
            <column name="reason" type="VARCHAR(2048)">
                <constraints nullable="false"/>
            </column>
            <column name="warn_date" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="false"/>
            </column>
            <column name="warned_user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="warning_user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <createIndex indexName="idx_warning_user" tableName="warning">
            <column name="warned_user_in_server_id"/>
        </createIndex>
        <addPrimaryKey columnNames="server_id, id" tableName="warning" constraintName="pk_warning" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="warned_user_in_server_id" baseTableName="warning" constraintName="fk_warning_warned_user" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="warning_user_in_server_id" baseTableName="warning" constraintName="fk_warning_warning_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="warning" constraintName="fk_warning_server_id" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS warning_update_trigger ON warning;
            CREATE TRIGGER warning_update_trigger BEFORE UPDATE ON warning FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS warning_insert_trigger ON warning;
            CREATE TRIGGER warning_insert_trigger BEFORE INSERT ON warning FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>

</databaseChangeLog>