v11_drop_number_support.py 4.4 KB

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