cable-service/docker/messagedb.xml
2017-06-29 11:18:48 +02:00

94 lines
3.5 KiB
XML

<?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>
<column name="account_id" type="bigint">
<constraints nullable="false"/>
</column>
<column name="device_id" type="bigint">
<constraints nullable="false"/>
</column>
<column name="encrypted_message" type="text">
<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>
<createIndex tableName="messages" indexName="messages_account_and_device">
<column name="account_id"/>
<column name="device_id"/>
</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>
</databaseChangeLog>