@@ -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