v11_drop_number_support.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, Scheme
  17. from . import upgrade_table
  18. @upgrade_table.register(description="Drop support for phone numbers as puppet identifiers")
  19. async def upgrade_v11(conn: Connection, scheme: Scheme) -> None:
  20. await conn.execute("DELETE FROM portal WHERE chat_id LIKE '+%'")
  21. await conn.execute("DELETE FROM message WHERE sender LIKE '+%'")
  22. await conn.execute("DELETE FROM reaction WHERE author LIKE '+%'")
  23. puppet_uuid_as_text = "puppet.uuid" if scheme == Scheme.SQLITE else "puppet.uuid::text"
  24. await conn.execute(
  25. f"""
  26. DELETE FROM message WHERE sender IN (
  27. SELECT DISTINCT(message.sender) FROM message
  28. LEFT JOIN puppet ON message.sender={puppet_uuid_as_text}
  29. WHERE puppet.uuid IS NULL
  30. )
  31. """
  32. )
  33. await conn.execute(
  34. f"""
  35. DELETE FROM reaction WHERE author IN (
  36. SELECT DISTINCT(reaction.author) FROM reaction
  37. LEFT JOIN puppet ON reaction.author={puppet_uuid_as_text}
  38. WHERE puppet.uuid IS NULL
  39. )
  40. """
  41. )
  42. await conn.execute("DELETE FROM puppet WHERE uuid IS NULL")
  43. if scheme in (Scheme.POSTGRES, Scheme.COCKROACH):
  44. await conn.execute(
  45. """
  46. ALTER TABLE puppet
  47. DROP CONSTRAINT puppet_uuid_key,
  48. ADD CONSTRAINT puppet_pkey PRIMARY KEY (uuid)
  49. """
  50. )
  51. await conn.execute("ALTER TABLE puppet DROP COLUMN number_registered")
  52. await conn.execute("ALTER TABLE puppet RENAME COLUMN uuid_registered TO is_registered")
  53. for c_row in await conn.fetch(
  54. "SELECT constraint_name FROM information_schema.table_constraints tc "
  55. "WHERE tc.constraint_type='FOREIGN KEY' AND tc.table_name='reaction'"
  56. ):
  57. constraint_name = c_row["constraint_name"]
  58. if constraint_name.startswith("reaction_msg_author_"):
  59. await conn.execute(f"ALTER TABLE reaction DROP CONSTRAINT {constraint_name}")
  60. await conn.execute("ALTER TABLE message ALTER COLUMN sender TYPE UUID USING sender::uuid")
  61. await conn.execute(
  62. "ALTER TABLE reaction ALTER COLUMN msg_author TYPE UUID USING msg_author::uuid"
  63. )
  64. await conn.execute(
  65. """
  66. ALTER TABLE reaction ADD CONSTRAINT reaction_message_fkey
  67. FOREIGN KEY (msg_author, msg_timestamp, signal_chat_id, signal_receiver)
  68. REFERENCES message(sender, timestamp, signal_chat_id, signal_receiver)
  69. ON DELETE CASCADE
  70. """
  71. )
  72. await conn.execute("ALTER TABLE reaction ALTER COLUMN author TYPE UUID USING author::uuid")
  73. await conn.execute(
  74. """
  75. ALTER TABLE message ADD CONSTRAINT message_sender_fkey
  76. FOREIGN KEY (sender) REFERENCES puppet(uuid) ON DELETE CASCADE
  77. """
  78. )
  79. await conn.execute(
  80. """
  81. ALTER TABLE reaction ADD CONSTRAINT reaction_author_fkey
  82. FOREIGN KEY (author) REFERENCES puppet(uuid) ON DELETE CASCADE
  83. """
  84. )
  85. else:
  86. await conn.execute(
  87. """CREATE TABLE new_puppet (
  88. uuid UUID PRIMARY KEY,
  89. number TEXT UNIQUE,
  90. name TEXT,
  91. name_quality INTEGER NOT NULL DEFAULT 0,
  92. avatar_hash TEXT,
  93. avatar_url TEXT,
  94. name_set BOOLEAN NOT NULL DEFAULT false,
  95. avatar_set BOOLEAN NOT NULL DEFAULT false,
  96. is_registered BOOLEAN NOT NULL DEFAULT false,
  97. custom_mxid TEXT,
  98. access_token TEXT,
  99. next_batch TEXT,
  100. base_url TEXT
  101. )"""
  102. )
  103. await conn.execute(
  104. """
  105. INSERT INTO new_puppet (
  106. uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
  107. is_registered, custom_mxid, access_token, next_batch, base_url
  108. )
  109. SELECT uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
  110. uuid_registered, custom_mxid, access_token, next_batch, base_url
  111. FROM puppet
  112. """
  113. )
  114. await conn.execute("DROP TABLE puppet")
  115. await conn.execute("ALTER TABLE new_puppet RENAME TO puppet")