fix: HTTP status responses for upserts

* PUT returns 201 instead of 200 when rows are inserted
* POST with "Prefer: resolution=merge-duplicates" returns 200 instead of 201 when no rows are inserted
This commit is contained in:
Taimoor Zaeem
2023-10-26 20:49:20 -05:00
committed by GitHub
parent 82b38341bf
commit 5c9c7f4ff4
12 changed files with 97 additions and 42 deletions
+7
View File
@@ -24,6 +24,8 @@ module PostgREST.Query.SqlFragment
, fromJsonBodyF
, responseHeadersF
, responseStatusF
, addConfigPgrstInserted
, currentSettingF
, returningF
, singleParameter
, sourceCTE
@@ -433,6 +435,11 @@ responseHeadersF = currentSettingF "response.headers"
responseStatusF :: SQL.Snippet
responseStatusF = currentSettingF "response.status"
addConfigPgrstInserted :: Bool -> SQL.Snippet
addConfigPgrstInserted add =
let (symbol, num) = if add then ("+", "0") else ("-", "-1") in
"set_config('pgrst.inserted', (coalesce(" <> currentSettingF "pgrst.inserted" <> "::int, 0) " <> symbol <> " 1)::text, true) <> '" <> num <> "'"
currentSettingF :: SQL.Snippet -> SQL.Snippet
currentSettingF setting =
-- nullif is used because of https://gist.github.com/steve-chavez/8d7033ea5655096903f3b52f8ed09a15