2022-03-18-historysync-store.go 3.2 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. 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. id SERIAL PRIMARY KEY,
  35. user_mxid TEXT,
  36. conversation_id TEXT,
  37. timestamp TIMESTAMP,
  38. data BYTEA,
  39. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  40. FOREIGN KEY (conversation_id) REFERENCES history_sync_conversation(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. UNIQUE (conversation_id),
  64. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  65. FOREIGN KEY (portal_jid, portal_receiver) REFERENCES portal(jid, receiver) ON DELETE CASCADE ON UPDATE CASCADE
  66. )
  67. `)
  68. if err != nil {
  69. return err
  70. }
  71. _, err = tx.Exec(`
  72. CREATE TABLE history_sync_message (
  73. id INTEGER PRIMARY KEY,
  74. user_mxid TEXT,
  75. conversation_id TEXT,
  76. timestamp DATETIME,
  77. data BLOB,
  78. FOREIGN KEY (user_mxid) REFERENCES "user"(mxid) ON DELETE CASCADE ON UPDATE CASCADE,
  79. FOREIGN KEY (conversation_id) REFERENCES history_sync_conversation(conversation_id) ON DELETE CASCADE
  80. )
  81. `)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return nil
  87. }}
  88. }