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

Commit 58f8305

Browse files
authored
Inline SQL queries using boolean parameters (#15525)
SQLite now supports TRUE and FALSE constants, simplify some queries by inlining those instead of passing them as arguments.
1 parent 96529c4 commit 58f8305

File tree

8 files changed

+24
-25
lines changed

8 files changed

+24
-25
lines changed

changelog.d/15525.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update SQL queries to inline boolean parameters as supported in SQLite 3.27.

synapse/storage/databases/main/event_federation.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ def get_backfill_points_in_room_txn(
843843
* because the schema change is in a background update, it's not
844844
* necessarily safe to assume that it will have been completed.
845845
*/
846-
AND edge.is_state is ? /* False */
846+
AND edge.is_state is FALSE
847847
/**
848848
* We only want backwards extremities that are older than or at
849849
* the same position of the given `current_depth` (where older
@@ -886,7 +886,6 @@ def get_backfill_points_in_room_txn(
886886
sql,
887887
(
888888
room_id,
889-
False,
890889
current_depth,
891890
self._clock.time_msec(),
892891
BACKFILL_EVENT_EXPONENTIAL_BACKOFF_MAXIMUM_DOUBLING_STEPS,

synapse/storage/databases/main/events.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1455,8 +1455,8 @@ def _update_outliers_txn(
14551455
},
14561456
)
14571457

1458-
sql = "UPDATE events SET outlier = ? WHERE event_id = ?"
1459-
txn.execute(sql, (False, event.event_id))
1458+
sql = "UPDATE events SET outlier = FALSE WHERE event_id = ?"
1459+
txn.execute(sql, (event.event_id,))
14601460

14611461
# Update the event_backward_extremities table now that this
14621462
# event isn't an outlier any more.
@@ -1549,13 +1549,13 @@ def event_dict(event: EventBase) -> JsonDict:
15491549
for event, _ in events_and_contexts
15501550
if not event.internal_metadata.is_redacted()
15511551
]
1552-
sql = "UPDATE redactions SET have_censored = ? WHERE "
1552+
sql = "UPDATE redactions SET have_censored = FALSE WHERE "
15531553
clause, args = make_in_list_sql_clause(
15541554
self.database_engine,
15551555
"redacts",
15561556
unredacted_events,
15571557
)
1558-
txn.execute(sql + clause, [False] + args)
1558+
txn.execute(sql + clause, args)
15591559

15601560
self.db_pool.simple_insert_many_txn(
15611561
txn,
@@ -2318,14 +2318,14 @@ def _update_backward_extremeties(
23182318
" SELECT 1 FROM events"
23192319
" LEFT JOIN event_edges edge"
23202320
" ON edge.event_id = events.event_id"
2321-
" WHERE events.event_id = ? AND events.room_id = ? AND (events.outlier = ? OR edge.event_id IS NULL)"
2321+
" WHERE events.event_id = ? AND events.room_id = ? AND (events.outlier = FALSE OR edge.event_id IS NULL)"
23222322
" )"
23232323
)
23242324

23252325
txn.execute_batch(
23262326
query,
23272327
[
2328-
(e_id, ev.room_id, e_id, ev.room_id, e_id, ev.room_id, False)
2328+
(e_id, ev.room_id, e_id, ev.room_id, e_id, ev.room_id)
23292329
for ev in events
23302330
for e_id in ev.prev_event_ids()
23312331
if not ev.internal_metadata.is_outlier()

synapse/storage/databases/main/purge_events.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,11 @@ def _purge_history_txn(
249249
# Mark all state and own events as outliers
250250
logger.info("[purge] marking remaining events as outliers")
251251
txn.execute(
252-
"UPDATE events SET outlier = ?"
252+
"UPDATE events SET outlier = TRUE"
253253
" WHERE event_id IN ("
254-
" SELECT event_id FROM events_to_purge "
255-
" WHERE NOT should_delete"
256-
")",
257-
(True,),
254+
" SELECT event_id FROM events_to_purge "
255+
" WHERE NOT should_delete"
256+
")"
258257
)
259258

260259
# synapse tries to take out an exclusive lock on room_depth whenever it

synapse/storage/databases/main/push_rule.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -560,19 +560,19 @@ def _upsert_push_rule_txn(
560560
if isinstance(self.database_engine, PostgresEngine):
561561
sql = """
562562
INSERT INTO push_rules_enable (id, user_name, rule_id, enabled)
563-
VALUES (?, ?, ?, ?)
563+
VALUES (?, ?, ?, 1)
564564
ON CONFLICT DO NOTHING
565565
"""
566566
elif isinstance(self.database_engine, Sqlite3Engine):
567567
sql = """
568568
INSERT OR IGNORE INTO push_rules_enable (id, user_name, rule_id, enabled)
569-
VALUES (?, ?, ?, ?)
569+
VALUES (?, ?, ?, 1)
570570
"""
571571
else:
572572
raise RuntimeError("Unknown database engine")
573573

574574
new_enable_id = self._push_rules_enable_id_gen.get_next()
575-
txn.execute(sql, (new_enable_id, user_id, rule_id, 1))
575+
txn.execute(sql, (new_enable_id, user_id, rule_id))
576576

577577
async def delete_push_rule(self, user_id: str, rule_id: str) -> None:
578578
"""

synapse/storage/databases/main/registration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ def select_users_txn(
454454
) -> List[Tuple[str, int]]:
455455
sql = (
456456
"SELECT user_id, expiration_ts_ms FROM account_validity"
457-
" WHERE email_sent = ? AND (expiration_ts_ms - ?) <= ?"
457+
" WHERE email_sent = FALSE AND (expiration_ts_ms - ?) <= ?"
458458
)
459-
values = [False, now_ms, renew_at]
459+
values = [now_ms, renew_at]
460460
txn.execute(sql, values)
461461
return cast(List[Tuple[str, int]], txn.fetchall())
462462

synapse/storage/databases/main/room.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -936,11 +936,11 @@ def _get_media_mxcs_in_room_txn(
936936
JOIN event_json USING (room_id, event_id)
937937
WHERE room_id = ?
938938
%(where_clause)s
939-
AND contains_url = ? AND outlier = ?
939+
AND contains_url = TRUE AND outlier = FALSE
940940
ORDER BY stream_ordering DESC
941941
LIMIT ?
942942
"""
943-
txn.execute(sql % {"where_clause": ""}, (room_id, True, False, 100))
943+
txn.execute(sql % {"where_clause": ""}, (room_id, 100))
944944

945945
local_media_mxcs = []
946946
remote_media_mxcs = []
@@ -976,7 +976,7 @@ def _get_media_mxcs_in_room_txn(
976976

977977
txn.execute(
978978
sql % {"where_clause": "AND stream_ordering < ?"},
979-
(room_id, next_token, True, False, 100),
979+
(room_id, next_token, 100),
980980
)
981981

982982
return local_media_mxcs, remote_media_mxcs
@@ -1086,9 +1086,9 @@ def _quarantine_media_txn(
10861086

10871087
# set quarantine
10881088
if quarantined_by is not None:
1089-
sql += "AND safe_from_quarantine = ?"
1089+
sql += "AND safe_from_quarantine = FALSE"
10901090
txn.executemany(
1091-
sql, [(quarantined_by, media_id, False) for media_id in local_mxcs]
1091+
sql, [(quarantined_by, media_id) for media_id in local_mxcs]
10921092
)
10931093
# remove from quarantine
10941094
else:

synapse/storage/databases/main/stream.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ def _paginate_room_events_txn(
14011401
`to_token`), or `limit` is zero.
14021402
"""
14031403

1404-
args = [False, room_id]
1404+
args: List[Any] = [room_id]
14051405

14061406
order, from_bound, to_bound = generate_pagination_bounds(
14071407
direction, from_token, to_token
@@ -1475,7 +1475,7 @@ def _paginate_room_events_txn(
14751475
event.topological_ordering, event.stream_ordering
14761476
FROM events AS event
14771477
%(join_clause)s
1478-
WHERE event.outlier = ? AND event.room_id = ? AND %(bounds)s
1478+
WHERE event.outlier = FALSE AND event.room_id = ? AND %(bounds)s
14791479
ORDER BY event.topological_ordering %(order)s,
14801480
event.stream_ordering %(order)s LIMIT ?
14811481
""" % {

0 commit comments

Comments
 (0)