Skip to content
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

feat: upsert with on-conflict support #142

Merged
merged 6 commits into from
Feb 7, 2023
Merged
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
3 changes: 3 additions & 0 deletions postgrest/_async/request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def upsert(
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
ignore_duplicates: bool = False,
on_conflict: str = "",
) -> AsyncQueryRequestBuilder:
"""Run an upsert (INSERT ... ON CONFLICT DO UPDATE) query.

Expand All @@ -164,6 +165,7 @@ def upsert(
count: The method to use to get the count of rows returned.
returning: Either 'minimal' or 'representation'
ignore_duplicates: Whether duplicate rows should be ignored.
on_conflict: Specified columns to be made to work with UNIQUE constraint.
Returns:
:class:`AsyncQueryRequestBuilder`
"""
Expand All @@ -172,6 +174,7 @@ def upsert(
count=count,
returning=returning,
ignore_duplicates=ignore_duplicates,
on_conflict=on_conflict
)
return AsyncQueryRequestBuilder(
self.session, self.path, method, headers, params, json
Expand Down
3 changes: 3 additions & 0 deletions postgrest/_sync/request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def upsert(
count: Optional[CountMethod] = None,
returning: ReturnMethod = ReturnMethod.representation,
ignore_duplicates: bool = False,
on_conflict: str = "",
Oisangui marked this conversation as resolved.
Show resolved Hide resolved
) -> SyncQueryRequestBuilder:
"""Run an upsert (INSERT ... ON CONFLICT DO UPDATE) query.

Expand All @@ -164,6 +165,7 @@ def upsert(
count: The method to use to get the count of rows returned.
returning: Either 'minimal' or 'representation'
ignore_duplicates: Whether duplicate rows should be ignored.
on_conflict: Specified columns to be made to work with UNIQUE constraint.
Returns:
:class:`SyncQueryRequestBuilder`
"""
Expand All @@ -172,6 +174,7 @@ def upsert(
count=count,
returning=returning,
ignore_duplicates=ignore_duplicates,
on_conflict=on_conflict
)
return SyncQueryRequestBuilder(
self.session, self.path, method, headers, params, json
Expand Down
6 changes: 5 additions & 1 deletion postgrest/base_request_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ def pre_upsert(
count: Optional[CountMethod],
returning: ReturnMethod,
ignore_duplicates: bool,
on_conflict: str= "",
) -> QueryArgs:
query_params = dict()
prefer_headers = [f"return={returning}"]
if count:
prefer_headers.append(f"count={count}")
resolution = "ignore" if ignore_duplicates else "merge"
prefer_headers.append(f"resolution={resolution}-duplicates")
headers = Headers({"Prefer": ",".join(prefer_headers)})
return QueryArgs(RequestMethod.POST, QueryParams(), headers, json)
if on_conflict:
query_params["on_conflict"] = on_conflict
return QueryArgs(RequestMethod.POST, QueryParams(query_params), headers, json)


def pre_update(
Expand Down