Allow query param 'on_conflict=key1,key2,...' to upsert with explicit columns with unique constraint.
This commit is contained in:
committed by
Steve Chavez
parent
e12c1319b6
commit
99b13fa25f
@@ -49,6 +49,31 @@ spec =
|
||||
[json|[]|] `shouldRespondWith`
|
||||
[json|[]|] { matchStatus = 201 , matchHeaders = [matchContentTypeJson] }
|
||||
|
||||
it "INSERTs and UPDATEs rows on single unique key conflict" $
|
||||
request methodPost "/single_unique?on_conflict=unique_key" [("Prefer", "return=representation"), ("Prefer", "resolution=merge-duplicates")]
|
||||
[json| [
|
||||
{ "unique_key": 1, "value": "B" },
|
||||
{ "unique_key": 2, "value": "C" }
|
||||
]|] `shouldRespondWith` [json| [
|
||||
{ "unique_key": 1, "value": "B" },
|
||||
{ "unique_key": 2, "value": "C" }
|
||||
]|]
|
||||
{ matchStatus = 201
|
||||
, matchHeaders = ["Preference-Applied" <:> "resolution=merge-duplicates", matchContentTypeJson]
|
||||
}
|
||||
|
||||
it "INSERTs and UPDATEs rows on compound unique keys conflict" $
|
||||
request methodPost "/compound_unique?on_conflict=key1,key2" [("Prefer", "return=representation"), ("Prefer", "resolution=merge-duplicates")]
|
||||
[json| [
|
||||
{ "key1": 1, "key2": 1, "value": "B" },
|
||||
{ "key1": 1, "key2": 2, "value": "C" }
|
||||
]|] `shouldRespondWith` [json| [
|
||||
{ "key1": 1, "key2": 1, "value": "B" },
|
||||
{ "key1": 1, "key2": 2, "value": "C" }
|
||||
]|]
|
||||
{ matchStatus = 201
|
||||
, matchHeaders = ["Preference-Applied" <:> "resolution=merge-duplicates", matchContentTypeJson]
|
||||
}
|
||||
|
||||
context "when Prefer: resolution=ignore-duplicates is specified" $ do
|
||||
it "INSERTs and ignores rows on pk conflict" $
|
||||
@@ -75,6 +100,32 @@ spec =
|
||||
, matchHeaders = ["Preference-Applied" <:> "resolution=ignore-duplicates", matchContentTypeJson]
|
||||
}
|
||||
|
||||
it "INSERTs and ignores rows on single unique key conflict" $
|
||||
request methodPost "/single_unique?on_conflict=unique_key" [("Prefer", "return=representation"), ("Prefer", "resolution=ignore-duplicates")]
|
||||
[json| [
|
||||
{ "unique_key": 1, "value": "B" },
|
||||
{ "unique_key": 2, "value": "C" },
|
||||
{ "unique_key": 3, "value": "D" }
|
||||
]|] `shouldRespondWith` [json| [
|
||||
{ "unique_key": 3, "value": "D" }
|
||||
]|]
|
||||
{ matchStatus = 201
|
||||
, matchHeaders = ["Preference-Applied" <:> "resolution=ignore-duplicates", matchContentTypeJson]
|
||||
}
|
||||
|
||||
it "INSERTs and UPDATEs rows on compound unique keys conflict" $
|
||||
request methodPost "/compound_unique?on_conflict=key1,key2" [("Prefer", "return=representation"), ("Prefer", "resolution=ignore-duplicates")]
|
||||
[json| [
|
||||
{ "key1": 1, "key2": 1, "value": "B" },
|
||||
{ "key1": 1, "key2": 2, "value": "C" },
|
||||
{ "key1": 1, "key2": 3, "value": "D" }
|
||||
]|] `shouldRespondWith` [json| [
|
||||
{ "key1": 1, "key2": 3, "value": "D" }
|
||||
]|]
|
||||
{ matchStatus = 201
|
||||
, matchHeaders = ["Preference-Applied" <:> "resolution=ignore-duplicates", matchContentTypeJson]
|
||||
}
|
||||
|
||||
it "succeeds if the table has only PK cols and no other cols" $ do
|
||||
request methodPost "/only_pk" [("Prefer", "return=representation"), ("Prefer", "resolution=ignore-duplicates")]
|
||||
[json|[ { "id": 1 }, { "id": 2 }, { "id": 3} ]|]
|
||||
|
||||
Vendored
+6
@@ -344,6 +344,12 @@ INSERT INTO employees VALUES
|
||||
TRUNCATE TABLE tiobe_pls CASCADE;
|
||||
INSERT INTO tiobe_pls VALUES ('Java', 1), ('C', 2), ('Python', 4);
|
||||
|
||||
TRUNCATE TABLE single_unique CASCADE;
|
||||
INSERT INTO single_unique (unique_key, value) VALUES (1, 'A');
|
||||
|
||||
TRUNCATE TABLE compound_unique CASCADE;
|
||||
INSERT INTO compound_unique (key1, key2, value) VALUES (1, 1, 'A');
|
||||
|
||||
TRUNCATE TABLE only_pk CASCADE;
|
||||
INSERT INTO only_pk VALUES (1), (2);
|
||||
|
||||
|
||||
Vendored
+2
@@ -66,6 +66,8 @@ GRANT ALL ON TABLE
|
||||
, perf_articles
|
||||
, employees
|
||||
, tiobe_pls
|
||||
, single_unique
|
||||
, compound_unique
|
||||
, only_pk
|
||||
, family_tree
|
||||
, managers
|
||||
|
||||
Vendored
+12
@@ -1379,6 +1379,18 @@ create table test.tiobe_pls(
|
||||
rank smallint
|
||||
);
|
||||
|
||||
create table test.single_unique(
|
||||
unique_key integer unique not null,
|
||||
value text
|
||||
);
|
||||
|
||||
create table test.compound_unique(
|
||||
key1 integer not null,
|
||||
key2 integer not null,
|
||||
value text,
|
||||
unique(key1, key2)
|
||||
);
|
||||
|
||||
create table test.family_tree (
|
||||
id text not null primary key,
|
||||
name text not null,
|
||||
|
||||
Reference in New Issue
Block a user