<?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="context_command-table">
        <createTable tableName="context_command">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="context_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="type" type="VARCHAR(255)">
                <constraints nullable="true"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="feature_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseColumnNames="feature_id" baseTableName="context_command" constraintName="fk_context_command_feature"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="id" referencedTableName="feature" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS context_command_update_trigger ON context_command;
            CREATE TRIGGER context_command_update_trigger BEFORE UPDATE ON context_command FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS context_command_insert_trigger ON context_command;
            CREATE TRIGGER context_command_insert_trigger BEFORE INSERT ON context_command FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
        <sql>
            ALTER TABLE context_command ADD CONSTRAINT check_context_command_type CHECK (type IN ('MESSAGE', 'MEMBER'));
        </sql>
    </changeSet>
    <changeSet author="Sheldan" id="context_command_in_server-table">
        <createTable tableName="context_command_in_server">
            <column autoIncrement="true" name="command_in_server_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="context_command_in_server_pkey"/>
            </column>
            <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"/>
            <column name="context_command_id" type="BIGINT">
                <constraints nullable="true" />
            </column>
        </createTable>
        <createIndex indexName="idx_context_command_in_server" tableName="context_command_in_server">
            <column name="server_id"/>
            <column name="command_id"/>
        </createIndex>
        <addForeignKeyConstraint baseColumnNames="command_id" baseTableName="command_in_server" constraintName="fk_context_command_in_server_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_context_command_in_server_server"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id"
                                 referencedTableName="server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS context_command_in_server_update_trigger ON context_command_in_server;
            CREATE TRIGGER context_command_in_server_update_trigger BEFORE UPDATE ON context_command_in_server FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS context_command_in_server_insert_trigger ON context_command_in_server;
            CREATE TRIGGER context_command_in_server_insert_trigger BEFORE INSERT ON context_command_in_server FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
</databaseChangeLog>