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

Commit 5babec2

Browse files
author
David Robertson
committed
Add back the foreign key constraint
1 parent 0f419d1 commit 5babec2

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

synapse/handlers/federation.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,9 @@ async def do_invite_join(
584584
# The background process is responsible for unmarking this flag,
585585
# even if the join fails.
586586
await self.store.store_partial_state_room(
587-
room_id,
588-
ret.servers_in_room,
589-
ret.event.event_id,
590-
self.store.get_device_stream_token(),
587+
room_id=room_id,
588+
servers=ret.servers_in_room,
589+
device_lists_stream_id=self.store.get_device_stream_token(),
591590
)
592591

593592
try:
@@ -613,6 +612,14 @@ async def do_invite_join(
613612
room_id,
614613
)
615614
raise LimitExceededError(msg=e.msg, errcode=e.errcode, retry_after_ms=0)
615+
else:
616+
# Record the join event id for future use (when we finish the full
617+
# join). We have to do this after persisting the event to keep foreign
618+
# key constraints intact.
619+
if ret.partial_state:
620+
await self.store.write_partial_state_rooms_join_event_id(
621+
room_id, event.event_id
622+
)
616623
finally:
617624
# Always kick off the background process that asynchronously fetches
618625
# state for the room.

synapse/storage/databases/main/room.py

+35-6
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,6 @@ async def store_partial_state_room(
17771777
self,
17781778
room_id: str,
17791779
servers: Collection[str],
1780-
join_event_id: str,
17811780
device_lists_stream_id: int,
17821781
) -> None:
17831782
"""Mark the given room as containing events with partial state.
@@ -1786,11 +1785,12 @@ async def store_partial_state_room(
17861785
room, which helps us to keep other homeservers in sync when we finally fully
17871786
join this room.
17881787
1788+
We do not include a `join_event_id` here---we need to wait for the join event
1789+
to be persisted first.
1790+
17891791
Args:
17901792
room_id: the ID of the room
17911793
servers: other servers known to be in the room
1792-
join_event_id: the event ID of the join membership event returned in the
1793-
(partial) /send_join response.
17941794
device_lists_stream_id: the device_lists stream ID at the time when we first
17951795
joined the room.
17961796
"""
@@ -1799,7 +1799,6 @@ async def store_partial_state_room(
17991799
self._store_partial_state_room_txn,
18001800
room_id,
18011801
servers,
1802-
join_event_id,
18031802
device_lists_stream_id,
18041803
)
18051804

@@ -1808,7 +1807,6 @@ def _store_partial_state_room_txn(
18081807
txn: LoggingTransaction,
18091808
room_id: str,
18101809
servers: Collection[str],
1811-
join_event_id: str,
18121810
device_lists_stream_id: int,
18131811
) -> None:
18141812
DatabasePool.simple_insert_txn(
@@ -1817,7 +1815,8 @@ def _store_partial_state_room_txn(
18171815
values={
18181816
"room_id": room_id,
18191817
"device_lists_stream_id": device_lists_stream_id,
1820-
"join_event_id": join_event_id,
1818+
# To be updated later once the join event is persisted.
1819+
"join_event_id": None,
18211820
},
18221821
)
18231822
DatabasePool.simple_insert_many_txn(
@@ -1828,6 +1827,36 @@ def _store_partial_state_room_txn(
18281827
)
18291828
self._invalidate_cache_and_stream(txn, self.is_partial_state_room, (room_id,))
18301829

1830+
async def write_partial_state_rooms_join_event_id(
1831+
self,
1832+
room_id: str,
1833+
join_event_id: str,
1834+
) -> None:
1835+
"""Record the join event which resulted from a partial join.
1836+
1837+
We do this separately to `store_partial_state_room` because we need to wait for
1838+
the join event to be persisted. Otherwise we violate a foreign key constraint.
1839+
"""
1840+
await self.db_pool.runInteraction(
1841+
"write_partial_state_rooms_join_event_id",
1842+
self._store_partial_state_room_txn,
1843+
room_id,
1844+
join_event_id,
1845+
)
1846+
1847+
async def _write_partial_state_rooms_join_event_id(
1848+
self,
1849+
txn: LoggingTransaction,
1850+
room_id: str,
1851+
join_event_id: str,
1852+
) -> None:
1853+
DatabasePool.simple_update_txn(
1854+
txn,
1855+
table="partial_state_rooms",
1856+
keyvalues={"room_id": room_id},
1857+
updatevalues={"join_event_id": join_event_id}
1858+
)
1859+
18311860
async def maybe_store_room_on_outlier_membership(
18321861
self, room_id: str, room_version: RoomVersion
18331862
) -> None:

synapse/storage/schema/main/delta/73/04partial_join_details.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
--
2121
-- Both columns are backwards compatible.
2222
ALTER TABLE partial_state_rooms ADD COLUMN device_lists_stream_id BIGINT NOT NULL DEFAULT 0;
23-
ALTER TABLE partial_state_rooms ADD COLUMN join_event_id TEXT;
23+
ALTER TABLE partial_state_rooms ADD COLUMN join_event_id TEXT REFERENCES events(event_id);

0 commit comments

Comments
 (0)