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

delete messages from device_inbox table when deleting device #10098

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10098.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where messages in `device_inbox` table where not deleted, when deleting device(s).
15 changes: 15 additions & 0 deletions synapse/storage/databases/main/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,12 @@ async def delete_device(self, user_id: str, device_id: str) -> None:
desc="delete_device",
)

await self.db_pool.simple_delete_one(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await self.db_pool.simple_delete_one(
await self.db_pool.simple_delete_many(

Since the table may have many rows (or no rows)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed it

table="device_inbox",
keyvalues={"user_id": user_id, "device_id": device_id},
desc="delete_device",
)

self.device_id_exists_cache.invalidate((user_id, device_id))

async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
Expand All @@ -1146,6 +1152,15 @@ async def delete_devices(self, user_id: str, device_ids: List[str]) -> None:
keyvalues={"user_id": user_id, "hidden": False},
desc="delete_devices",
)

await self.db_pool.simple_delete_many(
table="device_inbox",
column="device_id",
iterable=device_ids,
keyvalues={"user_id": user_id},
desc="delete_devices",
)

for device_id in device_ids:
self.device_id_exists_cache.invalidate((user_id, device_id))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Copyright 2021 The Matrix.org Foundation C.I.C
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

--- Remove messages from the device_inbox table which where sent to an
--- allready deleted device.
--- This schould run as the last task, it may take a little bit longer
--- to finish.

DELETE FROM device_inbox WHERE device_id NOT IN (SELECT device_id FROM devices);