From 980ee9aad63a2c3f3ebb141135a7cc1e202e6ce3 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Thu, 22 Aug 2024 15:40:08 -0500 Subject: [PATCH] Prefer `simple_update_txn` See https://github.com/element-hq/synapse/pull/17512#discussion_r1726808112 --- synapse/storage/database.py | 5 +++-- synapse/storage/databases/main/events.py | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/synapse/storage/database.py b/synapse/storage/database.py index 569f618193..66a7238deb 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -35,6 +35,7 @@ from typing import ( Iterable, Iterator, List, + Mapping, Optional, Sequence, Tuple, @@ -1966,8 +1967,8 @@ class DatabasePool: def simple_update_txn( txn: LoggingTransaction, table: str, - keyvalues: Dict[str, Any], - updatevalues: Dict[str, Any], + keyvalues: Mapping[str, Any], + updatevalues: Mapping[str, Any], ) -> int: """ Update rows in the given database table. diff --git a/synapse/storage/databases/main/events.py b/synapse/storage/databases/main/events.py index 74d2a8b0c5..c59c9b463c 100644 --- a/synapse/storage/databases/main/events.py +++ b/synapse/storage/databases/main/events.py @@ -1708,16 +1708,18 @@ class PersistEventsStore: # instead because there is no `event_id` to use for the `NON NULL` # constraint on `event_stream_ordering`. elif to_delete: - args = list(sliding_sync_updates_values) + [room_id] - txn.execute( - f""" - UPDATE sliding_sync_joined_rooms - SET - {", ".join(f"{key} = ?" for key in sliding_sync_updates_keys)} - WHERE room_id = ? - """, - args, + num_rows_updated = self.db_pool.simple_update_txn( + txn, + table="sliding_sync_joined_rooms", + keyvalues={ + "room_id": room_id, + }, + updatevalues=sliding_sync_table_changes.joined_room_updates, ) + # TODO: Is this assumption correct? + assert ( + num_rows_updated > 0 + ), "Expected to only run this against existing rows" # We now update `local_current_membership`. We do this regardless # of whether we're still in the room or not to handle the case where