upgrade.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2020 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from asyncpg import Connection
  17. from mautrix.util.async_db import UpgradeTable
  18. upgrade_table = UpgradeTable()
  19. @upgrade_table.register(description="Initial revision")
  20. async def upgrade_v1(conn: Connection) -> None:
  21. await conn.execute("""CREATE TABLE portal (
  22. chat_id TEXT,
  23. receiver TEXT,
  24. mxid TEXT,
  25. name TEXT,
  26. encrypted BOOLEAN NOT NULL DEFAULT false,
  27. PRIMARY KEY (chat_id, receiver)
  28. )""")
  29. await conn.execute("""CREATE TABLE "user" (
  30. mxid TEXT PRIMARY KEY,
  31. username TEXT,
  32. uuid UUID,
  33. notice_room TEXT
  34. )""")
  35. await conn.execute("""CREATE TABLE puppet (
  36. uuid UUID UNIQUE,
  37. number TEXT UNIQUE,
  38. name TEXT,
  39. uuid_registered BOOLEAN NOT NULL DEFAULT false,
  40. number_registered BOOLEAN NOT NULL DEFAULT false,
  41. custom_mxid TEXT,
  42. access_token TEXT,
  43. next_batch TEXT
  44. )""")
  45. await conn.execute("""CREATE TABLE user_portal (
  46. "user" TEXT,
  47. portal TEXT,
  48. portal_receiver TEXT,
  49. in_community BOOLEAN NOT NULL DEFAULT false,
  50. FOREIGN KEY (portal, portal_receiver) REFERENCES portal(chat_id, receiver)
  51. ON UPDATE CASCADE ON DELETE CASCADE
  52. )""")
  53. await conn.execute("""CREATE TABLE message (
  54. mxid TEXT NOT NULL,
  55. mx_room TEXT NOT NULL,
  56. sender UUID,
  57. timestamp BIGINT,
  58. signal_chat_id TEXT,
  59. signal_receiver TEXT,
  60. PRIMARY KEY (sender, timestamp, signal_chat_id, signal_receiver),
  61. FOREIGN KEY (signal_chat_id, signal_receiver) REFERENCES portal(chat_id, receiver)
  62. ON UPDATE CASCADE ON DELETE CASCADE,
  63. UNIQUE (mxid, mx_room)
  64. )""")
  65. await conn.execute("""CREATE TABLE reaction (
  66. mxid TEXT NOT NULL,
  67. mx_room TEXT NOT NULL,
  68. signal_chat_id TEXT NOT NULL,
  69. signal_receiver TEXT NOT NULL,
  70. msg_author UUID NOT NULL,
  71. msg_timestamp BIGINT NOT NULL,
  72. author UUID NOT NULL,
  73. emoji TEXT NOT NULL,
  74. PRIMARY KEY (signal_chat_id, signal_receiver, msg_author, msg_timestamp, author),
  75. FOREIGN KEY (msg_author, msg_timestamp, signal_chat_id, signal_receiver)
  76. REFERENCES message(sender, timestamp, signal_chat_id, signal_receiver)
  77. ON DELETE CASCADE ON UPDATE CASCADE,
  78. UNIQUE (mxid, mx_room)
  79. )""")