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

Commit b71d4a0

Browse files
authored
Convert simple_delete to async/await. (#8191)
1 parent 9b7ac03 commit b71d4a0

File tree

5 files changed

+90
-37
lines changed

5 files changed

+90
-37
lines changed

changelog.d/8191.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert various parts of the codebase to async/await.

synapse/storage/database.py

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ async def execute(
614614
"""Runs a single query for a result set.
615615
616616
Args:
617+
desc: description of the transaction, for logging and metrics
617618
decoder - The function which can resolve the cursor results to
618619
something meaningful.
619620
query - The query string to execute
@@ -649,7 +650,7 @@ async def simple_insert(
649650
or_ignore: bool stating whether an exception should be raised
650651
when a conflicting row already exists. If True, False will be
651652
returned by the function instead
652-
desc: string giving a description of the transaction
653+
desc: description of the transaction, for logging and metrics
653654
654655
Returns:
655656
Whether the row was inserted or not. Only useful when `or_ignore` is True
@@ -686,7 +687,7 @@ async def simple_insert_many(
686687
Args:
687688
table: string giving the table name
688689
values: dict of new column names and values for them
689-
desc: string giving a description of the transaction
690+
desc: description of the transaction, for logging and metrics
690691
"""
691692
await self.runInteraction(desc, self.simple_insert_many_txn, table, values)
692693

@@ -700,7 +701,6 @@ def simple_insert_many_txn(
700701
txn: The transaction to use.
701702
table: string giving the table name
702703
values: dict of new column names and values for them
703-
desc: string giving a description of the transaction
704704
"""
705705
if not values:
706706
return
@@ -755,6 +755,7 @@ async def simple_upsert(
755755
keyvalues: The unique key columns and their new values
756756
values: The nonunique columns and their new values
757757
insertion_values: additional key/values to use only when inserting
758+
desc: description of the transaction, for logging and metrics
758759
lock: True to lock the table when doing the upsert.
759760
Returns:
760761
Native upserts always return None. Emulated upserts return True if a
@@ -1081,6 +1082,7 @@ async def simple_select_one(
10811082
retcols: list of strings giving the names of the columns to return
10821083
allow_none: If true, return None instead of failing if the SELECT
10831084
statement returns no rows
1085+
desc: description of the transaction, for logging and metrics
10841086
"""
10851087
return await self.runInteraction(
10861088
desc, self.simple_select_one_txn, table, keyvalues, retcols, allow_none
@@ -1166,6 +1168,7 @@ async def simple_select_onecol(
11661168
table: table name
11671169
keyvalues: column names and values to select the rows with
11681170
retcol: column whos value we wish to retrieve.
1171+
desc: description of the transaction, for logging and metrics
11691172
11701173
Returns:
11711174
Results in a list
@@ -1190,6 +1193,7 @@ async def simple_select_list(
11901193
column names and values to select the rows with, or None to not
11911194
apply a WHERE clause.
11921195
retcols: the names of the columns to return
1196+
desc: description of the transaction, for logging and metrics
11931197
11941198
Returns:
11951199
A list of dictionaries.
@@ -1243,14 +1247,16 @@ async def simple_select_many_batch(
12431247
"""Executes a SELECT query on the named table, which may return zero or
12441248
more rows, returning the result as a list of dicts.
12451249
1246-
Filters rows by if value of `column` is in `iterable`.
1250+
Filters rows by whether the value of `column` is in `iterable`.
12471251
12481252
Args:
12491253
table: string giving the table name
12501254
column: column name to test for inclusion against `iterable`
12511255
iterable: list
1252-
keyvalues: dict of column names and values to select the rows with
12531256
retcols: list of strings giving the names of the columns to return
1257+
keyvalues: dict of column names and values to select the rows with
1258+
desc: description of the transaction, for logging and metrics
1259+
batch_size: the number of rows for each select query
12541260
"""
12551261
results = [] # type: List[Dict[str, Any]]
12561262

@@ -1291,7 +1297,7 @@ def simple_select_many_txn(
12911297
"""Executes a SELECT query on the named table, which may return zero or
12921298
more rows, returning the result as a list of dicts.
12931299
1294-
Filters rows by if value of `column` is in `iterable`.
1300+
Filters rows by whether the value of `column` is in `iterable`.
12951301
12961302
Args:
12971303
txn: Transaction object
@@ -1367,6 +1373,7 @@ async def simple_update_one(
13671373
table: string giving the table name
13681374
keyvalues: dict of column names and values to select the row with
13691375
updatevalues: dict giving column names and values to update
1376+
desc: description of the transaction, for logging and metrics
13701377
"""
13711378
await self.runInteraction(
13721379
desc, self.simple_update_one_txn, table, keyvalues, updatevalues
@@ -1426,6 +1433,7 @@ async def simple_delete_one(
14261433
Args:
14271434
table: string giving the table name
14281435
keyvalues: dict of column names and values to select the row with
1436+
desc: description of the transaction, for logging and metrics
14291437
"""
14301438
await self.runInteraction(desc, self.simple_delete_one_txn, table, keyvalues)
14311439

@@ -1451,13 +1459,38 @@ def simple_delete_one_txn(
14511459
if txn.rowcount > 1:
14521460
raise StoreError(500, "More than one row matched (%s)" % (table,))
14531461

1454-
def simple_delete(self, table: str, keyvalues: Dict[str, Any], desc: str):
1455-
return self.runInteraction(desc, self.simple_delete_txn, table, keyvalues)
1462+
async def simple_delete(
1463+
self, table: str, keyvalues: Dict[str, Any], desc: str
1464+
) -> int:
1465+
"""Executes a DELETE query on the named table.
1466+
1467+
Filters rows by the key-value pairs.
1468+
1469+
Args:
1470+
table: string giving the table name
1471+
keyvalues: dict of column names and values to select the row with
1472+
desc: description of the transaction, for logging and metrics
1473+
1474+
Returns:
1475+
The number of deleted rows.
1476+
"""
1477+
return await self.runInteraction(desc, self.simple_delete_txn, table, keyvalues)
14561478

14571479
@staticmethod
14581480
def simple_delete_txn(
14591481
txn: LoggingTransaction, table: str, keyvalues: Dict[str, Any]
14601482
) -> int:
1483+
"""Executes a DELETE query on the named table.
1484+
1485+
Filters rows by the key-value pairs.
1486+
1487+
Args:
1488+
table: string giving the table name
1489+
keyvalues: dict of column names and values to select the row with
1490+
1491+
Returns:
1492+
The number of deleted rows.
1493+
"""
14611494
sql = "DELETE FROM %s WHERE %s" % (
14621495
table,
14631496
" AND ".join("%s = ?" % (k,) for k in keyvalues),
@@ -1474,6 +1507,20 @@ async def simple_delete_many(
14741507
keyvalues: Dict[str, Any],
14751508
desc: str,
14761509
) -> int:
1510+
"""Executes a DELETE query on the named table.
1511+
1512+
Filters rows by if value of `column` is in `iterable`.
1513+
1514+
Args:
1515+
table: string giving the table name
1516+
column: column name to test for inclusion against `iterable`
1517+
iterable: list
1518+
keyvalues: dict of column names and values to select the rows with
1519+
desc: description of the transaction, for logging and metrics
1520+
1521+
Returns:
1522+
Number rows deleted
1523+
"""
14771524
return await self.runInteraction(
14781525
desc, self.simple_delete_many_txn, table, column, iterable, keyvalues
14791526
)

synapse/storage/databases/main/group_server.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,13 @@ def _add_room_to_summary_txn(
728728
},
729729
)
730730

731-
def remove_room_from_summary(self, group_id, room_id, category_id):
731+
async def remove_room_from_summary(
732+
self, group_id: str, room_id: str, category_id: str
733+
) -> int:
732734
if category_id is None:
733735
category_id = _DEFAULT_CATEGORY_ID
734736

735-
return self.db_pool.simple_delete(
737+
return await self.db_pool.simple_delete(
736738
table="group_summary_rooms",
737739
keyvalues={
738740
"group_id": group_id,
@@ -772,8 +774,8 @@ async def upsert_group_category(
772774
desc="upsert_group_category",
773775
)
774776

775-
def remove_group_category(self, group_id, category_id):
776-
return self.db_pool.simple_delete(
777+
async def remove_group_category(self, group_id: str, category_id: str) -> int:
778+
return await self.db_pool.simple_delete(
777779
table="group_room_categories",
778780
keyvalues={"group_id": group_id, "category_id": category_id},
779781
desc="remove_group_category",
@@ -809,8 +811,8 @@ async def upsert_group_role(
809811
desc="upsert_group_role",
810812
)
811813

812-
def remove_group_role(self, group_id, role_id):
813-
return self.db_pool.simple_delete(
814+
async def remove_group_role(self, group_id: str, role_id: str) -> int:
815+
return await self.db_pool.simple_delete(
814816
table="group_roles",
815817
keyvalues={"group_id": group_id, "role_id": role_id},
816818
desc="remove_group_role",
@@ -940,11 +942,13 @@ def _add_user_to_summary_txn(
940942
},
941943
)
942944

943-
def remove_user_from_summary(self, group_id, user_id, role_id):
945+
async def remove_user_from_summary(
946+
self, group_id: str, user_id: str, role_id: str
947+
) -> int:
944948
if role_id is None:
945949
role_id = _DEFAULT_ROLE_ID
946950

947-
return self.db_pool.simple_delete(
951+
return await self.db_pool.simple_delete(
948952
table="group_summary_users",
949953
keyvalues={"group_id": group_id, "role_id": role_id, "user_id": user_id},
950954
desc="remove_user_from_summary",
@@ -1264,16 +1268,16 @@ async def update_remote_attestion(
12641268
desc="update_remote_attestion",
12651269
)
12661270

1267-
def remove_attestation_renewal(self, group_id, user_id):
1271+
async def remove_attestation_renewal(self, group_id: str, user_id: str) -> int:
12681272
"""Remove an attestation that we thought we should renew, but actually
12691273
shouldn't. Ideally this would never get called as we would never
12701274
incorrectly try and do attestations for local users on local groups.
12711275
12721276
Args:
1273-
group_id (str)
1274-
user_id (str)
1277+
group_id
1278+
user_id
12751279
"""
1276-
return self.db_pool.simple_delete(
1280+
return await self.db_pool.simple_delete(
12771281
table="group_attestations_renewals",
12781282
keyvalues={"group_id": group_id, "user_id": user_id},
12791283
desc="remove_attestation_renewal",

synapse/storage/databases/main/registration.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -529,21 +529,21 @@ async def user_get_threepids(self, user_id):
529529
"user_get_threepids",
530530
)
531531

532-
def user_delete_threepid(self, user_id, medium, address):
533-
return self.db_pool.simple_delete(
532+
async def user_delete_threepid(self, user_id, medium, address) -> None:
533+
await self.db_pool.simple_delete(
534534
"user_threepids",
535535
keyvalues={"user_id": user_id, "medium": medium, "address": address},
536536
desc="user_delete_threepid",
537537
)
538538

539-
def user_delete_threepids(self, user_id: str):
539+
async def user_delete_threepids(self, user_id: str) -> None:
540540
"""Delete all threepid this user has bound
541541
542542
Args:
543543
user_id: The user id to delete all threepids of
544544
545545
"""
546-
return self.db_pool.simple_delete(
546+
await self.db_pool.simple_delete(
547547
"user_threepids",
548548
keyvalues={"user_id": user_id},
549549
desc="user_delete_threepids",
@@ -597,21 +597,20 @@ async def user_get_bound_threepids(self, user_id: str) -> List[Dict[str, Any]]:
597597
desc="user_get_bound_threepids",
598598
)
599599

600-
def remove_user_bound_threepid(self, user_id, medium, address, id_server):
600+
async def remove_user_bound_threepid(
601+
self, user_id: str, medium: str, address: str, id_server: str
602+
) -> None:
601603
"""The server proxied an unbind request to the given identity server on
602604
behalf of the given user, so we remove the mapping of threepid to
603605
identity server.
604606
605607
Args:
606-
user_id (str)
607-
medium (str)
608-
address (str)
609-
id_server (str)
610-
611-
Returns:
612-
Deferred
608+
user_id
609+
medium
610+
address
611+
id_server
613612
"""
614-
return self.db_pool.simple_delete(
613+
await self.db_pool.simple_delete(
615614
table="user_threepid_id_server",
616615
keyvalues={
617616
"user_id": user_id,
@@ -1247,14 +1246,14 @@ async def add_user_pending_deactivation(self, user_id: str) -> None:
12471246
desc="add_user_pending_deactivation",
12481247
)
12491248

1250-
def del_user_pending_deactivation(self, user_id):
1249+
async def del_user_pending_deactivation(self, user_id: str) -> None:
12511250
"""
12521251
Removes the given user to the table of users who need to be parted from all the
12531252
rooms they're in, effectively marking that user as fully deactivated.
12541253
"""
12551254
# XXX: This should be simple_delete_one but we failed to put a unique index on
12561255
# the table, so somehow duplicate entries have ended up in it.
1257-
return self.db_pool.simple_delete(
1256+
await self.db_pool.simple_delete(
12581257
"users_pending_deactivation",
12591258
keyvalues={"user_id": user_id},
12601259
desc="del_user_pending_deactivation",

tests/storage/test_event_push_actions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,10 @@ def _mark_read(stream, depth):
123123
yield _inject_actions(6, PlAIN_NOTIF)
124124
yield _rotate(7)
125125

126-
yield self.store.db_pool.simple_delete(
127-
table="event_push_actions", keyvalues={"1": 1}, desc=""
126+
yield defer.ensureDeferred(
127+
self.store.db_pool.simple_delete(
128+
table="event_push_actions", keyvalues={"1": 1}, desc=""
129+
)
128130
)
129131

130132
yield _assert_counts(1, 0)

0 commit comments

Comments
 (0)