<?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="users-table">
        <createTable tableName="auser">
            <column name="id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="users_pkey"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="true"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
        </createTable>
        <sql>
            DROP TRIGGER IF EXISTS auser_update_trigger ON auser;
            CREATE TRIGGER auser_update_trigger BEFORE UPDATE ON auser FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS auser_insert_trigger ON auser;
            CREATE TRIGGER auser_insert_trigger BEFORE INSERT ON auser FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>
    <changeSet author="Sheldan" id="user_in_server-table">
        <createTable tableName="user_in_server">
            <column autoIncrement="true" name="user_in_server_id" type="BIGINT">
                <constraints nullable="false" primaryKey="true" primaryKeyName="user_in_server_pkey"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="true"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="user_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addUniqueConstraint columnNames="user_id, server_id"
                             constraintName="uc_user_in_server"
                             disabled="false"
                             tableName="user_in_server"/>
        <addForeignKeyConstraint baseColumnNames="user_id" baseTableName="user_in_server" constraintName="fk_auser_user" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="auser" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="server_id" baseTableName="user_in_server" constraintName="fk_user_server" deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION" referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <createIndex indexName="idx_user_in_server" tableName="user_in_server">
            <column name="server_id"/>
            <column name="user_id"/>
        </createIndex>
        <sql>
            DROP TRIGGER IF EXISTS user_in_server_update_trigger ON user_in_server;
            CREATE TRIGGER user_in_server_update_trigger BEFORE UPDATE ON user_in_server FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS user_in_server_insert_trigger ON user_in_server;
            CREATE TRIGGER user_in_server_insert_trigger BEFORE INSERT ON user_in_server FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
    </changeSet>

</databaseChangeLog>