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

Commit c496729

Browse files
author
Mathieu Velten
committed
Do better
1 parent 45b6be2 commit c496729

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

synapse/storage/databases/main/deviceinbox.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,11 @@ async def delete_messages_for_device(
477477
log_kv({"message": "No changes in cache since last check"})
478478
return 0
479479

480-
ROW_ID_LITERAL = "ctid" if isinstance(self.database_engine, PostgresEngine) else "rowid"
481-
482480
def delete_messages_for_device_txn(txn: LoggingTransaction) -> int:
483481
sql = (
484-
f"DELETE FROM device_inbox WHERE {ROW_ID_LITERAL} IN ("
485-
f"SELECT {ROW_ID_LITERAL} FROM device_inbox"
486-
" WHERE user_id = ? AND device_id = ?"
487-
" AND stream_id <= ?"
482+
f"DELETE FROM device_inbox WHERE {self.database_engine.row_id_name} IN ("
483+
f"SELECT {self.database_engine.row_id_name} FROM device_inbox"
484+
" WHERE user_id = ? AND device_id = ? AND stream_id <= ?"
488485
)
489486
if limit:
490487
sql += f" LIMIT {limit}"

synapse/storage/databases/main/receipts.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -939,11 +939,6 @@ async def _background_receipts_linearized_unique_index(
939939
receipts."""
940940

941941
def _remote_duplicate_receipts_txn(txn: LoggingTransaction) -> None:
942-
if isinstance(self.database_engine, PostgresEngine):
943-
ROW_ID_NAME = "ctid"
944-
else:
945-
ROW_ID_NAME = "rowid"
946-
947942
# Identify any duplicate receipts arising from
948943
# https://github.com/matrix-org/synapse/issues/14406.
949944
# The following query takes less than a minute on matrix.org.
@@ -962,7 +957,7 @@ def _remote_duplicate_receipts_txn(txn: LoggingTransaction) -> None:
962957
# `stream_id`, we delete by the ctid instead.
963958
for stream_id, room_id, receipt_type, user_id in duplicate_keys:
964959
sql = f"""
965-
SELECT {ROW_ID_NAME}
960+
SELECT {self.database_engine.row_id_name}
966961
FROM receipts_linearized
967962
WHERE
968963
room_id = ? AND
@@ -982,7 +977,7 @@ def _remote_duplicate_receipts_txn(txn: LoggingTransaction) -> None:
982977
receipt_type = ? AND
983978
user_id = ? AND
984979
thread_id IS NULL AND
985-
{ROW_ID_NAME} != ?
980+
{self.database_engine.row_id_name} != ?
986981
"""
987982
txn.execute(sql, (room_id, receipt_type, user_id, row_id))
988983

synapse/storage/engines/_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def server_version(self) -> str:
100100
"""Gets a string giving the server version. For example: '3.22.0'"""
101101
...
102102

103+
@property
104+
@abc.abstractmethod
105+
def row_id_name(self) -> str:
106+
"""Gets the literal name representing a row id for this engine."""
107+
...
108+
103109
@abc.abstractmethod
104110
def in_transaction(self, conn: ConnectionType) -> bool:
105111
"""Whether the connection is currently in a transaction."""

synapse/storage/engines/postgres.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ def server_version(self) -> str:
211211
else:
212212
return "%i.%i.%i" % (numver / 10000, (numver % 10000) / 100, numver % 100)
213213

214+
@property
215+
def row_id_name(self) -> str:
216+
return "ctid"
217+
214218
def in_transaction(self, conn: psycopg2.extensions.connection) -> bool:
215219
return conn.status != psycopg2.extensions.STATUS_READY
216220

synapse/storage/engines/sqlite.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ def server_version(self) -> str:
123123
"""Gets a string giving the server version. For example: '3.22.0'."""
124124
return "%i.%i.%i" % sqlite3.sqlite_version_info
125125

126+
@property
127+
def row_id_name(self) -> str:
128+
return "rowid"
129+
126130
def in_transaction(self, conn: sqlite3.Connection) -> bool:
127131
return conn.in_transaction
128132

synapse/storage/schema/main/delta/48/group_unique_indexes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
from synapse.storage.database import LoggingTransaction
17-
from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine
17+
from synapse.storage.engines import BaseDatabaseEngine
1818
from synapse.storage.prepare_database import get_statements
1919

2020
FIX_INDEXES = """
@@ -37,7 +37,7 @@
3737

3838

3939
def run_create(cur: LoggingTransaction, database_engine: BaseDatabaseEngine) -> None:
40-
rowid = "ctid" if isinstance(database_engine, PostgresEngine) else "rowid"
40+
rowid = database_engine.row_id_name
4141

4242
# remove duplicates from group_users & group_invites tables
4343
cur.execute(

0 commit comments

Comments
 (0)