<?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="command-table">
        <createTable tableName="command">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="command_pkey"/>
            </column>
            <column name="name" type="VARCHAR(255)">
                <constraints nullable="true"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="true"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="feature_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="module_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseColumnNames="feature_id" baseTableName="command" constraintName="fk_command_feature" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="feature" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="module_id" baseTableName="command" constraintName="fk_command_module" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="module" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS command_update_trigger ON command;
            CREATE TRIGGER command_update_trigger BEFORE UPDATE ON command FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS command_insert_trigger ON command;
            CREATE TRIGGER command_insert_trigger BEFORE INSERT ON command FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
    <changeSet author="Sheldan" id="command_in_server-table">
        <createTable tableName="command_in_server">
            <column autoIncrement="true" name="command_in_server_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="command_in_server_pkey"/>
            </column>
            <column name="restricted" type="BOOLEAN"/>
            <column name="command_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
        </createTable>
        <createIndex indexName="idx_command_in_server" tableName="command_in_server">
            <column name="server_id"/>
            <column name="command_id"/>
        </createIndex>
        <addForeignKeyConstraint baseColumnNames="command_id" baseTableName="command_in_server" constraintName="fk_command_command" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="command" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="server_id" baseTableName="command_in_server" constraintName="fk_command_server" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS command_in_server_update_trigger ON command_in_server;
            CREATE TRIGGER command_in_server_update_trigger BEFORE UPDATE ON command_in_server FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS command_in_server_insert_trigger ON command_in_server;
            CREATE TRIGGER command_in_server_insert_trigger BEFORE INSERT ON command_in_server FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
    <changeSet author="Sheldan" id="command_in_server_immune_role-table">
        <createTable tableName="command_in_server_immune_role">
            <column name="command_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="role_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseColumnNames="command_in_server_id" baseTableName="command_in_server_immune_role" constraintName="fk_command_immune_role" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="command_in_server_id" referencedTableName="command_in_server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="role_id" baseTableName="command_in_server_immune_role" constraintName="fk_immune_role_role" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="role" validate="true"/>

    </changeSet>
    <changeSet author="Sheldan" id="command_in_server_allowed_role-table">
        <createTable tableName="command_in_server_allowed_role">
            <column name="command_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="role_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseColumnNames="command_in_server_id" baseTableName="command_in_server_allowed_role" constraintName="fk_command_allowed_role" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="command_in_server_id" referencedTableName="command_in_server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="role_id" baseTableName="command_in_server_allowed_role" constraintName="fk_role_allowed_role" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="role" validate="true"/>

    </changeSet>
</databaseChangeLog>