@@ -372,6 +372,100 @@ func TestRebuildVSchema(t *testing.T) {
372
372
}
373
373
}
374
374
375
+ // TestVSchemaUpdateWithFKReferenceToInternalTables tests that any internal table as part of fk reference is ignored.
376
+ func TestVSchemaUpdateWithFKReferenceToInternalTables (t * testing.T ) {
377
+ ks := & vindexes.Keyspace {Name : "ks" }
378
+ cols1 := []vindexes.Column {{
379
+ Name : sqlparser .NewIdentifierCI ("id" ),
380
+ Type : querypb .Type_INT64 ,
381
+ }}
382
+ sqlparserCols1 := sqlparser .MakeColumns ("id" )
383
+
384
+ vindexTable_t1 := & vindexes.Table {
385
+ Name : sqlparser .NewIdentifierCS ("t1" ),
386
+ Keyspace : ks ,
387
+ Columns : cols1 ,
388
+ ColumnListAuthoritative : true ,
389
+ }
390
+ vindexTable_t2 := & vindexes.Table {
391
+ Name : sqlparser .NewIdentifierCS ("t2" ),
392
+ Keyspace : ks ,
393
+ Columns : cols1 ,
394
+ ColumnListAuthoritative : true ,
395
+ }
396
+
397
+ vindexTable_t1 .ChildForeignKeys = append (vindexTable_t1 .ChildForeignKeys , vindexes.ChildFKInfo {
398
+ Table : vindexTable_t2 ,
399
+ ChildColumns : sqlparserCols1 ,
400
+ ParentColumns : sqlparserCols1 ,
401
+ OnDelete : sqlparser .SetNull ,
402
+ OnUpdate : sqlparser .Cascade ,
403
+ })
404
+ vindexTable_t2 .ParentForeignKeys = append (vindexTable_t2 .ParentForeignKeys , vindexes.ParentFKInfo {
405
+ Table : vindexTable_t1 ,
406
+ ChildColumns : sqlparserCols1 ,
407
+ ParentColumns : sqlparserCols1 ,
408
+ })
409
+
410
+ vm := & VSchemaManager {}
411
+ var vs * vindexes.VSchema
412
+ vm .subscriber = func (vschema * vindexes.VSchema , _ * VSchemaStats ) {
413
+ vs = vschema
414
+ vs .ResetCreated ()
415
+ }
416
+ vm .schema = & fakeSchema {t : map [string ]* vindexes.TableInfo {
417
+ "t1" : {Columns : cols1 },
418
+ "t2" : {
419
+ Columns : cols1 ,
420
+ ForeignKeys : []* sqlparser.ForeignKeyDefinition {
421
+ createFkDefinition ([]string {"id" }, "t1" , []string {"id" }, sqlparser .Cascade , sqlparser .SetNull ),
422
+ createFkDefinition ([]string {"id" }, "_vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410" , []string {"id" }, sqlparser .Cascade , sqlparser .SetNull ),
423
+ },
424
+ },
425
+ }}
426
+ vm .VSchemaUpdate (& vschemapb.SrvVSchema {
427
+ Keyspaces : map [string ]* vschemapb.Keyspace {
428
+ "ks" : {
429
+ ForeignKeyMode : vschemapb .Keyspace_managed ,
430
+ Tables : map [string ]* vschemapb.Table {
431
+ "t1" : {Columns : []* vschemapb.Column {{Name : "id" , Type : querypb .Type_INT64 }}},
432
+ "t2" : {Columns : []* vschemapb.Column {{Name : "id" , Type : querypb .Type_INT64 }}},
433
+ },
434
+ },
435
+ },
436
+ }, nil )
437
+
438
+ utils .MustMatchFn (".globalTables" , ".uniqueVindexes" )(t , & vindexes.VSchema {
439
+ RoutingRules : map [string ]* vindexes.RoutingRule {},
440
+ Keyspaces : map [string ]* vindexes.KeyspaceSchema {
441
+ "ks" : {
442
+ Keyspace : ks ,
443
+ ForeignKeyMode : vschemapb .Keyspace_managed ,
444
+ Vindexes : map [string ]vindexes.Vindex {},
445
+ Tables : map [string ]* vindexes.Table {
446
+ "t1" : vindexTable_t1 ,
447
+ "t2" : vindexTable_t2 ,
448
+ },
449
+ },
450
+ },
451
+ }, vs )
452
+ utils .MustMatch (t , vs , vm .currentVschema , "currentVschema should have same reference as Vschema" )
453
+ }
454
+
455
+ // createFkDefinition is a helper function to create a Foreign key definition struct from the columns used in it provided as list of strings.
456
+ func createFkDefinition (childCols []string , parentTableName string , parentCols []string , onUpdate , onDelete sqlparser.ReferenceAction ) * sqlparser.ForeignKeyDefinition {
457
+ pKs , pTbl , _ := sqlparser .ParseTable (parentTableName )
458
+ return & sqlparser.ForeignKeyDefinition {
459
+ Source : sqlparser .MakeColumns (childCols ... ),
460
+ ReferenceDefinition : & sqlparser.ReferenceDefinition {
461
+ ReferencedTable : sqlparser .NewTableNameWithQualifier (pTbl , pKs ),
462
+ ReferencedColumns : sqlparser .MakeColumns (parentCols ... ),
463
+ OnUpdate : onUpdate ,
464
+ OnDelete : onDelete ,
465
+ },
466
+ }
467
+ }
468
+
375
469
func makeTestVSchema (ks string , sharded bool , tbls map [string ]* vindexes.Table ) * vindexes.VSchema {
376
470
keyspaceSchema := & vindexes.KeyspaceSchema {
377
471
Keyspace : & vindexes.Keyspace {
0 commit comments