v11_drop_number_support.py 5.0 KB

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