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

Convert simple_delete to async/await. #8191

Merged
merged 3 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/8191.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various parts of the codebase to async/await.
42 changes: 40 additions & 2 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,13 +1451,37 @@ def simple_delete_one_txn(
if txn.rowcount > 1:
raise StoreError(500, "More than one row matched (%s)" % (table,))

def simple_delete(self, table: str, keyvalues: Dict[str, Any], desc: str):
return self.runInteraction(desc, self.simple_delete_txn, table, keyvalues)
async def simple_delete(
self, table: str, keyvalues: Dict[str, Any], desc: str
) -> int:
"""Executes a DELETE query on the named table.

Filters rows by the key-value pairs.

Args:
table: string giving the table name
keyvalues: dict of column names and values to select the row with
clokep marked this conversation as resolved.
Show resolved Hide resolved

Returns:
The number of deleted rows.
"""
return await self.runInteraction(desc, self.simple_delete_txn, table, keyvalues)

@staticmethod
def simple_delete_txn(
txn: LoggingTransaction, table: str, keyvalues: Dict[str, Any]
) -> int:
"""Executes a DELETE query on the named table.

Filters rows by the key-value pairs.

Args:
table: string giving the table name
keyvalues: dict of column names and values to select the row with

Returns:
The number of deleted rows.
"""
sql = "DELETE FROM %s WHERE %s" % (
table,
" AND ".join("%s = ?" % (k,) for k in keyvalues),
Expand All @@ -1474,6 +1498,20 @@ async def simple_delete_many(
keyvalues: Dict[str, Any],
desc: str,
) -> int:
"""Executes a DELETE query on the named table.

Filters rows by if value of `column` is in `iterable`.
clokep marked this conversation as resolved.
Show resolved Hide resolved

Args:
txn: Transaction object
table: string giving the table name
column: column name to test for inclusion against `iterable`
iterable: list
keyvalues: dict of column names and values to select the rows with
clokep marked this conversation as resolved.
Show resolved Hide resolved

Returns:
Number rows deleted
"""
return await self.runInteraction(
desc, self.simple_delete_many_txn, table, column, iterable, keyvalues
)
Expand Down
28 changes: 16 additions & 12 deletions synapse/storage/databases/main/group_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,13 @@ def _add_room_to_summary_txn(
},
)

def remove_room_from_summary(self, group_id, room_id, category_id):
async def remove_room_from_summary(
self, group_id: str, room_id: str, category_id: str
) -> int:
if category_id is None:
category_id = _DEFAULT_CATEGORY_ID

return self.db_pool.simple_delete(
return await self.db_pool.simple_delete(
table="group_summary_rooms",
keyvalues={
"group_id": group_id,
Expand Down Expand Up @@ -766,8 +768,8 @@ def upsert_group_category(self, group_id, category_id, profile, is_public):
desc="upsert_group_category",
)

def remove_group_category(self, group_id, category_id):
return self.db_pool.simple_delete(
async def remove_group_category(self, group_id: str, category_id: str) -> int:
return await self.db_pool.simple_delete(
table="group_room_categories",
keyvalues={"group_id": group_id, "category_id": category_id},
desc="remove_group_category",
Expand Down Expand Up @@ -797,8 +799,8 @@ def upsert_group_role(self, group_id, role_id, profile, is_public):
desc="upsert_group_role",
)

def remove_group_role(self, group_id, role_id):
return self.db_pool.simple_delete(
async def remove_group_role(self, group_id: str, role_id: str) -> int:
return await self.db_pool.simple_delete(
table="group_roles",
keyvalues={"group_id": group_id, "role_id": role_id},
desc="remove_group_role",
Expand Down Expand Up @@ -928,11 +930,13 @@ def _add_user_to_summary_txn(
},
)

def remove_user_from_summary(self, group_id, user_id, role_id):
async def remove_user_from_summary(
self, group_id: str, user_id: str, role_id: str
) -> int:
if role_id is None:
role_id = _DEFAULT_ROLE_ID

return self.db_pool.simple_delete(
return await self.db_pool.simple_delete(
table="group_summary_users",
keyvalues={"group_id": group_id, "role_id": role_id, "user_id": user_id},
desc="remove_user_from_summary",
Expand Down Expand Up @@ -1250,16 +1254,16 @@ async def update_remote_attestion(
desc="update_remote_attestion",
)

def remove_attestation_renewal(self, group_id, user_id):
async def remove_attestation_renewal(self, group_id: str, user_id: str) -> int:
"""Remove an attestation that we thought we should renew, but actually
shouldn't. Ideally this would never get called as we would never
incorrectly try and do attestations for local users on local groups.

Args:
group_id (str)
user_id (str)
group_id
user_id
"""
return self.db_pool.simple_delete(
return await self.db_pool.simple_delete(
table="group_attestations_renewals",
keyvalues={"group_id": group_id, "user_id": user_id},
desc="remove_attestation_renewal",
Expand Down
29 changes: 14 additions & 15 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,21 +529,21 @@ async def user_get_threepids(self, user_id):
"user_get_threepids",
)

def user_delete_threepid(self, user_id, medium, address):
return self.db_pool.simple_delete(
async def user_delete_threepid(self, user_id, medium, address) -> None:
await self.db_pool.simple_delete(
"user_threepids",
keyvalues={"user_id": user_id, "medium": medium, "address": address},
desc="user_delete_threepid",
)

def user_delete_threepids(self, user_id: str):
async def user_delete_threepids(self, user_id: str) -> None:
"""Delete all threepid this user has bound

Args:
user_id: The user id to delete all threepids of

"""
return self.db_pool.simple_delete(
await self.db_pool.simple_delete(
"user_threepids",
keyvalues={"user_id": user_id},
desc="user_delete_threepids",
Expand Down Expand Up @@ -598,21 +598,20 @@ async def user_get_bound_threepids(self, user_id: str) -> List[Dict[str, Any]]:
desc="user_get_bound_threepids",
)

def remove_user_bound_threepid(self, user_id, medium, address, id_server):
async def remove_user_bound_threepid(
self, user_id: str, medium: str, address: str, id_server: str
) -> None:
"""The server proxied an unbind request to the given identity server on
behalf of the given user, so we remove the mapping of threepid to
identity server.

Args:
user_id (str)
medium (str)
address (str)
id_server (str)

Returns:
Deferred
user_id
medium
address
id_server
"""
return self.db_pool.simple_delete(
await self.db_pool.simple_delete(
table="user_threepid_id_server",
keyvalues={
"user_id": user_id,
Expand Down Expand Up @@ -1248,14 +1247,14 @@ def add_user_pending_deactivation(self, user_id):
desc="add_user_pending_deactivation",
)

def del_user_pending_deactivation(self, user_id):
async def del_user_pending_deactivation(self, user_id: str) -> None:
"""
Removes the given user to the table of users who need to be parted from all the
rooms they're in, effectively marking that user as fully deactivated.
"""
# XXX: This should be simple_delete_one but we failed to put a unique index on
# the table, so somehow duplicate entries have ended up in it.
return self.db_pool.simple_delete(
await self.db_pool.simple_delete(
"users_pending_deactivation",
keyvalues={"user_id": user_id},
desc="del_user_pending_deactivation",
Expand Down
6 changes: 4 additions & 2 deletions tests/storage/test_event_push_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ def _mark_read(stream, depth):
yield _inject_actions(6, PlAIN_NOTIF)
yield _rotate(7)

yield self.store.db_pool.simple_delete(
table="event_push_actions", keyvalues={"1": 1}, desc=""
yield defer.ensureDeferred(
self.store.db_pool.simple_delete(
table="event_push_actions", keyvalues={"1": 1}, desc=""
)
)

yield _assert_counts(1, 0)
Expand Down