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

Commit

Permalink
Inline SQL queries using boolean parameters (#15525)
Browse files Browse the repository at this point in the history
SQLite now supports TRUE and FALSE constants, simplify some
queries by inlining those instead of passing them as arguments.
  • Loading branch information
hi-anshul committed Jul 26, 2023
1 parent 96529c4 commit 58f8305
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 25 deletions.
1 change: 1 addition & 0 deletions changelog.d/15525.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update SQL queries to inline boolean parameters as supported in SQLite 3.27.
3 changes: 1 addition & 2 deletions synapse/storage/databases/main/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ def get_backfill_points_in_room_txn(
* because the schema change is in a background update, it's not
* necessarily safe to assume that it will have been completed.
*/
AND edge.is_state is ? /* False */
AND edge.is_state is FALSE
/**
* We only want backwards extremities that are older than or at
* the same position of the given `current_depth` (where older
Expand Down Expand Up @@ -886,7 +886,6 @@ def get_backfill_points_in_room_txn(
sql,
(
room_id,
False,
current_depth,
self._clock.time_msec(),
BACKFILL_EVENT_EXPONENTIAL_BACKOFF_MAXIMUM_DOUBLING_STEPS,
Expand Down
12 changes: 6 additions & 6 deletions synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,8 +1455,8 @@ def _update_outliers_txn(
},
)

sql = "UPDATE events SET outlier = ? WHERE event_id = ?"
txn.execute(sql, (False, event.event_id))
sql = "UPDATE events SET outlier = FALSE WHERE event_id = ?"
txn.execute(sql, (event.event_id,))

# Update the event_backward_extremities table now that this
# event isn't an outlier any more.
Expand Down Expand Up @@ -1549,13 +1549,13 @@ def event_dict(event: EventBase) -> JsonDict:
for event, _ in events_and_contexts
if not event.internal_metadata.is_redacted()
]
sql = "UPDATE redactions SET have_censored = ? WHERE "
sql = "UPDATE redactions SET have_censored = FALSE WHERE "
clause, args = make_in_list_sql_clause(
self.database_engine,
"redacts",
unredacted_events,
)
txn.execute(sql + clause, [False] + args)
txn.execute(sql + clause, args)

self.db_pool.simple_insert_many_txn(
txn,
Expand Down Expand Up @@ -2318,14 +2318,14 @@ def _update_backward_extremeties(
" SELECT 1 FROM events"
" LEFT JOIN event_edges edge"
" ON edge.event_id = events.event_id"
" WHERE events.event_id = ? AND events.room_id = ? AND (events.outlier = ? OR edge.event_id IS NULL)"
" WHERE events.event_id = ? AND events.room_id = ? AND (events.outlier = FALSE OR edge.event_id IS NULL)"
" )"
)

txn.execute_batch(
query,
[
(e_id, ev.room_id, e_id, ev.room_id, e_id, ev.room_id, False)
(e_id, ev.room_id, e_id, ev.room_id, e_id, ev.room_id)
for ev in events
for e_id in ev.prev_event_ids()
if not ev.internal_metadata.is_outlier()
Expand Down
9 changes: 4 additions & 5 deletions synapse/storage/databases/main/purge_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,11 @@ def _purge_history_txn(
# Mark all state and own events as outliers
logger.info("[purge] marking remaining events as outliers")
txn.execute(
"UPDATE events SET outlier = ?"
"UPDATE events SET outlier = TRUE"
" WHERE event_id IN ("
" SELECT event_id FROM events_to_purge "
" WHERE NOT should_delete"
")",
(True,),
" SELECT event_id FROM events_to_purge "
" WHERE NOT should_delete"
")"
)

# synapse tries to take out an exclusive lock on room_depth whenever it
Expand Down
6 changes: 3 additions & 3 deletions synapse/storage/databases/main/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,19 +560,19 @@ def _upsert_push_rule_txn(
if isinstance(self.database_engine, PostgresEngine):
sql = """
INSERT INTO push_rules_enable (id, user_name, rule_id, enabled)
VALUES (?, ?, ?, ?)
VALUES (?, ?, ?, 1)
ON CONFLICT DO NOTHING
"""
elif isinstance(self.database_engine, Sqlite3Engine):
sql = """
INSERT OR IGNORE INTO push_rules_enable (id, user_name, rule_id, enabled)
VALUES (?, ?, ?, ?)
VALUES (?, ?, ?, 1)
"""
else:
raise RuntimeError("Unknown database engine")

new_enable_id = self._push_rules_enable_id_gen.get_next()
txn.execute(sql, (new_enable_id, user_id, rule_id, 1))
txn.execute(sql, (new_enable_id, user_id, rule_id))

async def delete_push_rule(self, user_id: str, rule_id: str) -> None:
"""
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,9 @@ def select_users_txn(
) -> List[Tuple[str, int]]:
sql = (
"SELECT user_id, expiration_ts_ms FROM account_validity"
" WHERE email_sent = ? AND (expiration_ts_ms - ?) <= ?"
" WHERE email_sent = FALSE AND (expiration_ts_ms - ?) <= ?"
)
values = [False, now_ms, renew_at]
values = [now_ms, renew_at]
txn.execute(sql, values)
return cast(List[Tuple[str, int]], txn.fetchall())

Expand Down
10 changes: 5 additions & 5 deletions synapse/storage/databases/main/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,11 +936,11 @@ def _get_media_mxcs_in_room_txn(
JOIN event_json USING (room_id, event_id)
WHERE room_id = ?
%(where_clause)s
AND contains_url = ? AND outlier = ?
AND contains_url = TRUE AND outlier = FALSE
ORDER BY stream_ordering DESC
LIMIT ?
"""
txn.execute(sql % {"where_clause": ""}, (room_id, True, False, 100))
txn.execute(sql % {"where_clause": ""}, (room_id, 100))

local_media_mxcs = []
remote_media_mxcs = []
Expand Down Expand Up @@ -976,7 +976,7 @@ def _get_media_mxcs_in_room_txn(

txn.execute(
sql % {"where_clause": "AND stream_ordering < ?"},
(room_id, next_token, True, False, 100),
(room_id, next_token, 100),
)

return local_media_mxcs, remote_media_mxcs
Expand Down Expand Up @@ -1086,9 +1086,9 @@ def _quarantine_media_txn(

# set quarantine
if quarantined_by is not None:
sql += "AND safe_from_quarantine = ?"
sql += "AND safe_from_quarantine = FALSE"
txn.executemany(
sql, [(quarantined_by, media_id, False) for media_id in local_mxcs]
sql, [(quarantined_by, media_id) for media_id in local_mxcs]
)
# remove from quarantine
else:
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/databases/main/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ def _paginate_room_events_txn(
`to_token`), or `limit` is zero.
"""

args = [False, room_id]
args: List[Any] = [room_id]

order, from_bound, to_bound = generate_pagination_bounds(
direction, from_token, to_token
Expand Down Expand Up @@ -1475,7 +1475,7 @@ def _paginate_room_events_txn(
event.topological_ordering, event.stream_ordering
FROM events AS event
%(join_clause)s
WHERE event.outlier = ? AND event.room_id = ? AND %(bounds)s
WHERE event.outlier = FALSE AND event.room_id = ? AND %(bounds)s
ORDER BY event.topological_ordering %(order)s,
event.stream_ordering %(order)s LIMIT ?
""" % {
Expand Down

0 comments on commit 58f8305

Please sign in to comment.