v00_latest_revision.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # mautrix-signal - A Matrix-Signal puppeting bridge
  2. # Copyright (C) 2022 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 mautrix.util.async_db import Connection
  17. from . import upgrade_table
  18. @upgrade_table.register(description="Initial revision", upgrades_to=11)
  19. async def upgrade_latest(conn: Connection) -> None:
  20. await conn.execute(
  21. """CREATE TABLE portal (
  22. chat_id TEXT,
  23. receiver TEXT,
  24. mxid TEXT,
  25. name TEXT,
  26. topic TEXT,
  27. encrypted BOOLEAN NOT NULL DEFAULT false,
  28. avatar_hash TEXT,
  29. avatar_url TEXT,
  30. name_set BOOLEAN NOT NULL DEFAULT false,
  31. avatar_set BOOLEAN NOT NULL DEFAULT false,
  32. revision INTEGER NOT NULL DEFAULT 0,
  33. expiration_time BIGINT,
  34. relay_user_id TEXT,
  35. PRIMARY KEY (chat_id, receiver)
  36. )"""
  37. )
  38. await conn.execute(
  39. """CREATE TABLE "user" (
  40. mxid TEXT PRIMARY KEY,
  41. username TEXT,
  42. uuid UUID,
  43. notice_room TEXT
  44. )"""
  45. )
  46. await conn.execute(
  47. """CREATE TABLE puppet (
  48. uuid UUID PRIMARY KEY,
  49. number TEXT UNIQUE,
  50. name TEXT,
  51. name_quality INTEGER NOT NULL DEFAULT 0,
  52. avatar_hash TEXT,
  53. avatar_url TEXT,
  54. name_set BOOLEAN NOT NULL DEFAULT false,
  55. avatar_set BOOLEAN NOT NULL DEFAULT false,
  56. is_registered BOOLEAN NOT NULL DEFAULT false,
  57. custom_mxid TEXT,
  58. access_token TEXT,
  59. next_batch TEXT,
  60. base_url TEXT
  61. )"""
  62. )
  63. await conn.execute(
  64. """CREATE TABLE user_portal (
  65. "user" TEXT,
  66. portal TEXT,
  67. portal_receiver TEXT,
  68. in_community BOOLEAN NOT NULL DEFAULT false,
  69. FOREIGN KEY (portal, portal_receiver) REFERENCES portal(chat_id, receiver)
  70. ON UPDATE CASCADE ON DELETE CASCADE
  71. )"""
  72. )
  73. await conn.execute(
  74. """CREATE TABLE message (
  75. mxid TEXT NOT NULL,
  76. mx_room TEXT NOT NULL,
  77. sender UUID,
  78. timestamp BIGINT,
  79. signal_chat_id TEXT,
  80. signal_receiver TEXT,
  81. PRIMARY KEY (sender, timestamp, signal_chat_id, signal_receiver),
  82. FOREIGN KEY (signal_chat_id, signal_receiver) REFERENCES portal(chat_id, receiver) ON DELETE CASCADE,
  83. FOREIGN KEY (sender) REFERENCES puppet(uuid) ON DELETE CASCADE,
  84. UNIQUE (mxid, mx_room)
  85. )"""
  86. )
  87. await conn.execute(
  88. """CREATE TABLE reaction (
  89. mxid TEXT NOT NULL,
  90. mx_room TEXT NOT NULL,
  91. signal_chat_id TEXT NOT NULL,
  92. signal_receiver TEXT NOT NULL,
  93. msg_author UUID NOT NULL,
  94. msg_timestamp BIGINT NOT NULL,
  95. author UUID NOT NULL,
  96. emoji TEXT NOT NULL,
  97. PRIMARY KEY (signal_chat_id, signal_receiver, msg_author, msg_timestamp, author),
  98. CONSTRAINT reaction_message_fkey
  99. FOREIGN KEY (msg_author, msg_timestamp, signal_chat_id, signal_receiver)
  100. REFERENCES message(sender, timestamp, signal_chat_id, signal_receiver)
  101. ON DELETE CASCADE,
  102. FOREIGN KEY (author) REFERENCES puppet(uuid) ON DELETE CASCADE,
  103. UNIQUE (mxid, mx_room)
  104. )"""
  105. )
  106. await conn.execute(
  107. """CREATE TABLE disappearing_message (
  108. room_id TEXT,
  109. mxid TEXT,
  110. expiration_seconds BIGINT,
  111. expiration_ts BIGINT,
  112. PRIMARY KEY (room_id, mxid)
  113. )"""
  114. )