Skip to content

Commit

Permalink
on conflict tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olirice committed Aug 26, 2024
1 parent 8da22dc commit ffcfe27
Show file tree
Hide file tree
Showing 2 changed files with 354 additions and 0 deletions.
216 changes: 216 additions & 0 deletions test/expected/mutation_insert_on_conflict.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
begin;
create table account(
id int primary key,
email varchar(255) not null,
priority int,
status text default 'active'
);
/*
Literals
*/
select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "foo@barsley.com", priority: 1 },
{ id: 2, email: "bar@foosworth.com" }
]
onConflict: {
constraint: account_pkey,
updateFields: [email, priority, status],
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$));
jsonb_pretty
---------------------------------------------------
{ +
"data": { +
"insertIntoAccountCollection": { +
"records": [ +
{ +
"id": 1, +
"email": "foo@barsley.com", +
"priority": 1 +
}, +
{ +
"id": 2, +
"email": "bar@foosworth.com",+
"priority": null +
} +
], +
"affectedCount": 2 +
} +
} +
}
(1 row)

-- Email should update. Priority should not
-- 1 row affected
select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "new@email.com", priority: 2 },
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
) {
affectedCount
records {
id
email
}
}
}
$$));
jsonb_pretty
----------------------------------------------
{ +
"data": { +
"insertIntoAccountCollection": { +
"records": [ +
{ +
"id": 1, +
"email": "new@email.com"+
} +
], +
"affectedCount": 1 +
} +
} +
}
(1 row)

-- Email and priority should update
-- 2 row affected
select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "new@email.com", priority: 2 },
{ id: 2, email: "new@email.com"},
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$));
jsonb_pretty
-----------------------------------------------
{ +
"data": { +
"insertIntoAccountCollection": { +
"records": [ +
{ +
"id": 1, +
"email": "new@email.com",+
"priority": 1 +
}, +
{ +
"id": 2, +
"email": "new@email.com",+
"priority": null +
} +
], +
"affectedCount": 2 +
} +
} +
}
(1 row)

-- Filter prevents second row update
-- 1 row affected
select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "third@email.com"},
{ id: 2, email: "new@email.com"},
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
filter: {
id: {id: $ifilt}
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$));
jsonb_pretty
-----------------------------------------------------------------
{ +
"data": null, +
"errors": [ +
{ +
"message": "Input contains extra keys [\"filter\"]"+
} +
] +
}
(1 row)

-- Variable Filter
-- Only row id=2 updated due to where clause
select jsonb_pretty(graphql.resolve($$
mutation AccountsFiltered($ifilt: IntFilter!)
insertIntoAccountCollection(
objects: [
{ id: 1, email: "fourth@email.com"},
{ id: 2, email: "fourth@email.com"},
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
filter: {
id: {id: $ifilt}
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$,
variables:= '{"ifilt": {"eq": 2}}'
));
jsonb_pretty
--------------------------------------------------------------------------------------------------------------------------------
{ +
"errors": [ +
{ +
"message": "query parse error: Parse error at 3:7\nUnexpected `insertIntoAccountCollection[Name]`\nExpected `{`\n"+
} +
] +
}
(1 row)

rollback;
138 changes: 138 additions & 0 deletions test/sql/mutation_insert_on_conflict.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
begin;

create table account(
id int primary key,
email varchar(255) not null,
priority int,
status text default 'active'
);

/*
Literals
*/

select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "foo@barsley.com", priority: 1 },
{ id: 2, email: "bar@foosworth.com" }
]
onConflict: {
constraint: account_pkey,
updateFields: [email, priority, status],
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$));

-- Email should update. Priority should not
-- 1 row affected
select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "new@email.com", priority: 2 },
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
) {
affectedCount
records {
id
email
}
}
}
$$));

-- Email and priority should update
-- 2 row affected
select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "new@email.com", priority: 2 },
{ id: 2, email: "new@email.com"},
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$));

-- Filter prevents second row update
-- 1 row affected
select jsonb_pretty(graphql.resolve($$
mutation {
insertIntoAccountCollection(
objects: [
{ id: 1, email: "third@email.com"},
{ id: 2, email: "new@email.com"},
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
filter: {
id: {id: $ifilt}
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$));

-- Variable Filter
-- Only row id=2 updated due to where clause
select jsonb_pretty(graphql.resolve($$
mutation AccountsFiltered($ifilt: IntFilter!)
insertIntoAccountCollection(
objects: [
{ id: 1, email: "fourth@email.com"},
{ id: 2, email: "fourth@email.com"},
]
onConflict: {
constraint: account_pkey,
updateFields: [email, status],
}
filter: {
id: {id: $ifilt}
}
) {
affectedCount
records {
id
email
priority
}
}
}
$$,
variables:= '{"ifilt": {"eq": 2}}'
));

rollback;

0 comments on commit ffcfe27

Please sign in to comment.