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

Inline SQL queries using boolean parameters #15525

Merged
merged 39 commits into from Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5cfdae4
Fixing SQL Query
hi-anshul May 3, 2023
cd4359a
Inline SQL queries using boolean parameters #15515
hi-anshul May 3, 2023
3857478
Update purge_events.py
hi-anshul May 3, 2023
2ea3b58
Merge branch 'develop' into develop
hi-anshul May 3, 2023
44aedd8
Merge branch 'matrix-org:develop' into develop
hi-anshul May 22, 2023
7d60307
Updated all Inline SQL queries using boolean parameters as supported …
hi-anshul May 22, 2023
36c4012
Updated all Inline SQL queries using boolean parameters as supported …
hi-anshul May 22, 2023
e5c5d35
Merge branch 'develop' into develop
hi-anshul May 22, 2023
07e90d4
Merge branch 'matrix-org:develop' into develop
hi-anshul Jun 16, 2023
c29c05f
Merge branch 'matrix-org:develop' into develop
hi-anshul Jun 19, 2023
be3fbc1
Merge branch 'matrix-org:develop' into develop
hi-anshul Jun 19, 2023
8737a24
Updated Inline SQL queries using boolean parameters as supported in S…
hi-anshul Jun 19, 2023
24f4de0
Merge branch 'develop' into develop
hi-anshul Jun 20, 2023
7eb4f91
Update 15515.misc
hi-anshul Jun 21, 2023
e5a668d
Merge branch 'matrix-org:develop' into develop
hi-anshul Jun 21, 2023
aa97aa2
Merge branch 'develop' into develop
hi-anshul Jun 21, 2023
f733e25
Update event_federation.py
hi-anshul Jun 23, 2023
7ae7fef
Update stream.py
hi-anshul Jun 23, 2023
b33a4d2
Merge branch 'matrix-org:develop' into develop
hi-anshul Jun 23, 2023
152e015
Merge branch 'matrix-org:develop' into develop
hi-anshul Jul 5, 2023
97f337b
Updated Inline SQL queries using boolean parameters as supported in S…
hi-anshul Jul 5, 2023
53d04b9
Merge branch 'develop' into develop
hi-anshul Jul 5, 2023
19e15c3
Update changelog.d/15515.misc
MadLittleMods Jul 5, 2023
32d8dd4
Rename 15515.misc to 15525.misc
hi-anshul Jul 6, 2023
99e265b
Merge branch 'develop' into develop
hi-anshul Jul 6, 2023
e871524
Update stream.py
hi-anshul Jul 6, 2023
1c5a66e
Merge branch 'matrix-org:develop' into develop
hi-anshul Jul 6, 2023
3e7c901
Update synapse/storage/databases/main/purge_events.py
hi-anshul Jul 7, 2023
34e98ec
Merge branch 'develop' into develop
hi-anshul Jul 7, 2023
66b8ab0
Update synapse/storage/databases/main/stream.py
hi-anshul Jul 7, 2023
634b269
Merge branch 'develop' into develop
hi-anshul Jul 8, 2023
8f7029d
Merge branch 'develop' into develop
hi-anshul Jul 11, 2023
ea1fbfc
Update synapse/storage/databases/main/events.py
hi-anshul Jul 12, 2023
cafd08e
Merge branch 'matrix-org:develop' into develop
hi-anshul Jul 23, 2023
aaa1ad2
Updated push_rule.py
hi-anshul Jul 23, 2023
c4340ff
Merge branch 'develop' into develop
clokep Jul 24, 2023
37d3013
Merge branch 'matrix-org:develop' into develop
hi-anshul Jul 26, 2023
b9e505a
Update room.py
hi-anshul Jul 26, 2023
4506e7f
Merge branch 'develop' into develop
hi-anshul Jul 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/15515.misc
@@ -0,0 +1 @@
Update SQL queries to inline boolean parameters as supported in SQLite 3.27.
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 1 addition & 2 deletions synapse/storage/databases/main/event_federation.py
Expand Up @@ -841,7 +841,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 @@ -884,7 +884,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
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))
hi-anshul marked this conversation as resolved.
Show resolved Hide resolved

# 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
11 changes: 5 additions & 6 deletions synapse/storage/databases/main/purge_events.py
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 = ?"
" WHERE event_id IN ("
" SELECT event_id FROM events_to_purge "
" WHERE NOT should_delete"
")",
(True,),
"UPDATE events SET outlier = TRUE"
"WHERE event_id IN ("
hi-anshul marked this conversation as resolved.
Show resolved Hide resolved
" 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
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 (?, ?, ?, TRUE)
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 (?, ?, ?, TRUE)
"""
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))
clokep marked this conversation as resolved.
Show resolved Hide resolved
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
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
8 changes: 4 additions & 4 deletions synapse/storage/databases/main/room.py
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))
clokep marked this conversation as resolved.
Show resolved Hide resolved
txn.execute(sql % {"where_clause": ""}, (room_id, 100))

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
2 changes: 1 addition & 1 deletion synapse/storage/databases/main/stream.py
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
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
ORDER BY event.topological_ordering %(order)s,
event.stream_ordering %(order)s LIMIT ?
""" % {
Expand Down