2022-03-18-historysync-store.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package upgrades
  2. import (
  3. "database/sql"
  4. )
  5. func init() {
  6. upgrades[40] = upgrade{"Store history syncs for later backfills", func(tx *sql.Tx, ctx context) error {
  7. if ctx.dialect == Postgres {
  8. _, err := tx.Exec(`
  9. CREATE TABLE history_sync_conversation (
  10. user_mxid TEXT,
  11. conversation_id TEXT,
  12. portal_jid TEXT,
  13. portal_receiver TEXT,
  14. last_message_timestamp TIMESTAMP,
  15. archived BOOLEAN,
  16. pinned INTEGER,
  17. mute_end_time TIMESTAMP,
  18. disappearing_mode INTEGER,
  19. end_of_history_transfer_type INTEGER,
  20. ephemeral_expiration INTEGER,
  21. marked_as_unread BOOLEAN,
  22. unread_count INTEGER,
  23. PRIMARY KEY (user_mxid, conversation_id),
  24. UNIQUE (conversation_id),
  25. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  26. FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE ON UPDATE CASCADE
  27. )
  28. `)
  29. if err != nil {
  30. return err
  31. }
  32. _, err = tx.Exec(`
  33. CREATE TABLE history_sync_message (
  34. user_mxid TEXT,
  35. conversation_id TEXT,
  36. timestamp TIMESTAMP,
  37. data BYTEA,
  38. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  39. FOREIGN KEY (conversation_id) REFERENCES history_sync_conversation(conversation_id) ON DELETE CASCADE
  40. )
  41. `)
  42. if err != nil {
  43. return err
  44. }
  45. } else if ctx.dialect == SQLite {
  46. _, err := tx.Exec(`
  47. CREATE TABLE history_sync_conversation (
  48. user_mxid TEXT,
  49. conversation_id TEXT,
  50. portal_jid TEXT,
  51. portal_receiver TEXT,
  52. last_message_timestamp DATETIME,
  53. archived INTEGER,
  54. pinned INTEGER,
  55. mute_end_time DATETIME,
  56. disappearing_mode INTEGER,
  57. end_of_history_transfer_type INTEGER,
  58. ephemeral_expiration INTEGER,
  59. marked_as_unread INTEGER,
  60. unread_count INTEGER,
  61. PRIMARY KEY (user_mxid, conversation_id),
  62. UNIQUE (conversation_id),
  63. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  64. FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE ON UPDATE CASCADE
  65. )
  66. `)
  67. if err != nil {
  68. return err
  69. }
  70. _, err = tx.Exec(`
  71. CREATE TABLE history_sync_message (
  72. user_mxid TEXT,
  73. conversation_id TEXT,
  74. timestamp DATETIME,
  75. data BLOB,
  76. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  77. FOREIGN KEY (conversation_id) REFERENCES history_sync_conversation(conversation_id) ON DELETE CASCADE
  78. )
  79. `)
  80. if err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }}
  86. }