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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  25. FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE ON UPDATE CASCADE
  26. )
  27. `)
  28. if err != nil {
  29. return err
  30. }
  31. _, err = tx.Exec(`
  32. CREATE TABLE history_sync_message (
  33. user_mxid TEXT,
  34. conversation_id TEXT,
  35. message_id TEXT,
  36. timestamp TIMESTAMP,
  37. data BYTEA,
  38. PRIMARY KEY (user_mxid, conversation_id, message_id),
  39. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  40. FOREIGN KEY (user_mxid, conversation_id) REFERENCES history_sync_conversation(user_mxid, conversation_id) ON DELETE CASCADE
  41. )
  42. `)
  43. if err != nil {
  44. return err
  45. }
  46. } else if ctx.dialect == SQLite {
  47. _, err := tx.Exec(`
  48. CREATE TABLE history_sync_conversation (
  49. user_mxid TEXT,
  50. conversation_id TEXT,
  51. portal_jid TEXT,
  52. portal_receiver TEXT,
  53. last_message_timestamp DATETIME,
  54. archived INTEGER,
  55. pinned INTEGER,
  56. mute_end_time DATETIME,
  57. disappearing_mode INTEGER,
  58. end_of_history_transfer_type INTEGER,
  59. ephemeral_expiration INTEGER,
  60. marked_as_unread INTEGER,
  61. unread_count INTEGER,
  62. PRIMARY KEY (user_mxid, 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. message_id TEXT,
  75. timestamp DATETIME,
  76. data BLOB,
  77. PRIMARY KEY (user_mxid, conversation_id, message_id),
  78. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  79. FOREIGN KEY (user_mxid, conversation_id) REFERENCES history_sync_conversation(user_mxid, conversation_id) ON DELETE CASCADE
  80. )
  81. `)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }}
  88. }