From 5ea26508e072f684b70fce02529847499822830d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 7 Nov 2023 09:20:56 -0500 Subject: [PATCH 1/2] Avoid updating the same rows multiple times. --- synapse/storage/database.py | 5 +---- tests/storage/test_base.py | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/synapse/storage/database.py b/synapse/storage/database.py index abc7d8a5d268..302f82342202 100644 --- a/synapse/storage/database.py +++ b/synapse/storage/database.py @@ -2047,10 +2047,7 @@ def simple_update_many_txn( # List of tuples of (value values, then key values) # (This matches the order needed for the query) - args = [tuple(x) + tuple(y) for x, y in zip(value_values, key_values)] - - for ks, vs in zip(key_values, value_values): - args.append(tuple(vs) + tuple(ks)) + args = [tuple(vv) + tuple(kv) for vv, kv in zip(value_values, key_values)] # 'col1 = ?, col2 = ?, ...' set_clause = ", ".join(f"{n} = ?" for n in value_names) diff --git a/tests/storage/test_base.py b/tests/storage/test_base.py index b4c490b568f2..5bae1b67bfd5 100644 --- a/tests/storage/test_base.py +++ b/tests/storage/test_base.py @@ -371,12 +371,12 @@ def test_update_many(self) -> Generator["defer.Deferred[object]", object, None]: self.mock_execute_batch.assert_called_once_with( self.mock_txn, "UPDATE tablename SET col3 = ? WHERE col1 = ? AND col2 = ?", - [("val3", "val1", "val2"), ("val3", "val1", "val2")], + [("val3", "val1", "val2")], ) else: self.mock_txn.executemany.assert_called_once_with( "UPDATE tablename SET col3 = ? WHERE col1 = ? AND col2 = ?", - [("val3", "val1", "val2"), ("val3", "val1", "val2")], + [("val3", "val1", "val2")], ) # key_values and value_values must be the same length. From 5dd32b24060804cb4dafb41b9425a9e55e70675d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Tue, 7 Nov 2023 12:55:47 -0500 Subject: [PATCH 2/2] Newsfragment --- changelog.d/16609.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/16609.bugfix diff --git a/changelog.d/16609.bugfix b/changelog.d/16609.bugfix new file mode 100644 index 000000000000..a52d395cd332 --- /dev/null +++ b/changelog.d/16609.bugfix @@ -0,0 +1 @@ +Fix a long-standing bug where some queries updated the same row twice. Introduced in Synapse 1.57.0.