@@ -371,7 +371,7 @@ def _make_generic_sql_bound(
371371def _filter_results (
372372 lower_token : Optional [RoomStreamToken ],
373373 upper_token : Optional [RoomStreamToken ],
374- instance_name : str ,
374+ instance_name : Optional [ str ] ,
375375 topological_ordering : int ,
376376 stream_ordering : int ,
377377) -> bool :
@@ -384,8 +384,14 @@ def _filter_results(
384384 position maps, which we handle by fetching more than necessary from the DB
385385 and then filtering (rather than attempting to construct a complicated SQL
386386 query).
387+
388+ The `instance_name` arg is optional to handle historic rows, and is
389+ interpreted as if it was "master".
387390 """
388391
392+ if instance_name is None :
393+ instance_name = "master"
394+
389395 event_historical_tuple = (
390396 topological_ordering ,
391397 stream_ordering ,
@@ -420,7 +426,7 @@ def _filter_results(
420426def _filter_results_by_stream (
421427 lower_token : Optional [RoomStreamToken ],
422428 upper_token : Optional [RoomStreamToken ],
423- instance_name : str ,
429+ instance_name : Optional [ str ] ,
424430 stream_ordering : int ,
425431) -> bool :
426432 """
@@ -436,7 +442,14 @@ def _filter_results_by_stream(
436442 position maps, which we handle by fetching more than necessary from the DB
437443 and then filtering (rather than attempting to construct a complicated SQL
438444 query).
445+
446+ The `instance_name` arg is optional to handle historic rows, and is
447+ interpreted as if it was "master".
439448 """
449+
450+ if instance_name is None :
451+ instance_name = "master"
452+
440453 if lower_token :
441454 assert lower_token .topological is None
442455
@@ -912,7 +925,6 @@ def f(txn: LoggingTransaction) -> List[CurrentStateDeltaMembership]:
912925 prev_sender ,
913926 ) in txn :
914927 assert room_id is not None
915- assert instance_name is not None
916928 assert stream_ordering is not None
917929
918930 if _filter_results_by_stream (
@@ -936,7 +948,8 @@ def f(txn: LoggingTransaction) -> List[CurrentStateDeltaMembership]:
936948 # Event
937949 event_id = event_id ,
938950 event_pos = PersistedEventPosition (
939- instance_name = instance_name ,
951+ # If instance_name is null we default to "master"
952+ instance_name = instance_name or "master" ,
940953 stream = stream_ordering ,
941954 ),
942955 # When `s.event_id = null`, we won't be able to get respective
@@ -952,13 +965,11 @@ def f(txn: LoggingTransaction) -> List[CurrentStateDeltaMembership]:
952965 prev_event_id = prev_event_id ,
953966 prev_event_pos = (
954967 PersistedEventPosition (
955- instance_name = prev_instance_name ,
968+ # If instance_name is null we default to "master"
969+ instance_name = prev_instance_name or "master" ,
956970 stream = prev_stream_ordering ,
957971 )
958- if (
959- prev_instance_name is not None
960- and prev_stream_ordering is not None
961- )
972+ if (prev_stream_ordering is not None )
962973 else None
963974 ),
964975 prev_membership = prev_membership ,
@@ -1270,7 +1281,9 @@ def get_last_event_pos_in_room_before_stream_ordering_txn(
12701281 stream_ordering = stream_ordering ,
12711282 ):
12721283 return event_id , PersistedEventPosition (
1273- instance_name , stream_ordering
1284+ # If instance_name is null we default to "master"
1285+ instance_name or "master" ,
1286+ stream_ordering ,
12741287 )
12751288
12761289 return None
0 commit comments