@@ -536,9 +536,15 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
536536 r .Exec (t , "CREATE TABLE child2 (pk INT PRIMARY KEY, fk INT REFERENCES parent(pk));" )
537537 r .Exec (t , "CREATE TABLE grandchild1 (pk INT PRIMARY KEY, fk INT REFERENCES child1(pk));" )
538538 r .Exec (t , "CREATE TABLE grandchild2 (pk INT PRIMARY KEY, fk INT REFERENCES child2(pk));" )
539+ getFK := func (table string ) string {
540+ if table == "parent" {
541+ return ""
542+ }
543+ return fmt .Sprintf ("ALTER TABLE defaultdb.public.%s ADD CONSTRAINT" , table )
544+ }
539545 // Only the target tables should be included since we perform a
540546 // read-only stmt.
541- getContentCheckFn := func (targetTableNames , targetFKs []string ) func (name , contents string ) error {
547+ getContentCheckFn := func (targetTableNames , addFKs , skipFKs []string ) func (name , contents string ) error {
542548 return func (name , contents string ) error {
543549 if name == "schema.sql" {
544550 for _ , targetTableName := range targetTableNames {
@@ -563,14 +569,27 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
563569 "unexpectedly found non-target table 'USE defaultdb;\n CREATE TABLE public.%s' in schema.sql:\n %s" , tableName , contents )
564570 }
565571 }
566- // Now confirm that only relevant FKs are included.
572+ // Sanity-check that all FKs in the output are either in the
573+ // "added" or "skipped" set.
567574 numFoundFKs := strings .Count (contents , "FOREIGN KEY" )
568- if numFoundFKs != len (targetFKs ) {
569- return errors .Newf ("found %d FKs, expected %d\n %s" , numFoundFKs , len (targetFKs ), contents )
575+ if numFoundFKs != len (addFKs )+ len (skipFKs ) {
576+ return errors .Newf (
577+ "found %d FKs total whereas %d added and %d skipped were passed\n %s" ,
578+ numFoundFKs , len (addFKs ), len (skipFKs ), contents ,
579+ )
580+ }
581+ // Now check that all expected added and skipped FKs are
582+ // present.
583+ for _ , addFK := range addFKs {
584+ if ! strings .Contains (contents , addFK ) {
585+ return errors .Newf ("didn't find added FK: %s\n %s" , addFK , contents )
586+ } else if strings .Contains (contents , "-- " + addFK ) {
587+ return errors .Newf ("added FK shouldn't be commented out: %s\n %s" , addFK , contents )
588+ }
570589 }
571- for _ , fk := range targetFKs {
572- if ! strings .Contains (contents , fk ) {
573- return errors .Newf ("didn't find target FK: %s\n %s" , fk , contents )
590+ for _ , skipFK := range skipFKs {
591+ if ! strings .Contains (contents , "-- " + skipFK ) {
592+ return errors .Newf ("didn't find skipped FK in commented out form : %s\n %s" , skipFK , contents )
574593 }
575594 }
576595 }
@@ -580,8 +599,12 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
580599 // First read each table separately.
581600 for _ , tableName := range tableNames {
582601 targetTableName := tableName
583- // There should be no FKs included.
584- contentCheck := getContentCheckFn ([]string {targetTableName }, nil /* targetFKs */ )
602+ // No FKs should be added, but 1 FK might be skipped.
603+ var skipFKs []string
604+ if skipFK := getFK (tableName ); skipFK != "" {
605+ skipFKs = []string {skipFK }
606+ }
607+ contentCheck := getContentCheckFn ([]string {targetTableName }, nil /* addFKS */ , skipFKs )
585608 rows := r .QueryStr (t , "EXPLAIN ANALYZE (DEBUG) SELECT * FROM " + targetTableName )
586609 checkBundle (
587610 t , fmt .Sprint (rows ), targetTableName , contentCheck , false , /* expectErrors */
@@ -590,26 +613,27 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
590613 )
591614 }
592615 // Now read different combinations of tables which will influence
593- // whether ADD CONSTRAINT ... FOREIGN KEY statements are included.
594- contentCheck := getContentCheckFn ([]string {"parent" , "child1" }, []string {"ALTER TABLE defaultdb.public.child1 ADD CONSTRAINT" })
616+ // whether ADD CONSTRAINT ... FOREIGN KEY statements are added or
617+ // skipped.
618+ contentCheck := getContentCheckFn ([]string {"parent" , "child1" }, []string {getFK ("child1" )}, nil )
595619 rows := r .QueryStr (t , "EXPLAIN ANALYZE (DEBUG) SELECT * FROM parent, child1" )
596620 checkBundle (
597621 t , fmt .Sprint (rows ), "parent" , contentCheck , false , /* expectErrors */
598622 base , plans , "stats-defaultdb.public.parent.sql stats-defaultdb.public.child1.sql distsql.html vec.txt vec-v.txt" ,
599623 )
600624
601- // There should be no FKs since there isn't a direct link between the
602- // tables.
603- contentCheck = getContentCheckFn ([]string {"parent" , "grandchild1" }, nil /* targetFKs */ )
625+ // There should be no added FKs since there isn't a direct link between
626+ // the tables.
627+ contentCheck = getContentCheckFn ([]string {"parent" , "grandchild1" }, nil /* addFKS */ , [] string { getFK ( "grandchild1" )} )
604628 rows = r .QueryStr (t , "EXPLAIN ANALYZE (DEBUG) SELECT * FROM parent, grandchild1" )
605629 checkBundle (
606630 t , fmt .Sprint (rows ), "parent" , contentCheck , false , /* expectErrors */
607631 base , plans , "stats-defaultdb.public.parent.sql stats-defaultdb.public.grandchild1.sql distsql.html vec.txt vec-v.txt" ,
608632 )
609633
610- // Note that we omit the FK from grandchild1 since the FK referenced
634+ // Note that we skip the FK from grandchild1 since the FK referenced
611635 // table isn't being read.
612- contentCheck = getContentCheckFn ([]string {"parent" , "child2" , "grandchild1" }, []string {"ALTER TABLE defaultdb.public. child2 ADD CONSTRAINT" })
636+ contentCheck = getContentCheckFn ([]string {"parent" , "child2" , "grandchild1" }, []string {getFK ( " child2" )}, [] string { getFK ( "grandchild1" ) })
613637 rows = r .QueryStr (t , "EXPLAIN ANALYZE (DEBUG) SELECT * FROM parent, child2, grandchild1" )
614638 checkBundle (
615639 t , fmt .Sprint (rows ), "parent" , contentCheck , false , /* expectErrors */
@@ -618,10 +642,9 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
618642
619643 contentCheck = getContentCheckFn (
620644 []string {"parent" , "child1" , "grandchild1" },
621- []string {
622- "ALTER TABLE defaultdb.public.child1 ADD CONSTRAINT" ,
623- "ALTER TABLE defaultdb.public.grandchild1 ADD CONSTRAINT" ,
624- })
645+ []string {getFK ("child1" ), getFK ("grandchild1" )},
646+ nil , /* skipFKs */
647+ )
625648 rows = r .QueryStr (t , "EXPLAIN ANALYZE (DEBUG) SELECT * FROM parent, child1, grandchild1" )
626649 checkBundle (
627650 t , fmt .Sprint (rows ), "parent" , contentCheck , false , /* expectErrors */
0 commit comments