<?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="channel_group-table">
        <createTable tableName="channel_group">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="channel_group_pkey"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="true"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="group_name" type="VARCHAR(255)">
                <constraints nullable="true"/>
            </column>
            <column name="group_type_id" type="BIGINT">
                <constraints nullable="true"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <createIndex indexName="idx_channel_group_name_server" tableName="channel_group">
            <column name="group_name"/>
            <column name="server_id"/>
        </createIndex>
        <createIndex indexName="idx_channel_group_server" tableName="channel_group">
            <column name="server_id"/>
        </createIndex>
        <addForeignKeyConstraint baseColumnNames="group_type_id" baseTableName="channel_group" constraintName="fk_channel_group_channel_group_type" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="channel_group_type" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="server_id" baseTableName="channel_group" constraintName="fk_channel_group_server" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS channel_group_update_trigger ON channel_group;
            CREATE TRIGGER channel_group_update_trigger BEFORE UPDATE ON channel_group FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS channel_group_insert_trigger ON channel_group;
            CREATE TRIGGER channel_group_insert_trigger BEFORE INSERT ON channel_group FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
    <changeSet author="Sheldan" id="channel_in_group-table">
        <createTable tableName="channel_in_group">
            <column name="group_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="channel_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseColumnNames="channel_id" baseTableName="channel_in_group" constraintName="fk_channel_in_group_channel" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="channel" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="group_id" baseTableName="channel_in_group" constraintName="fk_channel_in_group_group" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="channel_group" validate="true"/>
    </changeSet>
    <changeSet author="Sheldan" id="channel_group_command-table">
        <createTable tableName="channel_group_command">
            <column autoIncrement="true" name="command_in_group_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="channel_group_command_pkey"/>
            </column>
            <column name="enabled" type="BOOLEAN"/>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="command_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="group_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addForeignKeyConstraint baseColumnNames="group_id" baseTableName="channel_group_command" constraintName="fk_channel_group_command_channel_group" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="channel_group" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="server_id" baseTableName="channel_group_command" constraintName="fk_channel_group_command_server" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="command_id" baseTableName="channel_group_command" constraintName="fk_channel_group_command_command" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="command" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS channel_group_command_update_trigger ON channel_group_command;
            CREATE TRIGGER channel_group_command_update_trigger BEFORE UPDATE ON channel_group_command FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS channel_group_command_insert_trigger ON channel_group_command;
            CREATE TRIGGER channel_group_command_insert_trigger BEFORE INSERT ON channel_group_command FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
</databaseChangeLog>