Skip to content

Commit d26ce0b

Browse files
authored
Fix handling of inserts with both unless conflict and a with block (#1291)
Fixes #788, and should also fix #679
1 parent c05fc96 commit d26ce0b

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

integration-tests/lts/dbschema/default.esdl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ module default {
5050
multi link villains := .<nemesis[IS Villain];
5151
}
5252

53+
type Director {
54+
required property name -> str {
55+
constraint exclusive;
56+
};
57+
}
58+
5359
scalar type year extending int16 {
5460
constraint min_value(1878);
5561
}
@@ -67,6 +73,7 @@ module default {
6773
link profile -> Profile {
6874
constraint exclusive;
6975
}
76+
multi link directors -> Director;
7077
constraint exclusive on ((.title, .release_year));
7178
}
7279

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE MIGRATION m1hbukk7q7j6plwsmgronpimcotrc3uq2i457xronjbre6dt6ie23q
2+
ONTO m14ccrzhfjulgoy3mdub7s7cfw5neuoxawxlwos2efvwv2si66mbka
3+
{
4+
CREATE TYPE default::Director {
5+
CREATE REQUIRED PROPERTY name: std::str {
6+
CREATE CONSTRAINT std::exclusive;
7+
};
8+
};
9+
ALTER TYPE default::Movie {
10+
CREATE MULTI LINK directors: default::Director;
11+
};
12+
};

integration-tests/lts/gel.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[edgedb]
2-
server-version = "3"
2+
server-version = "6"

integration-tests/lts/insert.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,51 @@ describe("insert", () => {
165165
assert.equal(result, 42);
166166
});
167167

168+
test("with block on insert with unless conflict", async () => {
169+
// test that the 'bulk inserts with conflict handling' query we show in
170+
// the docs works in the query builder
171+
// https://docs.geldata.com/reference/using/js/querybuilder#handling-conflicts-in-bulk-inserts
172+
const query = e.params(
173+
{
174+
movies: e.array(
175+
e.tuple({
176+
title: e.str,
177+
directors: e.array(e.str),
178+
}),
179+
),
180+
},
181+
($) => {
182+
const directors = e.for(
183+
e.op("distinct", e.array_unpack(e.array_unpack($.movies).directors)),
184+
(director_name) =>
185+
e
186+
.insert(e.Director, { name: director_name })
187+
.unlessConflict((d) => ({ on: d.name, else: d })),
188+
);
189+
190+
return e.with(
191+
[directors],
192+
e.for(e.array_unpack($.movies), (movie) =>
193+
e
194+
.insert(e.Movie, {
195+
title: movie.title,
196+
directors: e.select(directors, (director) => ({
197+
filter: e.op(
198+
director.name,
199+
"in",
200+
e.array_unpack(movie.directors),
201+
),
202+
})),
203+
})
204+
.unlessConflict((m) => ({ on: m.title, else: m })),
205+
),
206+
);
207+
},
208+
);
209+
210+
await query.run(client, { movies: [] });
211+
});
212+
168213
test("nested insert", async () => {
169214
const q1 = e.insert(e.Villain, {
170215
name: e.str("villain"),

integration-tests/lts/interfaces.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ export interface test_Person extends BaseObject {
2727
height?: string | null;
2828
isAdult?: boolean | null;
2929
}
30+
export interface test_Director extends BaseObject {
31+
name: string;
32+
}
3033
export interface test_Movie extends BaseObject {
3134
characters: test_Person[];
35+
directors: test_Director[];
3236
profile?: test_Profile | null;
3337
genre?: Genre | null;
3438
rating?: number | null;

packages/generate/src/syntax/toEdgeQL.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,10 @@ function walkExprTree(
606606
);
607607
}
608608

609-
walkExprTree(expr.__expr__, parentScope, ctx);
609+
childExprs.push(
610+
...walkExprTree(expr.__expr__, parentScope, ctx),
611+
...insertChildExprs,
612+
);
610613
ctx.seen
611614
.get(expr.__expr__ as $expr_Insert)!
612615
.childExprs.push(...insertChildExprs);

0 commit comments

Comments
 (0)