Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 5640992

Browse files
authored
Disambiguate queries on state_key (#11497)
We're going to add a `state_key` column to the `events` table, so we need to add some disambiguation to queries which use it.
1 parent d26808d commit 5640992

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

changelog.d/11497.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Preparation for database schema simplifications: disambiguate queries on `state_key`.

synapse/storage/databases/main/event_federation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1552,9 +1552,9 @@ def delete_event_auth(txn):
15521552
DELETE FROM event_auth
15531553
WHERE event_id IN (
15541554
SELECT event_id FROM events
1555-
LEFT JOIN state_events USING (room_id, event_id)
1555+
LEFT JOIN state_events AS se USING (room_id, event_id)
15561556
WHERE ? <= stream_ordering AND stream_ordering < ?
1557-
AND state_key IS null
1557+
AND se.state_key IS null
15581558
)
15591559
"""
15601560

synapse/storage/databases/main/events.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,9 @@ def _add_chain_cover_index(
575575
# fetch their auth event info.
576576
while missing_auth_chains:
577577
sql = """
578-
SELECT event_id, events.type, state_key, chain_id, sequence_number
578+
SELECT event_id, events.type, se.state_key, chain_id, sequence_number
579579
FROM events
580-
INNER JOIN state_events USING (event_id)
580+
INNER JOIN state_events AS se USING (event_id)
581581
LEFT JOIN event_auth_chains USING (event_id)
582582
WHERE
583583
"""

synapse/storage/databases/main/events_worker.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1408,10 +1408,10 @@ def get_all_new_forward_event_rows(
14081408
) -> List[Tuple[int, str, str, str, str, str, str, str, str]]:
14091409
sql = (
14101410
"SELECT e.stream_ordering, e.event_id, e.room_id, e.type,"
1411-
" state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL"
1411+
" se.state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL"
14121412
" FROM events AS e"
14131413
" LEFT JOIN redactions USING (event_id)"
1414-
" LEFT JOIN state_events USING (event_id)"
1414+
" LEFT JOIN state_events AS se USING (event_id)"
14151415
" LEFT JOIN event_relations USING (event_id)"
14161416
" LEFT JOIN room_memberships USING (event_id)"
14171417
" LEFT JOIN rejections USING (event_id)"
@@ -1449,11 +1449,11 @@ def get_ex_outlier_stream_rows_txn(
14491449
) -> List[Tuple[int, str, str, str, str, str, str, str, str]]:
14501450
sql = (
14511451
"SELECT event_stream_ordering, e.event_id, e.room_id, e.type,"
1452-
" state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL"
1452+
" se.state_key, redacts, relates_to_id, membership, rejections.reason IS NOT NULL"
14531453
" FROM events AS e"
14541454
" INNER JOIN ex_outlier_stream AS out USING (event_id)"
14551455
" LEFT JOIN redactions USING (event_id)"
1456-
" LEFT JOIN state_events USING (event_id)"
1456+
" LEFT JOIN state_events AS se USING (event_id)"
14571457
" LEFT JOIN event_relations USING (event_id)"
14581458
" LEFT JOIN room_memberships USING (event_id)"
14591459
" LEFT JOIN rejections USING (event_id)"
@@ -1507,10 +1507,10 @@ def get_all_new_backfill_event_rows(
15071507
) -> Tuple[List[Tuple[int, Tuple[str, str, str, str, str, str]]], int, bool]:
15081508
sql = (
15091509
"SELECT -e.stream_ordering, e.event_id, e.room_id, e.type,"
1510-
" state_key, redacts, relates_to_id"
1510+
" se.state_key, redacts, relates_to_id"
15111511
" FROM events AS e"
15121512
" LEFT JOIN redactions USING (event_id)"
1513-
" LEFT JOIN state_events USING (event_id)"
1513+
" LEFT JOIN state_events AS se USING (event_id)"
15141514
" LEFT JOIN event_relations USING (event_id)"
15151515
" WHERE ? > stream_ordering AND stream_ordering >= ?"
15161516
" AND instance_name = ?"
@@ -1537,11 +1537,11 @@ def get_all_new_backfill_event_rows(
15371537

15381538
sql = (
15391539
"SELECT -event_stream_ordering, e.event_id, e.room_id, e.type,"
1540-
" state_key, redacts, relates_to_id"
1540+
" se.state_key, redacts, relates_to_id"
15411541
" FROM events AS e"
15421542
" INNER JOIN ex_outlier_stream AS out USING (event_id)"
15431543
" LEFT JOIN redactions USING (event_id)"
1544-
" LEFT JOIN state_events USING (event_id)"
1544+
" LEFT JOIN state_events AS se USING (event_id)"
15451545
" LEFT JOIN event_relations USING (event_id)"
15461546
" WHERE ? > event_stream_ordering"
15471547
" AND event_stream_ordering >= ?"

synapse/storage/databases/main/purge_events.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _purge_history_txn(
118118

119119
logger.info("[purge] looking for events to delete")
120120

121-
should_delete_expr = "state_key IS NULL"
121+
should_delete_expr = "state_events.state_key IS NULL"
122122
should_delete_params: Tuple[Any, ...] = ()
123123
if not delete_local_events:
124124
should_delete_expr += " AND event_id NOT LIKE ?"

synapse/storage/databases/main/roommember.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ def _get_rooms_for_user_with_stream_ordering_txn(
476476
INNER JOIN events AS e USING (room_id, event_id)
477477
WHERE
478478
c.type = 'm.room.member'
479-
AND state_key = ?
479+
AND c.state_key = ?
480480
AND c.membership = ?
481481
"""
482482
else:
@@ -487,7 +487,7 @@ def _get_rooms_for_user_with_stream_ordering_txn(
487487
INNER JOIN events AS e USING (room_id, event_id)
488488
WHERE
489489
c.type = 'm.room.member'
490-
AND state_key = ?
490+
AND c.state_key = ?
491491
AND m.membership = ?
492492
"""
493493

synapse/storage/schema/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
SCHEMA_VERSION = 65 # remember to update the list below when updating
15+
SCHEMA_VERSION = 66 # remember to update the list below when updating
1616
"""Represents the expectations made by the codebase about the database schema
1717
1818
This should be incremented whenever the codebase changes its requirements on the
@@ -46,6 +46,10 @@
4646
- MSC2716: Remove unique event_id constraint from insertion_event_edges
4747
because an insertion event can have multiple edges.
4848
- Remove unused tables `user_stats_historical` and `room_stats_historical`.
49+
50+
Changes in SCHEMA_VERSION = 66:
51+
- Queries on state_key columns are now disambiguated (ie, the codebase can handle
52+
the `events` table having a `state_key` column).
4953
"""
5054

5155

0 commit comments

Comments
 (0)