Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a keyspace configuration in the vschema for foreign key mode #13553

Merged
merged 7 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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