12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- # mautrix-signal - A Matrix-Signal puppeting bridge
- # Copyright (C) 2022 Tulir Asokan
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU Affero General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Affero General Public License for more details.
- #
- # You should have received a copy of the GNU Affero General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
- from mautrix.util.async_db import Connection, Scheme
- from . import upgrade_table
- @upgrade_table.register(description="Drop support for phone numbers as puppet identifiers")
- async def upgrade_v11(conn: Connection, scheme: Scheme) -> None:
- await conn.execute("DELETE FROM portal WHERE chat_id LIKE '+%'")
- await conn.execute("DELETE FROM message WHERE sender LIKE '+%'")
- await conn.execute("DELETE FROM reaction WHERE author LIKE '+%'")
- await conn.execute("DELETE FROM puppet WHERE uuid IS NULL")
- if scheme in (Scheme.POSTGRES, Scheme.COCKROACH):
- await conn.execute(
- """
- ALTER TABLE puppet
- DROP CONSTRAINT puppet_uuid_key,
- ADD CONSTRAINT puppet_pkey PRIMARY KEY (uuid)
- """
- )
- await conn.execute("ALTER TABLE puppet DROP COLUMN number_registered")
- await conn.execute("ALTER TABLE puppet RENAME COLUMN uuid_registered TO is_registered")
- await conn.execute(
- "ALTER TABLE reaction DROP CONSTRAINT reaction_msg_author_msg_timestamp_signal_chat_id_signal_re_fkey"
- )
- await conn.execute("ALTER TABLE message ALTER COLUMN sender TYPE UUID USING sender::uuid")
- await conn.execute(
- "ALTER TABLE reaction ALTER COLUMN msg_author TYPE UUID USING msg_author::uuid"
- )
- await conn.execute(
- """
- ALTER TABLE reaction ADD CONSTRAINT reaction_message_fkey
- FOREIGN KEY (msg_author, msg_timestamp, signal_chat_id, signal_receiver)
- REFERENCES message(sender, timestamp, signal_chat_id, signal_receiver)
- ON DELETE CASCADE
- """
- )
- await conn.execute("ALTER TABLE reaction ALTER COLUMN author TYPE UUID USING author::uuid")
- await conn.execute(
- """
- ALTER TABLE message ADD CONSTRAINT message_sender_fkey
- FOREIGN KEY (sender) REFERENCES puppet(uuid) ON DELETE CASCADE
- """
- )
- await conn.execute(
- """
- ALTER TABLE reaction ADD CONSTRAINT reaction_author_fkey
- FOREIGN KEY (author) REFERENCES puppet(uuid) ON DELETE CASCADE
- """
- )
- else:
- await conn.execute(
- """CREATE TABLE new_puppet (
- uuid UUID PRIMARY KEY,
- number TEXT UNIQUE,
- name TEXT,
- name_quality INTEGER NOT NULL DEFAULT 0,
- avatar_hash TEXT,
- avatar_url TEXT,
- name_set BOOLEAN NOT NULL DEFAULT false,
- avatar_set BOOLEAN NOT NULL DEFAULT false,
- is_registered BOOLEAN NOT NULL DEFAULT false,
- custom_mxid TEXT,
- access_token TEXT,
- next_batch TEXT,
- base_url TEXT
- )"""
- )
- await conn.execute(
- """
- INSERT INTO new_puppet (
- uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
- is_registered, custom_mxid, access_token, next_batch, base_url
- )
- SELECT uuid, number, name, name_quality, avatar_hash, avatar_url, name_set, avatar_set,
- uuid_registered, custom_mxid, access_token, next_batch, base_url
- FROM puppet
- """
- )
- await conn.execute("DROP TABLE puppet")
- await conn.execute("ALTER TABLE new_puppet RENAME TO puppet")
|