@@ -438,56 +438,6 @@ func TestRebuildVSchema(t *testing.T) {
438
438
}
439
439
}
440
440
441
- func makeTestVSchema (ks string , sharded bool , tbls map [string ]* vindexes.Table ) * vindexes.VSchema {
442
- keyspaceSchema := & vindexes.KeyspaceSchema {
443
- Keyspace : & vindexes.Keyspace {
444
- Name : ks ,
445
- Sharded : sharded ,
446
- },
447
- // Default foreign key mode
448
- ForeignKeyMode : vschemapb .Keyspace_unmanaged ,
449
- Tables : tbls ,
450
- Vindexes : map [string ]vindexes.Vindex {},
451
- }
452
- vs := makeTestEmptyVSchema ()
453
- vs .Keyspaces [ks ] = keyspaceSchema
454
- vs .ResetCreated ()
455
- return vs
456
- }
457
-
458
- func makeTestEmptyVSchema () * vindexes.VSchema {
459
- return & vindexes.VSchema {
460
- RoutingRules : map [string ]* vindexes.RoutingRule {},
461
- Keyspaces : map [string ]* vindexes.KeyspaceSchema {},
462
- }
463
- }
464
-
465
- func makeTestSrvVSchema (ks string , sharded bool , tbls map [string ]* vschemapb.Table ) * vschemapb.SrvVSchema {
466
- keyspaceSchema := & vschemapb.Keyspace {
467
- Sharded : sharded ,
468
- Tables : tbls ,
469
- // Default foreign key mode
470
- ForeignKeyMode : vschemapb .Keyspace_unmanaged ,
471
- }
472
- return & vschemapb.SrvVSchema {
473
- Keyspaces : map [string ]* vschemapb.Keyspace {ks : keyspaceSchema },
474
- }
475
- }
476
-
477
- type fakeSchema struct {
478
- t map [string ]* vindexes.TableInfo
479
- }
480
-
481
- func (f * fakeSchema ) Tables (string ) map [string ]* vindexes.TableInfo {
482
- return f .t
483
- }
484
-
485
- func (f * fakeSchema ) Views (string ) map [string ]sqlparser.SelectStatement {
486
- return nil
487
- }
488
-
489
- var _ SchemaInfo = (* fakeSchema )(nil )
490
-
491
441
func TestMarkErrorIfCyclesInFk (t * testing.T ) {
492
442
ksName := "ks"
493
443
keyspace := & vindexes.Keyspace {
@@ -573,6 +523,86 @@ func TestMarkErrorIfCyclesInFk(t *testing.T) {
573
523
}
574
524
}
575
525
526
+ // TestVSchemaUpdateWithFKReferenceToInternalTables tests that any internal table as part of fk reference is ignored.
527
+ func TestVSchemaUpdateWithFKReferenceToInternalTables (t * testing.T ) {
528
+ ks := & vindexes.Keyspace {Name : "ks" }
529
+ cols1 := []vindexes.Column {{
530
+ Name : sqlparser .NewIdentifierCI ("id" ),
531
+ Type : querypb .Type_INT64 ,
532
+ }}
533
+ sqlparserCols1 := sqlparser .MakeColumns ("id" )
534
+
535
+ vindexTable_t1 := & vindexes.Table {
536
+ Name : sqlparser .NewIdentifierCS ("t1" ),
537
+ Keyspace : ks ,
538
+ Columns : cols1 ,
539
+ ColumnListAuthoritative : true ,
540
+ }
541
+ vindexTable_t2 := & vindexes.Table {
542
+ Name : sqlparser .NewIdentifierCS ("t2" ),
543
+ Keyspace : ks ,
544
+ Columns : cols1 ,
545
+ ColumnListAuthoritative : true ,
546
+ }
547
+
548
+ vindexTable_t1 .ChildForeignKeys = append (vindexTable_t1 .ChildForeignKeys , vindexes.ChildFKInfo {
549
+ Table : vindexTable_t2 ,
550
+ ChildColumns : sqlparserCols1 ,
551
+ ParentColumns : sqlparserCols1 ,
552
+ OnDelete : sqlparser .SetNull ,
553
+ OnUpdate : sqlparser .Cascade ,
554
+ })
555
+ vindexTable_t2 .ParentForeignKeys = append (vindexTable_t2 .ParentForeignKeys , vindexes.ParentFKInfo {
556
+ Table : vindexTable_t1 ,
557
+ ChildColumns : sqlparserCols1 ,
558
+ ParentColumns : sqlparserCols1 ,
559
+ })
560
+
561
+ vm := & VSchemaManager {}
562
+ var vs * vindexes.VSchema
563
+ vm .subscriber = func (vschema * vindexes.VSchema , _ * VSchemaStats ) {
564
+ vs = vschema
565
+ vs .ResetCreated ()
566
+ }
567
+ vm .schema = & fakeSchema {t : map [string ]* vindexes.TableInfo {
568
+ "t1" : {Columns : cols1 },
569
+ "t2" : {
570
+ Columns : cols1 ,
571
+ ForeignKeys : []* sqlparser.ForeignKeyDefinition {
572
+ createFkDefinition ([]string {"id" }, "t1" , []string {"id" }, sqlparser .Cascade , sqlparser .SetNull ),
573
+ createFkDefinition ([]string {"id" }, "_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410" , []string {"id" }, sqlparser .Cascade , sqlparser .SetNull ),
574
+ },
575
+ },
576
+ }}
577
+ vm .VSchemaUpdate (& vschemapb.SrvVSchema {
578
+ Keyspaces : map [string ]* vschemapb.Keyspace {
579
+ "ks" : {
580
+ ForeignKeyMode : vschemapb .Keyspace_managed ,
581
+ Tables : map [string ]* vschemapb.Table {
582
+ "t1" : {Columns : []* vschemapb.Column {{Name : "id" , Type : querypb .Type_INT64 }}},
583
+ "t2" : {Columns : []* vschemapb.Column {{Name : "id" , Type : querypb .Type_INT64 }}},
584
+ },
585
+ },
586
+ },
587
+ }, nil )
588
+
589
+ utils .MustMatchFn (".globalTables" , ".uniqueVindexes" )(t , & vindexes.VSchema {
590
+ RoutingRules : map [string ]* vindexes.RoutingRule {},
591
+ Keyspaces : map [string ]* vindexes.KeyspaceSchema {
592
+ "ks" : {
593
+ Keyspace : ks ,
594
+ ForeignKeyMode : vschemapb .Keyspace_managed ,
595
+ Vindexes : map [string ]vindexes.Vindex {},
596
+ Tables : map [string ]* vindexes.Table {
597
+ "t1" : vindexTable_t1 ,
598
+ "t2" : vindexTable_t2 ,
599
+ },
600
+ },
601
+ },
602
+ }, vs )
603
+ utils .MustMatch (t , vs , vm .currentVschema , "currentVschema should have same reference as Vschema" )
604
+ }
605
+
576
606
// createFkDefinition is a helper function to create a Foreign key definition struct from the columns used in it provided as list of strings.
577
607
func createFkDefinition (childCols []string , parentTableName string , parentCols []string , onUpdate , onDelete sqlparser.ReferenceAction ) * sqlparser.ForeignKeyDefinition {
578
608
pKs , pTbl , _ := sqlparser .NewTestParser ().ParseTable (parentTableName )
@@ -586,3 +616,53 @@ func createFkDefinition(childCols []string, parentTableName string, parentCols [
586
616
},
587
617
}
588
618
}
619
+
620
+ func makeTestVSchema (ks string , sharded bool , tbls map [string ]* vindexes.Table ) * vindexes.VSchema {
621
+ keyspaceSchema := & vindexes.KeyspaceSchema {
622
+ Keyspace : & vindexes.Keyspace {
623
+ Name : ks ,
624
+ Sharded : sharded ,
625
+ },
626
+ // Default foreign key mode
627
+ ForeignKeyMode : vschemapb .Keyspace_unmanaged ,
628
+ Tables : tbls ,
629
+ Vindexes : map [string ]vindexes.Vindex {},
630
+ }
631
+ vs := makeTestEmptyVSchema ()
632
+ vs .Keyspaces [ks ] = keyspaceSchema
633
+ vs .ResetCreated ()
634
+ return vs
635
+ }
636
+
637
+ func makeTestEmptyVSchema () * vindexes.VSchema {
638
+ return & vindexes.VSchema {
639
+ RoutingRules : map [string ]* vindexes.RoutingRule {},
640
+ Keyspaces : map [string ]* vindexes.KeyspaceSchema {},
641
+ }
642
+ }
643
+
644
+ func makeTestSrvVSchema (ks string , sharded bool , tbls map [string ]* vschemapb.Table ) * vschemapb.SrvVSchema {
645
+ keyspaceSchema := & vschemapb.Keyspace {
646
+ Sharded : sharded ,
647
+ Tables : tbls ,
648
+ // Default foreign key mode
649
+ ForeignKeyMode : vschemapb .Keyspace_unmanaged ,
650
+ }
651
+ return & vschemapb.SrvVSchema {
652
+ Keyspaces : map [string ]* vschemapb.Keyspace {ks : keyspaceSchema },
653
+ }
654
+ }
655
+
656
+ type fakeSchema struct {
657
+ t map [string ]* vindexes.TableInfo
658
+ }
659
+
660
+ func (f * fakeSchema ) Tables (string ) map [string ]* vindexes.TableInfo {
661
+ return f .t
662
+ }
663
+
664
+ func (f * fakeSchema ) Views (string ) map [string ]sqlparser.SelectStatement {
665
+ return nil
666
+ }
667
+
668
+ var _ SchemaInfo = (* fakeSchema )(nil )
0 commit comments