matrix-appservice-kakaotalk/matrix_appservice_kakaotalk/db/upgrade/v04_read_receipt_sync.py

53 lines
2.4 KiB
Python

# matrix-appservice-kakaotalk - A Matrix-KakaoTalk puppeting bridge.
# Copyright (C) 2022 Tulir Asokan, Andrew Ferrazzutti
#
# 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="Fix message table and add tracking to assist with backfilling read receipts")
async def upgrade_v4(conn: Connection, scheme: Scheme) -> None:
if scheme != Scheme.SQLITE:
await conn.execute("ALTER TABLE message RENAME COLUMN kt_chat TO kt_channel")
await conn.execute("ALTER TABLE message DROP CONSTRAINT message_pkey")
await conn.execute('ALTER TABLE message ADD PRIMARY KEY (ktid, kt_channel, kt_receiver, "index")')
else:
await conn.execute(
"""CREATE TABLE message_v4 (
mxid TEXT NOT NULL,
mx_room TEXT NOT NULL,
ktid BIGINT,
kt_receiver BIGINT NOT NULL,
"index" SMALLINT NOT NULL,
kt_channel BIGINT NOT NULL,
timestamp BIGINT NOT NULL,
PRIMARY KEY (ktid, kt_channel, kt_receiver, "index"),
FOREIGN KEY (kt_channel, kt_receiver) REFERENCES portal(ktid, kt_receiver)
ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE (mxid, mx_room)
)"""
)
await conn.execute(
"""
INSERT INTO message_v4 (mxid, mx_room, ktid, kt_receiver, "index", kt_channel, timestamp)
SELECT mxid, mx_room, ktid, kt_receiver, "index", kt_chat, timestamp FROM message
"""
)
await conn.execute("DROP TABLE message")
await conn.execute("ALTER TABLE message_v4 RENAME TO message")
await conn.execute("ALTER TABLE portal ADD COLUMN fully_read_kt_chat BIGINT")