From d9f32492aba7dca3171e66d72417951ef5fd29ee Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 8 Nov 2021 12:19:49 +0000 Subject: [PATCH] Make get_new_messages_txn SQL easier to read --- synapse/storage/databases/main/deviceinbox.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/synapse/storage/databases/main/deviceinbox.py b/synapse/storage/databases/main/deviceinbox.py index f50ac55baccf..ee9d9243a68c 100644 --- a/synapse/storage/databases/main/deviceinbox.py +++ b/synapse/storage/databases/main/deviceinbox.py @@ -162,7 +162,6 @@ async def get_new_messages( def get_new_messages_txn(txn: LoggingTransaction): # Build a query to select messages from any of the given users that are between # the given stream id bounds - sql = "SELECT stream_id, user_id, device_id, message_json FROM device_inbox" # Scope to only the given users. We need to use this method as doing so is # different across database engines. @@ -170,12 +169,13 @@ def get_new_messages_txn(txn: LoggingTransaction): self.database_engine, "user_id", user_ids ) - sql += ( - " WHERE %s" - " AND ? < stream_id AND stream_id <= ?" - " ORDER BY stream_id ASC" - " LIMIT ?" - ) % many_clause_sql + sql = f""" + SELECT stream_id, user_id, device_id, message_json FROM device_inbox + WHERE {many_clause_sql} + AND ? < stream_id AND stream_id <= ? + ORDER BY stream_id ASC + LIMIT ? + """ txn.execute(sql, (*many_clause_args, from_stream_id, to_stream_id, limit))