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

Commit

Permalink
More tests for the simple_* methods. (#16596)
Browse files Browse the repository at this point in the history
Expand tests for the simple_* database methods, additionally
test against both PostgreSQL and SQLite variants.
  • Loading branch information
clokep authored Nov 7, 2023
1 parent 7e5d3b0 commit ec9ff38
Show file tree
Hide file tree
Showing 3 changed files with 633 additions and 27 deletions.
1 change: 1 addition & 0 deletions changelog.d/16596.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve tests of the SQL generator.
13 changes: 4 additions & 9 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,12 +1401,12 @@ def simple_upsert_txn_native_upsert(
allvalues.update(values)
latter = "UPDATE SET " + ", ".join(k + "=EXCLUDED." + k for k in values)

sql = "INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) %s DO %s" % (
sql = "INSERT INTO %s (%s) VALUES (%s) ON CONFLICT (%s) %sDO %s" % (
table,
", ".join(k for k in allvalues),
", ".join("?" for _ in allvalues),
", ".join(k for k in keyvalues),
f"WHERE {where_clause}" if where_clause else "",
f"WHERE {where_clause} " if where_clause else "",
latter,
)
txn.execute(sql, list(allvalues.values()))
Expand Down Expand Up @@ -2062,9 +2062,7 @@ def simple_update_many_txn(
where_clause = ""

# UPDATE mytable SET col1 = ?, col2 = ? WHERE col3 = ? AND col4 = ?
sql = f"""
UPDATE {table} SET {set_clause} {where_clause}
"""
sql = f"UPDATE {table} SET {set_clause} {where_clause}"

txn.execute_batch(sql, args)

Expand Down Expand Up @@ -2283,17 +2281,14 @@ def simple_delete_many_txn(
if not values:
return 0

sql = "DELETE FROM %s" % table

clause, values = make_in_list_sql_clause(txn.database_engine, column, values)
clauses = [clause]

for key, value in keyvalues.items():
clauses.append("%s = ?" % (key,))
values.append(value)

if clauses:
sql = "%s WHERE %s" % (sql, " AND ".join(clauses))
sql = "DELETE FROM %s WHERE %s" % (table, " AND ".join(clauses))
txn.execute(sql, values)

return txn.rowcount
Expand Down
Loading

0 comments on commit ec9ff38

Please sign in to comment.