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

Commit 823e482

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

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
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/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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)