Skip to content

Improve docstring on simple_upsert_many. #18573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/18573.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve docstring on `simple_upsert_many`.
46 changes: 42 additions & 4 deletions synapse/storage/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,13 +1478,49 @@ async def simple_upsert_many(
"""
Upsert, many times.

This executes a query equivalent to `INSERT INTO ... ON CONFLICT DO UPDATE`,
with multiple value rows.
The query may use emulated upserts if the database engine does not support upserts,
or if the table is currently unsafe to upsert.

If there are no value columns, this instead generates a `ON CONFLICT DO NOTHING`.

Args:
table: The table to upsert into
key_names: The key column names.
key_values: A list of each row's key column values.
value_names: The value column names
value_values: A list of each row's value column values.
key_names: The unique key column names. These are the columns used in the ON CONFLICT clause.
key_values: A list of each row's key column values, in the same order as `key_names`.
value_names: The non-unique value column names
value_values: A list of each row's value column values, in the same order as `value_names`.
Ignored if value_names is empty.

Example:
```python
simple_upsert_many(
"mytable",
key_names=("room_id", "user_id"),
key_values=[
("!room1:example.org", "@user1:example.org"),
("!room2:example.org", "@user2:example.org"),
],
value_names=("wombat_count", "is_updated"),
value_values=[
(42, True),
(7, False)
],
)
```

gives something equivalent to:

```sql
INSERT INTO mytable (room_id, user_id, wombat_count, is_updated)
VALUES
('!room1:example.org', '@user1:example.org', 42, True),
('!room2:example.org', '@user2:example.org', 7, False)
ON CONFLICT DO UPDATE SET
wombat_count = EXCLUDED.wombat_count,
is_updated = EXCLUDED.is_updated
```
"""

# We can autocommit if it safe to upsert
Expand Down Expand Up @@ -1513,6 +1549,8 @@ def simple_upsert_many_txn(
"""
Upsert, many times.

See the documentation for `simple_upsert_many` for examples.

Args:
table: The table to upsert into
key_names: The key column names.
Expand Down
Loading