v00_latest_revision.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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=12)
  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. contact_info_set BOOLEAN NOT NULL DEFAULT false,
  57. is_registered BOOLEAN NOT NULL DEFAULT false,
  58. custom_mxid TEXT,
  59. access_token TEXT,
  60. next_batch TEXT,
  61. base_url TEXT
  62. )"""
  63. )
  64. await conn.execute(
  65. """CREATE TABLE user_portal (
  66. "user" TEXT,
  67. portal TEXT,
  68. portal_receiver TEXT,
  69. in_community BOOLEAN NOT NULL DEFAULT false,
  70. FOREIGN KEY (portal, portal_receiver) REFERENCES portal(chat_id, receiver)
  71. ON UPDATE CASCADE ON DELETE CASCADE
  72. )"""
  73. )
  74. await conn.execute(
  75. """CREATE TABLE message (
  76. mxid TEXT NOT NULL,
  77. mx_room TEXT NOT NULL,
  78. sender UUID,
  79. timestamp BIGINT,
  80. signal_chat_id TEXT,
  81. signal_receiver TEXT,
  82. PRIMARY KEY (sender, timestamp, signal_chat_id, signal_receiver),
  83. FOREIGN KEY (signal_chat_id, signal_receiver) REFERENCES portal(chat_id, receiver) ON DELETE CASCADE,
  84. FOREIGN KEY (sender) REFERENCES puppet(uuid) ON DELETE CASCADE,
  85. UNIQUE (mxid, mx_room)
  86. )"""
  87. )
  88. await conn.execute(
  89. """CREATE TABLE reaction (
  90. mxid TEXT NOT NULL,
  91. mx_room TEXT NOT NULL,
  92. signal_chat_id TEXT NOT NULL,
  93. signal_receiver TEXT NOT NULL,
  94. msg_author UUID NOT NULL,
  95. msg_timestamp BIGINT NOT NULL,
  96. author UUID NOT NULL,
  97. emoji TEXT NOT NULL,
  98. PRIMARY KEY (signal_chat_id, signal_receiver, msg_author, msg_timestamp, author),
  99. CONSTRAINT reaction_message_fkey
  100. FOREIGN KEY (msg_author, msg_timestamp, signal_chat_id, signal_receiver)
  101. REFERENCES message(sender, timestamp, signal_chat_id, signal_receiver)
  102. ON DELETE CASCADE,
  103. FOREIGN KEY (author) REFERENCES puppet(uuid) ON DELETE CASCADE,
  104. UNIQUE (mxid, mx_room)
  105. )"""
  106. )
  107. await conn.execute(
  108. """CREATE TABLE disappearing_message (
  109. room_id TEXT,
  110. mxid TEXT,
  111. expiration_seconds BIGINT,
  112. expiration_ts BIGINT,
  113. PRIMARY KEY (room_id, mxid)
  114. )"""
  115. )