v11_drop_number_support.py 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. await conn.execute(
  35. "ALTER TABLE reaction DROP CONSTRAINT reaction_msg_author_msg_timestamp_signal_chat_id_signal_re_fkey"
  36. )
  37. await conn.execute("ALTER TABLE message ALTER COLUMN sender TYPE UUID USING sender::uuid")
  38. await conn.execute(
  39. "ALTER TABLE reaction ALTER COLUMN msg_author TYPE UUID USING msg_author::uuid"
  40. )
  41. await conn.execute(
  42. """
  43. ALTER TABLE reaction ADD CONSTRAINT reaction_message_fkey
  44. FOREIGN KEY (msg_author, msg_timestamp, signal_chat_id, signal_receiver)
  45. REFERENCES message(sender, timestamp, signal_chat_id, signal_receiver)
  46. ON DELETE CASCADE
  47. """
  48. )
  49. await conn.execute("ALTER TABLE reaction ALTER COLUMN author TYPE UUID USING author::uuid")
  50. await conn.execute(
  51. """
  52. ALTER TABLE message ADD CONSTRAINT message_sender_fkey
  53. FOREIGN KEY (sender) REFERENCES puppet(uuid) ON DELETE CASCADE
  54. """
  55. )
  56. await conn.execute(
  57. """
  58. ALTER TABLE reaction ADD CONSTRAINT reaction_author_fkey
  59. FOREIGN KEY (author) REFERENCES puppet(uuid) ON DELETE CASCADE
  60. """
  61. )
  62. else:
  63. await conn.execute(
  64. """CREATE TABLE new_puppet (
  65. uuid UUID PRIMARY KEY,
  66. number TEXT UNIQUE,
  67. name TEXT,
  68. name_quality INTEGER NOT NULL DEFAULT 0,
  69. avatar_hash TEXT,
  70. avatar_url TEXT,
  71. name_set BOOLEAN NOT NULL DEFAULT false,
  72. avatar_set BOOLEAN NOT NULL DEFAULT false,
  73. is_registered BOOLEAN NOT NULL DEFAULT false,
  74. custom_mxid TEXT,
  75. access_token TEXT,
  76. next_batch TEXT,
  77. base_url TEXT
  78. )"""
  79. )
  80. await conn.execute(
  81. """
  82. INSERT INTO new_puppet (
  83. uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
  84. is_registered, custom_mxid, access_token, next_batch, base_url
  85. )
  86. SELECT uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
  87. uuid_registered, custom_mxid, access_token, next_batch, base_url
  88. FROM puppet
  89. """
  90. )
  91. await conn.execute("DROP TABLE puppet")
  92. await conn.execute("ALTER TABLE new_puppet RENAME TO puppet")