messagedb.xml 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <databaseChangeLog
  3. xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
  6. http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd">
  7. <changeSet id="1" author="moxie">
  8. <dropTable tableName="messages"/>
  9. <createTable tableName="messages">
  10. <column name="id" type="bigint" autoIncrement="true">
  11. <constraints primaryKey="true" nullable="false"/>
  12. </column>
  13. <column name="type" type="tinyint">
  14. <constraints nullable="false"/>
  15. </column>
  16. <column name="relay" type="text">
  17. <constraints nullable="false"/>
  18. </column>
  19. <column name="timestamp" type="bigint">
  20. <constraints nullable="false"/>
  21. </column>
  22. <column name="source" type="text">
  23. <constraints nullable="false"/>
  24. </column>
  25. <column name="source_device" type="int">
  26. <constraints nullable="false"/>
  27. </column>
  28. <column name="destination" type="text">
  29. <constraints nullable="false"/>
  30. </column>
  31. <column name="destination_device" type="int">
  32. <constraints nullable="false"/>
  33. </column>
  34. <column name="message" type="bytea">
  35. <constraints nullable="false"/>
  36. </column>
  37. </createTable>
  38. <createIndex tableName="messages" indexName="destination_index">
  39. <column name="destination"></column>
  40. <column name="destination_device"></column>
  41. </createIndex>
  42. <createIndex tableName="messages" indexName="destination_and_type_index">
  43. <column name="destination"></column>
  44. <column name="destination_device"></column>
  45. <column name="type"></column>
  46. </createIndex>
  47. </changeSet>
  48. <changeSet id="2" author="moxie">
  49. <addColumn tableName="messages">
  50. <column name="content" type="bytea"/>
  51. </addColumn>
  52. <dropNotNullConstraint tableName="messages" columnName="message"/>
  53. </changeSet>
  54. <changeSet id="3" author="moxie">
  55. <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>
  56. </changeSet>
  57. <changeSet id="4" author="moxie">
  58. <sql>DROP RULE bounded_message_queue ON messages;</sql>
  59. <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>
  60. </changeSet>
  61. </databaseChangeLog>