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

Commit

Permalink
Support PostgreSQL.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Sep 15, 2022
1 parent 3c52b6a commit 2ed39f6
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions synapse/storage/databases/main/event_push_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
DatabasePool,
LoggingDatabaseConnection,
LoggingTransaction,
PostgresEngine,
)
from synapse.storage.databases.main.receipts import ReceiptsWorkerStore
from synapse.storage.databases.main.stream import StreamWorkerStore
Expand Down Expand Up @@ -445,6 +446,14 @@ def _get_unread_counts_by_pos_txn(
(ReceiptTypes.READ, ReceiptTypes.READ_PRIVATE),
)

# PostgreSQL and SQLite differ in comparing scalar numerics.
if isinstance(self.database_engine, PostgresEngine):
# GREATEST ignores NULLs.
receipt_stream_clause = "GREATEST(receipt_stream_ordering, ?)"
else:
# MAX returns NULL if any are NULL, so COALESCE to 0 first.
receipt_stream_clause = "MAX(COALESCE(receipt_stream_ordering, 0), ?)"

# First we pull the counts from the summary table.
#
# We check that `last_receipt_stream_ordering` matches the stream
Expand Down Expand Up @@ -474,8 +483,8 @@ def _get_unread_counts_by_pos_txn(
) AS receipts USING (thread_id)
WHERE room_id = ? AND user_id = ?
AND (
(last_receipt_stream_ordering IS NULL AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?))
OR last_receipt_stream_ordering = MAX(COALESCE(receipt_stream_ordering, 0), ?)
(last_receipt_stream_ordering IS NULL AND stream_ordering > {receipt_stream_clause})
OR last_receipt_stream_ordering = {receipt_stream_clause}
)
""",
(
Expand Down Expand Up @@ -516,7 +525,7 @@ def _get_unread_counts_by_pos_txn(
) AS receipts USING (thread_id)
WHERE user_id = ?
AND room_id = ?
AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?)
AND stream_ordering > {receipt_stream_clause}
AND highlight = 1
GROUP BY thread_id
"""
Expand Down Expand Up @@ -601,7 +610,7 @@ def _get_unread_counts_by_pos_txn(
) AS receipts USING (thread_id)
WHERE user_id = ?
AND room_id = ?
AND stream_ordering > MAX(COALESCE(receipt_stream_ordering, 0), ?)
AND stream_ordering > {receipt_stream_clause}
AND NOT {thread_id_clause}
GROUP BY thread_id
"""
Expand Down

0 comments on commit 2ed39f6

Please sign in to comment.