From 473cc5ad9c7fa7721ac63d40ac546ca002251dc2 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 29 Oct 2022 13:41:40 -0400 Subject: [PATCH 1/3] Add index to speed up Message.find_recent query --- mautrix_telegram/db/upgrade/__init__.py | 1 + .../db/upgrade/v17_message_find_recent.py | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 mautrix_telegram/db/upgrade/v17_message_find_recent.py diff --git a/mautrix_telegram/db/upgrade/__init__.py b/mautrix_telegram/db/upgrade/__init__.py index f40e2781..1d91c93e 100644 --- a/mautrix_telegram/db/upgrade/__init__.py +++ b/mautrix_telegram/db/upgrade/__init__.py @@ -19,4 +19,5 @@ v14_puppet_custom_mxid_index, v15_backfill_anchor_id, v16_backfill_type, + v17_message_find_recent, ) diff --git a/mautrix_telegram/db/upgrade/v17_message_find_recent.py b/mautrix_telegram/db/upgrade/v17_message_find_recent.py new file mode 100644 index 00000000..8a249d97 --- /dev/null +++ b/mautrix_telegram/db/upgrade/v17_message_find_recent.py @@ -0,0 +1,25 @@ +# mautrix-telegram - A Matrix-Telegram 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 . +from mautrix.util.async_db import Connection + +from . import upgrade_table + + +@upgrade_table.register(description="Add index for Message.find_recent") +async def upgrade_v17(conn: Connection) -> None: + await conn.execute( + "CREATE INDEX message_mx_room_and_tgid_idx ON message(mx_room, tgid DESC)" + ) From 82c2af1cb50d277d9a8006974066591f52a652cd Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 29 Oct 2022 15:14:16 -0400 Subject: [PATCH 2/3] Don't error on pre-existing DB indexes Prevent some new DB upgrades from failing if an index they try to create already exists. This allows for manually adding an index to a live DB before its schema is "officially" upgraded. --- mautrix_telegram/db/upgrade/v14_puppet_custom_mxid_index.py | 2 +- mautrix_telegram/db/upgrade/v17_message_find_recent.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mautrix_telegram/db/upgrade/v14_puppet_custom_mxid_index.py b/mautrix_telegram/db/upgrade/v14_puppet_custom_mxid_index.py index c94d2e7e..87c03589 100644 --- a/mautrix_telegram/db/upgrade/v14_puppet_custom_mxid_index.py +++ b/mautrix_telegram/db/upgrade/v14_puppet_custom_mxid_index.py @@ -20,4 +20,4 @@ @upgrade_table.register(description="Add index to puppet custom_mxid column") async def upgrade_v14(conn: Connection) -> None: - await conn.execute("CREATE INDEX puppet_custom_mxid_idx ON puppet(custom_mxid)") + await conn.execute("CREATE INDEX IF NOT EXISTS puppet_custom_mxid_idx ON puppet(custom_mxid)") diff --git a/mautrix_telegram/db/upgrade/v17_message_find_recent.py b/mautrix_telegram/db/upgrade/v17_message_find_recent.py index 8a249d97..9f9002f3 100644 --- a/mautrix_telegram/db/upgrade/v17_message_find_recent.py +++ b/mautrix_telegram/db/upgrade/v17_message_find_recent.py @@ -21,5 +21,5 @@ @upgrade_table.register(description="Add index for Message.find_recent") async def upgrade_v17(conn: Connection) -> None: await conn.execute( - "CREATE INDEX message_mx_room_and_tgid_idx ON message(mx_room, tgid DESC)" + "CREATE INDEX IF NOT EXISTS message_mx_room_and_tgid_idx ON message(mx_room, tgid DESC)" ) From aeb590b074a957a94749f32ac8294400dcf522f7 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Mon, 31 Oct 2022 00:28:47 -0400 Subject: [PATCH 3/3] Update latest revision migration --- mautrix_telegram/db/upgrade/v00_latest_revision.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mautrix_telegram/db/upgrade/v00_latest_revision.py b/mautrix_telegram/db/upgrade/v00_latest_revision.py index e95c12e7..f5eb7c3e 100644 --- a/mautrix_telegram/db/upgrade/v00_latest_revision.py +++ b/mautrix_telegram/db/upgrade/v00_latest_revision.py @@ -15,7 +15,7 @@ # along with this program. If not, see . from mautrix.util.async_db import Connection, Scheme -latest_version = 16 +latest_version = 17 async def create_latest_tables(conn: Connection, scheme: Scheme) -> int: @@ -73,6 +73,7 @@ async def create_latest_tables(conn: Connection, scheme: Scheme) -> int: UNIQUE (mxid, mx_room, tg_space) )""" ) + await conn.execute("CREATE INDEX message_mx_room_and_tgid_idx ON message(mx_room, tgid DESC)") await conn.execute( """CREATE TABLE reaction ( mxid TEXT NOT NULL,