<?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="streamer-table">
        <createTable tableName="streamer">
            <column name="id" type="BIGINT" autoIncrement="true">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="user_id" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            <column name="template_key" type="VARCHAR(128)">
                <constraints nullable="true"/>
            </column>
            <column name="name" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="notification_channel_id" type="BIGINT">
                <constraints nullable="true"/>
            </column>
            <column name="creator_user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="streamer_user_in_server_id" type="BIGINT">
                <constraints nullable="true"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="current_game_id" type="VARCHAR(255)">
                <constraints nullable="true"/>
            </column>
            <column name="online" type="BOOLEAN">
                <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="show_notifications" type="BOOLEAN">
                <constraints nullable="true"/>
            </column>
        </createTable>
        <addUniqueConstraint
                columnNames="user_id, server_id"
                constraintName="uq_streamer_id"
                tableName="streamer"
        />
        <addForeignKeyConstraint baseColumnNames="notification_channel_id" baseTableName="streamer" constraintName="fk_streamer_notification_channel"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="id" referencedTableName="channel" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="creator_user_in_server_id" baseTableName="streamer" constraintName="fk_streamer_creator"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="streamer_user_in_server_id" baseTableName="streamer" constraintName="fk_streamer_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="streamer" constraintName="fk_streamer_server"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS streamer_update_trigger ON streamer;
            CREATE TRIGGER streamer_update_trigger BEFORE UPDATE ON streamer FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS streamer_insert_trigger ON streamer;
            CREATE TRIGGER streamer_insert_trigger BEFORE INSERT ON streamer FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
</databaseChangeLog>