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/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,
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
new file mode 100644
index 00000000..9f9002f3
--- /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 IF NOT EXISTS message_mx_room_and_tgid_idx ON message(mx_room, tgid DESC)"
+ )