<?xml version="1.0" encoding="UTF-8"?>

<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
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">

    <changeSet id="1" author="moxie">
        <createTable tableName="messages">
            <column name="id" type="bigint" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>

            <column name="type" type="tinyint">
                <constraints nullable="false"/>
            </column>

            <column name="relay" type="text">
                <constraints nullable="false"/>
            </column>

            <column name="timestamp" type="bigint">
                <constraints nullable="false"/>
            </column>

            <column name="source" type="text">
                <constraints nullable="false"/>
            </column>

            <column name="source_device" type="int">
                <constraints nullable="false"/>
            </column>

            <column name="destination" type="text">
                <constraints nullable="false"/>
            </column>

            <column name="destination_device" type="int">
                <constraints nullable="false"/>
            </column>

            <column name="message" type="bytea">
                <constraints nullable="false"/>
            </column>
        </createTable>

        <createIndex tableName="messages" indexName="destination_index">
            <column name="destination"></column>
            <column name="destination_device"></column>
        </createIndex>

        <createIndex tableName="messages" indexName="destination_and_type_index">
            <column name="destination"></column>
            <column name="destination_device"></column>
            <column name="type"></column>
        </createIndex>
    </changeSet>

    <changeSet id="2" author="moxie">
        <addColumn tableName="messages">
            <column name="content" type="bytea"/>
        </addColumn>

        <dropNotNullConstraint tableName="messages" columnName="message"/>
    </changeSet>

    <changeSet id="3" author="moxie">
        <sql>CREATE RULE bounded_message_queue AS ON INSERT TO messages DO ALSO DELETE FROM messages WHERE id IN (SELECT id FROM messages WHERE destination = NEW.destination AND destination_device = NEW.destination_device ORDER BY timestamp DESC OFFSET 5000);</sql>
    </changeSet>

    <changeSet id="4" author="moxie">
        <sql>DROP RULE bounded_message_queue ON messages;</sql>
        <sql>CREATE RULE bounded_message_queue AS ON INSERT TO messages DO ALSO DELETE FROM messages WHERE id IN (SELECT id FROM messages WHERE destination = NEW.destination AND destination_device = NEW.destination_device ORDER BY timestamp DESC OFFSET 1000);</sql>
    </changeSet>

    <changeSet id="5" author="moxie">
        <addColumn tableName="messages">
            <column name="deleted" type="integer"/>
        </addColumn>

        <sql>DROP RULE bounded_message_queue ON messages;</sql>
    </changeSet>

    <changeSet id="6" author="moxie">
        <sql>CREATE RULE bounded_message_queue AS ON INSERT TO messages DO ALSO DELETE FROM messages WHERE id IN (SELECT id FROM messages WHERE destination = NEW.destination AND destination_device = NEW.destination_device ORDER BY timestamp DESC OFFSET 1000);</sql>
    </changeSet>

    <changeSet id="7" author="moxie">
        <dropColumn tableName="messages" columnName="deleted"/>

        <sql>DROP RULE bounded_message_queue ON messages;</sql>
    </changeSet>

    <changeSet id="8" author="moxie">
        <sql>CREATE RULE bounded_message_queue AS ON INSERT TO messages DO ALSO DELETE FROM messages WHERE id IN (SELECT id FROM messages WHERE destination = NEW.destination AND destination_device = NEW.destination_device ORDER BY timestamp DESC OFFSET 1000);</sql>
    </changeSet>

    <changeSet id="9" author="moxie">
        <sql>DROP RULE bounded_message_queue ON messages;</sql>
    </changeSet>

    <changeSet id="10" author="moxie">
        <sql>CREATE RULE bounded_message_queue AS ON INSERT TO messages DO ALSO DELETE FROM messages WHERE id IN (SELECT id FROM messages WHERE destination = NEW.destination AND destination_device = NEW.destination_device ORDER BY timestamp DESC OFFSET 1000);</sql>
    </changeSet>

    <changeSet id="11" author="moxie">
        <addColumn tableName="messages">
            <column name="guid" type="uuid"/>
        </addColumn>

        <addColumn tableName="messages">
            <column name="server_timestamp" type="bigint"/>
        </addColumn>

        <dropNotNullConstraint tableName="messages" columnName="source"/>
        <dropNotNullConstraint tableName="messages" columnName="source_device"/>
    </changeSet>

    <changeSet id="12" author="moxie" runInTransaction="false">
        <sql>CREATE INDEX CONCURRENTLY guid_index ON messages (guid);</sql>
    </changeSet>

    <changeSet id="13" author="moxie">
        <addColumn tableName="messages">
            <column name="source_uuid" type="uuid"/>
        </addColumn>
    </changeSet>

</databaseChangeLog>
