Skip to content

Commit f1757f8

Browse files
authored
Merge pull request #878 from dolthub/zachmu/cross-schema-fks
tests for cross-schema foreign keys
2 parents b5b0193 + 32ab888 commit f1757f8

File tree

3 files changed

+236
-13
lines changed

3 files changed

+236
-13
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ require (
88
github.com/PuerkitoBio/goquery v1.8.1
99
github.com/cockroachdb/apd/v2 v2.0.3-0.20200518165714-d020e156310a
1010
github.com/cockroachdb/errors v1.7.5
11-
github.com/dolthub/dolt/go v0.40.5-0.20241022005459-1290f6a902c8
11+
github.com/dolthub/dolt/go v0.40.5-0.20241025002354-1bf5606b7e23
1212
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240827111219-e4bb9ca3442d
1313
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
1414
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662
15-
github.com/dolthub/go-mysql-server v0.18.2-0.20241022002146-c5725b1bf340
15+
github.com/dolthub/go-mysql-server v0.18.2-0.20241024233927-96a484368bea
1616
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216
1717
github.com/dolthub/vitess v0.0.0-20241016191424-d14e107a654e
1818
github.com/fatih/color v1.13.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
214214
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
215215
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
216216
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
217-
github.com/dolthub/dolt/go v0.40.5-0.20241022005459-1290f6a902c8 h1:N0V3lAnC7vWscLYDg3ZNVjIYoOJWeQxlOd/kvwBJhIM=
218-
github.com/dolthub/dolt/go v0.40.5-0.20241022005459-1290f6a902c8/go.mod h1:24J7n/VGWEwl5EM8qLBDxaAJBxRt0rraOcoYRBVFIYg=
217+
github.com/dolthub/dolt/go v0.40.5-0.20241025002354-1bf5606b7e23 h1:R/isZppGvXrUIujDnYblRvdfGJxgsNSbnNwG6Bu2BSY=
218+
github.com/dolthub/dolt/go v0.40.5-0.20241025002354-1bf5606b7e23/go.mod h1:Dlf8rNaNwRfW+RRcsGxDxxpkwB616hrh5Gqr2YJGQOg=
219219
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240827111219-e4bb9ca3442d h1:RZkQeYOrDrOWzCxaP2ttkvg4E2TM9n8lnEsIBLKjqkM=
220220
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20240827111219-e4bb9ca3442d/go.mod h1:L5RDYZbC9BBWmoU2+TjTekeqqhFXX5EqH9ln00O0stY=
221221
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1Gms9599cr0REMww=
@@ -224,8 +224,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U=
224224
github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
225225
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662 h1:aC17hZD6iwzBwwfO5M+3oBT5E5gGRiQPdn+vzpDXqIA=
226226
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
227-
github.com/dolthub/go-mysql-server v0.18.2-0.20241022002146-c5725b1bf340 h1:OJkZTo67Ikm6Tz6ml4Mn9qbC6he49X9dZfYqIwaZrNM=
228-
github.com/dolthub/go-mysql-server v0.18.2-0.20241022002146-c5725b1bf340/go.mod h1:z/GGuH2asedC+lkJA4sx+C3oyRH1HRx8ET6N9AGBVms=
227+
github.com/dolthub/go-mysql-server v0.18.2-0.20241024233927-96a484368bea h1:bqHfelpj4LTC4RQnnFeC8KiXda90E0YfG0Elg5AqV0w=
228+
github.com/dolthub/go-mysql-server v0.18.2-0.20241024233927-96a484368bea/go.mod h1:z/GGuH2asedC+lkJA4sx+C3oyRH1HRx8ET6N9AGBVms=
229229
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
230230
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
231231
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=

testing/go/foreign_keys_test.go

Lines changed: 230 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,66 @@ func TestForeignKeys(t *testing.T) {
134134
},
135135
{
136136
Name: "foreign key in another schema with search path",
137-
Skip: true, // no GMS support for schemas in foreign key defns
138137
SetUpScript: []string{
139138
"create schema parent",
140139
"create schema child",
140+
"create schema fake",
141+
"call dolt_commit('-Am', 'create schemas')",
141142
"set search_path to 'parent, child'",
142-
"create table parent.parent (pk int, \"value\" int, primary key(pk));",
143+
`create table parent.parent (pk int, val int, primary key(pk));`,
144+
`create table fake.parent (pk int, val int, primary key(pk));`,
143145
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id), foreign key (test_pk) references parent(pk))",
146+
"INSERT INTO parent VALUES (0, 0), (1, 1), (2,2)",
147+
"SELECT DOLT_ADD('.')",
148+
},
149+
Assertions: []ScriptTestAssertion{
150+
{
151+
Query: "SELECT * FROM dolt_status",
152+
Expected: []sql.Row{
153+
{"child.child", 1, "new table"},
154+
{"fake.parent", 1, "new table"},
155+
{"parent.parent", 1, "new table"},
156+
},
157+
},
158+
{
159+
Query: "SELECT dolt_commit('-am', 'new tables')",
160+
SkipResultsCheck: true,
161+
},
162+
{
163+
Query: "SELECT * FROM dolt_status",
164+
Expected: []sql.Row{},
165+
},
166+
{
167+
Query: "SELECT * FROM dolt_schema_diff('HEAD', 'WORKING', 'child')",
168+
Expected: []sql.Row{},
169+
},
170+
{
171+
Query: "INSERT INTO child VALUES (2, 'two', 2)",
172+
Expected: []sql.Row{},
173+
},
174+
{
175+
Query: "INSERT INTO child VALUES (3, 'three', 3)",
176+
ExpectedErr: "Foreign key violation",
177+
},
178+
{
179+
Query: "SELECT * FROM child.child",
180+
Expected: []sql.Row{
181+
{2, "two", 2},
182+
},
183+
},
184+
},
185+
},
186+
{
187+
Name: "foreign key in another schema with search path, parent table not on search path",
188+
SetUpScript: []string{
189+
"create schema parent",
190+
"create schema child",
191+
"create schema fake",
192+
"call dolt_commit('-Am', 'create schemas')",
193+
"set search_path to 'child, fake'",
194+
`create table parent.parent (pk int, val int, primary key(pk));`,
195+
`create table fake.parent (pk int, val int, primary key(pk));`,
196+
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id), foreign key (test_pk) references parent.parent(pk))",
144197
"INSERT INTO parent.parent VALUES (0, 0), (1, 1), (2,2)",
145198
"SELECT DOLT_ADD('.')",
146199
},
@@ -149,6 +202,7 @@ func TestForeignKeys(t *testing.T) {
149202
Query: "SELECT * FROM dolt_status",
150203
Expected: []sql.Row{
151204
{"child.child", 1, "new table"},
205+
{"fake.parent", 1, "new table"},
152206
{"parent.parent", 1, "new table"},
153207
},
154208
},
@@ -165,11 +219,11 @@ func TestForeignKeys(t *testing.T) {
165219
Expected: []sql.Row{},
166220
},
167221
{
168-
Query: "INSERT INTO child.child VALUES (2, 'two', 2)",
222+
Query: "INSERT INTO child VALUES (2, 'two', 2)",
169223
Expected: []sql.Row{},
170224
},
171225
{
172-
Query: "INSERT INTO child.child VALUES (3, 'three', 3)",
226+
Query: "INSERT INTO child VALUES (3, 'three', 3)",
173227
ExpectedErr: "Foreign key violation",
174228
},
175229
{
@@ -181,12 +235,14 @@ func TestForeignKeys(t *testing.T) {
181235
},
182236
},
183237
{
184-
Name: "foreign key in another schema",
185-
Skip: true, // no GMS support for schemas in foreign key defns
238+
Name: "foreign key in another schema, no search path",
186239
SetUpScript: []string{
187240
"create schema parent",
188241
"create schema child",
189-
"create table parent.parent (pk int, \"value\" int, primary key(pk));",
242+
"create schema fake",
243+
"call dolt_commit('-Am', 'create schemas')",
244+
`create table parent.parent (pk int, val int, primary key(pk));`,
245+
`create table fake.parent (pk int, val int, primary key(pk));`,
190246
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id), foreign key (test_pk) references parent.parent(pk))",
191247
"INSERT INTO parent.parent VALUES (0, 0), (1, 1), (2,2)",
192248
"SELECT DOLT_ADD('.')",
@@ -196,6 +252,7 @@ func TestForeignKeys(t *testing.T) {
196252
Query: "SELECT * FROM dolt_status",
197253
Expected: []sql.Row{
198254
{"child.child", 1, "new table"},
255+
{"fake.parent", 1, "new table"},
199256
{"parent.parent", 1, "new table"},
200257
},
201258
},
@@ -227,6 +284,172 @@ func TestForeignKeys(t *testing.T) {
227284
},
228285
},
229286
},
287+
{
288+
Name: "add foreign key in another schema on search path",
289+
SetUpScript: []string{
290+
"create schema parent",
291+
"create schema child",
292+
"create schema fake",
293+
"call dolt_commit('-Am', 'create schemas')",
294+
"set search_path to 'child, parent'",
295+
`create table parent.parent (pk int, val int, primary key(pk));`,
296+
`create table fake.parent (pk int, val int, primary key(pk));`,
297+
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id))",
298+
"INSERT INTO parent.parent VALUES (0, 0), (1, 1), (2,2)",
299+
"SELECT DOLT_COMMIT('-Am', 'new tables')",
300+
},
301+
Assertions: []ScriptTestAssertion{
302+
{
303+
Query: "INSERT INTO child.child VALUES (2, 'two', 2)",
304+
Expected: []sql.Row{},
305+
},
306+
{
307+
Query: "ALTER TABLE child ADD FOREIGN KEY (test_pk) REFERENCES parent(pk)",
308+
SkipResultsCheck: true,
309+
},
310+
{
311+
Query: "INSERT INTO child VALUES (3, 'three', 3)",
312+
ExpectedErr: "Foreign key violation",
313+
},
314+
{
315+
Query: "SELECT * FROM child",
316+
Expected: []sql.Row{
317+
{2, "two", 2},
318+
},
319+
},
320+
},
321+
},
322+
{
323+
Name: "add foreign key in another schema, parent table not on search path",
324+
SetUpScript: []string{
325+
"create schema parent",
326+
"create schema child",
327+
"create schema fake",
328+
"call dolt_commit('-Am', 'create schemas')",
329+
"set search_path to 'child, fake'",
330+
`create table parent.parent (pk int, val int, primary key(pk));`,
331+
`create table fake.parent (pk int, val int, primary key(pk));`,
332+
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id))",
333+
"INSERT INTO parent.parent VALUES (0, 0), (1, 1), (2,2)",
334+
"SELECT DOLT_COMMIT('-Am', 'new tables')",
335+
},
336+
Assertions: []ScriptTestAssertion{
337+
{
338+
Query: "INSERT INTO child.child VALUES (2, 'two', 2)",
339+
Expected: []sql.Row{},
340+
},
341+
{
342+
Query: "ALTER TABLE child ADD FOREIGN KEY (test_pk) REFERENCES parent.parent(pk)",
343+
SkipResultsCheck: true,
344+
},
345+
{
346+
Query: "INSERT INTO child VALUES (3, 'three', 3)",
347+
ExpectedErr: "Foreign key violation",
348+
},
349+
{
350+
Query: "SELECT * FROM child",
351+
Expected: []sql.Row{
352+
{2, "two", 2},
353+
},
354+
},
355+
},
356+
},
357+
{
358+
Name: "add foreign key in another schema, no search path",
359+
SetUpScript: []string{
360+
"create schema parent",
361+
"create schema child",
362+
"create schema fake",
363+
"call dolt_commit('-Am', 'create schemas')",
364+
`create table parent.parent (pk int, val int, primary key(pk));`,
365+
`create table fake.parent (pk int, val int, primary key(pk));`,
366+
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id))",
367+
"INSERT INTO parent.parent VALUES (0, 0), (1, 1), (2,2)",
368+
"SELECT DOLT_COMMIT('-Am', 'new tables')",
369+
},
370+
Assertions: []ScriptTestAssertion{
371+
{
372+
Query: "INSERT INTO child.child VALUES (2, 'two', 2)",
373+
Expected: []sql.Row{},
374+
},
375+
{
376+
Query: "ALTER TABLE child.child ADD FOREIGN KEY (test_pk) REFERENCES parent.parent(pk)",
377+
SkipResultsCheck: true,
378+
},
379+
{
380+
Query: "INSERT INTO child.child VALUES (3, 'three', 3)",
381+
ExpectedErr: "Foreign key violation",
382+
},
383+
{
384+
Query: "SELECT * FROM child.child",
385+
Expected: []sql.Row{
386+
{2, "two", 2},
387+
},
388+
},
389+
},
390+
},
391+
{
392+
Name: "drop foreign key in another schema, on search path",
393+
SetUpScript: []string{
394+
"create schema parent",
395+
"create schema child",
396+
"create schema fake",
397+
"call dolt_commit('-Am', 'create schemas')",
398+
"set search_path to 'child, parent'",
399+
`create table parent.parent (pk int, val int, primary key(pk));`,
400+
`create table fake.parent (pk int, val int, primary key(pk));`,
401+
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id))",
402+
"INSERT INTO parent.parent VALUES (0, 0), (1, 1), (2,2)",
403+
"SELECT DOLT_COMMIT('-Am', 'new tables')",
404+
"INSERT INTO child.child VALUES (2, 'two', 2)",
405+
"ALTER TABLE child.child ADD FOREIGN KEY (test_pk) REFERENCES parent(pk)",
406+
},
407+
Assertions: []ScriptTestAssertion{
408+
{
409+
Query: "INSERT INTO child.child VALUES (3, 'three', 3)",
410+
ExpectedErr: "Foreign key violation",
411+
},
412+
{
413+
Query: "alter table child DROP constraint child_ibfk_1",
414+
SkipResultsCheck: true,
415+
},
416+
{
417+
Query: "INSERT INTO child.child VALUES (3, 'three', 3)",
418+
Expected: []sql.Row{},
419+
},
420+
},
421+
},
422+
{
423+
Name: "drop foreign key in another schema, no search path",
424+
Skip: true, // not getting the explicit schema name passed to the node
425+
SetUpScript: []string{
426+
"create schema parent",
427+
"create schema child",
428+
"create schema fake",
429+
"call dolt_commit('-Am', 'create schemas')",
430+
`create table parent.parent (pk int, val int, primary key(pk));`,
431+
`create table fake.parent (pk int, val int, primary key(pk));`,
432+
"CREATE TABLE child.child (id int, info varchar(255), test_pk int, primary key(id))",
433+
"INSERT INTO parent.parent VALUES (0, 0), (1, 1), (2,2)",
434+
"SELECT DOLT_COMMIT('-Am', 'new tables')",
435+
"INSERT INTO child.child VALUES (2, 'two', 2)",
436+
"ALTER TABLE child.child ADD FOREIGN KEY (test_pk) REFERENCES parent.parent(pk)",
437+
},
438+
Assertions: []ScriptTestAssertion{
439+
{
440+
Query: "INSERT INTO child.child VALUES (3, 'three', 3)",
441+
ExpectedErr: "Foreign key violation",
442+
},
443+
{
444+
Query: "alter table child.child DROP constraint child_ibfk_1",
445+
SkipResultsCheck: true,
446+
},
447+
{
448+
Query: "INSERT INTO child.child VALUES (3, 'three', 3)",
449+
Expected: []sql.Row{},
450+
},
451+
},
452+
},
230453
},
231454
)
232455
}

0 commit comments

Comments
 (0)