messagedb.xml 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. <createTable tableName="messages">
  9. <column name="id" type="bigint" autoIncrement="true">
  10. <constraints primaryKey="true" nullable="false"/>
  11. </column>
  12. <column name="type" type="tinyint">
  13. <constraints nullable="false"/>
  14. </column>
  15. <column name="relay" type="text">
  16. <constraints nullable="false"/>
  17. </column>
  18. <column name="timestamp" type="bigint">
  19. <constraints nullable="false"/>
  20. </column>
  21. <column name="source" type="text">
  22. <constraints nullable="false"/>
  23. </column>
  24. <column name="source_device" type="int">
  25. <constraints nullable="false"/>
  26. </column>
  27. <column name="destination" type="text">
  28. <constraints nullable="false"/>
  29. </column>
  30. <column name="destination_device" type="int">
  31. <constraints nullable="false"/>
  32. </column>
  33. <column name="message" type="bytea">
  34. <constraints nullable="false"/>
  35. </column>
  36. <column name="account_id" type="bigint">
  37. <constraints nullable="false"/>
  38. </column>
  39. <column name="device_id" type="bigint">
  40. <constraints nullable="false"/>
  41. </column>
  42. <column name="encrypted_message" type="text">
  43. <constraints nullable="false"/>
  44. </column>
  45. </createTable>
  46. <createIndex tableName="messages" indexName="destination_index">
  47. <column name="destination"></column>
  48. <column name="destination_device"></column>
  49. </createIndex>
  50. <createIndex tableName="messages" indexName="destination_and_type_index">
  51. <column name="destination"></column>
  52. <column name="destination_device"></column>
  53. <column name="type"></column>
  54. </createIndex>
  55. <createIndex tableName="messages" indexName="messages_account_and_device">
  56. <column name="account_id"/>
  57. <column name="device_id"/>
  58. </createIndex>
  59. </changeSet>
  60. <changeSet id="2" author="moxie">
  61. <addColumn tableName="messages">
  62. <column name="content" type="bytea"/>
  63. </addColumn>
  64. <dropNotNullConstraint tableName="messages" columnName="message"/>
  65. </changeSet>
  66. <changeSet id="3" author="moxie">
  67. <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>
  68. </changeSet>
  69. <changeSet id="4" author="moxie">
  70. <sql>DROP RULE bounded_message_queue ON messages;</sql>
  71. <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>
  72. </changeSet>
  73. </databaseChangeLog>