Skip to content

Commit

Permalink
Add a keyspace configuration in the vschema for foreign key mode (#…
Browse files Browse the repository at this point in the history
…13553)

Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 authored Jul 20, 2023
1 parent b7555de commit 79629a0
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 159 deletions.
321 changes: 198 additions & 123 deletions go/vt/proto/vschema/vschema.pb.go

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions go/vt/proto/vschema/vschema_vtproto.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 27 additions & 15 deletions go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,21 @@ func (col *Column) MarshalJSON() ([]byte, error) {

// KeyspaceSchema contains the schema(table) for a keyspace.
type KeyspaceSchema struct {
Keyspace *Keyspace
Tables map[string]*Table
Vindexes map[string]Vindex
Views map[string]sqlparser.SelectStatement
Error error
Keyspace *Keyspace
ForeignKeyMode vschemapb.Keyspace_ForeignKeyMode
Tables map[string]*Table
Vindexes map[string]Vindex
Views map[string]sqlparser.SelectStatement
Error error
}

type ksJSON struct {
Sharded bool `json:"sharded,omitempty"`
Tables map[string]*Table `json:"tables,omitempty"`
Vindexes map[string]Vindex `json:"vindexes,omitempty"`
Views map[string]string `json:"views,omitempty"`
Error string `json:"error,omitempty"`
Sharded bool `json:"sharded,omitempty"`
ForeignKeyMode string `json:"foreignKeyMode,omitempty"`
Tables map[string]*Table `json:"tables,omitempty"`
Vindexes map[string]Vindex `json:"vindexes,omitempty"`
Views map[string]string `json:"views,omitempty"`
Error string `json:"error,omitempty"`
}

// findTable looks for the table with the requested tablename in the keyspace.
Expand Down Expand Up @@ -216,9 +218,10 @@ func (ks *KeyspaceSchema) findTable(
// MarshalJSON returns a JSON representation of KeyspaceSchema.
func (ks *KeyspaceSchema) MarshalJSON() ([]byte, error) {
ksJ := ksJSON{
Sharded: ks.Keyspace.Sharded,
Tables: ks.Tables,
Vindexes: ks.Vindexes,
Sharded: ks.Keyspace.Sharded,
Tables: ks.Tables,
ForeignKeyMode: ks.ForeignKeyMode.String(),
Vindexes: ks.Vindexes,
}
if ks.Error != nil {
ksJ.Error = ks.Error.Error()
Expand Down Expand Up @@ -304,14 +307,23 @@ func buildKeyspaces(source *vschemapb.SrvVSchema, vschema *VSchema) {
Name: ksname,
Sharded: ks.Sharded,
},
Tables: make(map[string]*Table),
Vindexes: make(map[string]Vindex),
ForeignKeyMode: replaceDefaultForeignKeyMode(ks.ForeignKeyMode),
Tables: make(map[string]*Table),
Vindexes: make(map[string]Vindex),
}
vschema.Keyspaces[ksname] = ksvschema
ksvschema.Error = buildTables(ks, vschema, ksvschema)
}
}

// replaceDefaultForeignKeyMode replaces the default value of the foreign key mode enum with the default we want to keep.
func replaceDefaultForeignKeyMode(fkMode vschemapb.Keyspace_ForeignKeyMode) vschemapb.Keyspace_ForeignKeyMode {
if fkMode == vschemapb.Keyspace_FK_DEFAULT {
return vschemapb.Keyspace_FK_UNMANAGED
}
return fkMode
}

func (vschema *VSchema) AddView(ksname string, viewName, query string) error {
ks, ok := vschema.Keyspaces[ksname]
if !ok {
Expand Down
Loading

0 comments on commit 79629a0

Please sign in to comment.