Skip to content

Commit

Permalink
feat: rebuild global tables after schema has augmented the vschema
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 committed Aug 1, 2024
1 parent 6e54e70 commit 4bad8e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 8 additions & 3 deletions go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,9 @@ func BuildVSchema(source *vschemapb.SrvVSchema, parser *sqlparser.Parser) (vsche
created: time.Now(),
}
buildKeyspaces(source, vschema, parser)
// buildGlobalTables before buildReferences so that buildReferences can
// BuildGlobalTables before buildReferences so that buildReferences can
// resolve sources which reference global tables.
buildGlobalTables(source, vschema)
BuildGlobalTables(source, vschema)
buildReferences(source, vschema)
buildRoutingRule(source, vschema, parser)
buildShardRoutingRule(source, vschema)
Expand Down Expand Up @@ -437,7 +437,12 @@ func (vschema *VSchema) AddUDF(ksname, udfName string) error {
return nil
}

func buildGlobalTables(source *vschemapb.SrvVSchema, vschema *VSchema) {
// BuildGlobalTables rebuilds the global tables map that is used for global routing.
// If a table name is unique across all keyspaces, then Vitess allows the users to use the table
// without needing to select a database first.
func BuildGlobalTables(source *vschemapb.SrvVSchema, vschema *VSchema) {
// Reinitialize the global Tables map to restart the building process.
vschema.globalTables = make(map[string]*Table)
for ksname, ks := range source.Keyspaces {
ksvschema := vschema.Keyspaces[ksname]
// If the keyspace requires explicit routing, don't include any of
Expand Down
2 changes: 2 additions & 0 deletions go/vt/vtgate/vschema_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ func (vm *VSchemaManager) buildAndEnhanceVSchema(v *vschemapb.SrvVSchema) *vinde
// We mark the keyspaces that have foreign key management in Vitess and have cyclic foreign keys
// to have an error. This makes all queries against them to fail.
markErrorIfCyclesInFk(vschema)
// We build the global routing table again because schema tracking might have added more tables to the vschema.
vindexes.BuildGlobalTables(v, vschema)
}
return vschema
}
Expand Down

0 comments on commit 4bad8e5

Please sign in to comment.