Do not return the number of deleted rows from simple_truncate

Postgres does not support returning this information for a TRUNCATE command.
This commit is contained in:
Andrew Morgan 2022-08-04 16:37:10 +01:00
parent 1da8f115b5
commit efc492ed99
2 changed files with 8 additions and 15 deletions

View file

@ -2204,20 +2204,18 @@ class DatabasePool:
return txn.rowcount
async def simple_truncate(self, table: str, desc: str) -> int:
async def simple_truncate(self, table: str, desc: str) -> None:
"""Executes a TRUNCATE query on the given table, deleting all rows.
SQLite does not support TRUNCATE, thus a 'DELETE FROM table_name' will
be used instead.
be used instead. This method does not return the number of rows deleted,
as this is not returned by postgres for TRUNCATE commands.
Args:
table: The name of the table to delete all rows from.
desc: description of the transaction, for logging and metrics.
Returns:
The number of deleted rows.
"""
return await self.runInteraction(
await self.runInteraction(
desc, self._simple_truncate_txn, table, db_autocommit=True
)
@ -2225,18 +2223,16 @@ class DatabasePool:
def _simple_truncate_txn(
txn: LoggingTransaction,
table: str,
) -> int:
) -> None:
"""Executes a TRUNCATE query on the given table, deleting all rows.
SQLite does not support TRUNCATE, thus a 'DELETE FROM table_name' will
be used instead.
be used instead. This method does not return the number of rows deleted,
as this is not returned by postgres for TRUNCATE commands.
Args:
txn: Transaction object
table: The name of the table to delete all rows from.
Returns:
The number of deleted rows.
"""
if isinstance(txn.database_engine, PostgresEngine):
sql = "TRUNCATE %s" % table
@ -2245,7 +2241,6 @@ class DatabasePool:
sql = "DELETE FROM %s" % table
txn.execute(sql)
return txn.rowcount
def get_cache_dict(
self,

View file

@ -196,14 +196,12 @@ class SimpleTruncateTestCase(unittest.HomeserverTestCase):
self.assertGreater(len(table_rows), 0)
# Attempt to truncate the table
number_of_deleted_rows = self.get_success(
self.get_success(
self.storage.db_pool.simple_truncate(
table=self.table_name,
desc="simple_truncate_test_truncate",
)
)
# Check that the number of deleted rows is as we expect.
self.assertEqual(number_of_deleted_rows, len(table_rows))
# Perform another select and ensure there are no remaining rows.
table_rows = self.get_success(