Skip to content

Commit

Permalink
Rename Foreign Key enum values in VSchema and drop FK_ prefix (#14274)
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 authored Oct 17, 2023
1 parent e7aaa5b commit a5e819d
Show file tree
Hide file tree
Showing 22 changed files with 199 additions and 199 deletions.
6 changes: 3 additions & 3 deletions changelog/18.0/18.0.0/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ It is disabled by default.
A new field `foreignKeyMode` has been added to the VSchema. This field can be provided for each keyspace. The VTGate flag `--foreign_key_mode` has been deprecated in favour of this field.

There are 3 foreign key modes now supported in Vitess -
1. `FK_UNMANAGED` -
1. `unmanaged` -
This mode represents the default behaviour in Vitess, where it does not manage foreign keys column references. Users are responsible for configuring foreign keys in MySQL in such a way that related rows, as determined by foreign keys, reside within the same shard.
2. `FK_MANAGED` [EXPERIMENTAL] -
2. `managed` [EXPERIMENTAL] -
In this experimental mode, Vitess is fully aware of foreign key relationships and actively tracks foreign key constraints using the schema tracker. Vitess takes charge of handling DML operations with foreign keys cascading updates, deletes and verifying restrict. It will also validate parent row existence.
This ensures that all the operations are logged in binary logs, unlike MySQL implementation of foreign keys.
This enables seamless integration of VReplication with foreign keys.
For more details on what operations Vitess takes please refer to the [design document for foreign keys](https://github.com/vitessio/vitess/issues/12967).
3. `FK_DISALLOW` -
3. `disallow` -
In this mode Vitess explicitly disallows any DDL statements that try to create a foreign key constraint. This mode is equivalent to running VTGate with the flag `--foreign_key_mode=disallow`.

#### Upgrade process
Expand Down
6 changes: 3 additions & 3 deletions changelog/18.0/18.0.0/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ It is disabled by default.
A new field `foreignKeyMode` has been added to the VSchema. This field can be provided for each keyspace. The VTGate flag `--foreign_key_mode` has been deprecated in favour of this field.

There are 3 foreign key modes now supported in Vitess -
1. `FK_UNMANAGED` -
1. `unmanaged` -
This mode represents the default behaviour in Vitess, where it does not manage foreign keys column references. Users are responsible for configuring foreign keys in MySQL in such a way that related rows, as determined by foreign keys, reside within the same shard.
2. `FK_MANAGED` [EXPERIMENTAL] -
2. `managed` [EXPERIMENTAL] -
In this experimental mode, Vitess is fully aware of foreign key relationships and actively tracks foreign key constraints using the schema tracker. Vitess takes charge of handling DML operations with foreign keys cascading updates, deletes and verifying restrict. It will also validate parent row existence.
This ensures that all the operations are logged in binary logs, unlike MySQL implementation of foreign keys.
This enables seamless integration of VReplication with foreign keys.
For more details on what operations Vitess takes please refer to the [design document for foreign keys](https://github.com/vitessio/vitess/issues/12967).
3. `FK_DISALLOW` -
3. `disallow` -
In this mode Vitess explicitly disallows any DDL statements that try to create a foreign key constraint. This mode is equivalent to running VTGate with the flag `--foreign_key_mode=disallow`.

#### Upgrade process
Expand Down
2 changes: 1 addition & 1 deletion go/test/endtoend/vtgate/foreignkey/sharded_vschema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sharded": true,
"foreignKeyMode": "FK_MANAGED",
"foreignKeyMode": "managed",
"vindexes": {
"xxhash": {
"type": "xxhash"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func TestMain(m *testing.M) {
Name: keyspaceName,
VSchema: `{
"sharded": false,
"foreignKeyMode": "FK_MANAGED"
"foreignKeyMode": "managed"
}`,
}

Expand Down
2 changes: 1 addition & 1 deletion go/test/endtoend/vtgate/foreignkey/unsharded_vschema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sharded": false,
"foreignKeyMode": "FK_MANAGED",
"foreignKeyMode": "managed",
"tables": {
"u_t1": {},
"u_t2": {},
Expand Down
4 changes: 2 additions & 2 deletions go/test/vschemawrapper/vschema_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func (vw *VSchemaWrapper) PlannerWarning(_ string) {
}

func (vw *VSchemaWrapper) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) {
defaultFkMode := vschemapb.Keyspace_FK_UNMANAGED
if vw.V.Keyspaces[keyspace] != nil && vw.V.Keyspaces[keyspace].ForeignKeyMode != vschemapb.Keyspace_FK_DEFAULT {
defaultFkMode := vschemapb.Keyspace_unmanaged
if vw.V.Keyspaces[keyspace] != nil && vw.V.Keyspaces[keyspace].ForeignKeyMode != vschemapb.Keyspace_unspecified {
return vw.V.Keyspaces[keyspace].ForeignKeyMode, nil
}
return defaultFkMode, nil
Expand Down
192 changes: 96 additions & 96 deletions go/vt/proto/vschema/vschema.pb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go/vt/schemadiff/semantics.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (si *declarativeSchemaInformation) ConnCollation() collations.ID {
}

func (si *declarativeSchemaInformation) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) {
return vschemapb.Keyspace_FK_UNMANAGED, nil
return vschemapb.Keyspace_unmanaged, nil
}

// addTable adds a fake table with an empty column list
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func checkFKError(vschema plancontext.VSchema, ddlStatement sqlparser.DDLStateme
if err != nil {
return err
}
if fkMode == vschemapb.Keyspace_FK_DISALLOW {
if fkMode == vschemapb.Keyspace_disallow {
fk := &fkContraint{}
_ = sqlparser.Walk(fk.FkWalk, ddlStatement)
if fk.found {
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func createOperatorFromInsert(ctx *plancontext.PlanningContext, ins *sqlparser.I
if err != nil {
return nil, err
}
if ksMode != vschemapb.Keyspace_FK_MANAGED {
if ksMode != vschemapb.Keyspace_managed {
return insOp, nil
}

Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/testdata/vschemas/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@
},
"sharded_fk_allow": {
"sharded": true,
"foreignKeyMode": "FK_MANAGED",
"foreignKeyMode": "managed",
"vindexes": {
"hash_vin": {
"type": "hash_test"
Expand Down Expand Up @@ -746,7 +746,7 @@
}
},
"unsharded_fk_allow": {
"foreignKeyMode": "FK_MANAGED",
"foreignKeyMode": "managed",
"tables": {
"u_tbl1": {},
"u_tbl2": {},
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/semantics/FakeSI.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (s *FakeSI) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyM
if s.KsForeignKeyMode != nil {
fkMode, isPresent := s.KsForeignKeyMode[keyspace]
if !isPresent {
return vschemapb.Keyspace_FK_DEFAULT, fmt.Errorf("%v keyspace not found", keyspace)
return vschemapb.Keyspace_unspecified, fmt.Errorf("%v keyspace not found", keyspace)
}
return fkMode, nil
}
return vschemapb.Keyspace_FK_UNMANAGED, nil
return vschemapb.Keyspace_unmanaged, nil
}
2 changes: 1 addition & 1 deletion go/vt/vtgate/semantics/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ func (a *analyzer) getAllManagedForeignKeys() (map[TableSet][]vindexes.ChildFKIn
if err != nil {
return nil, nil, err
}
if fkMode != vschemapb.Keyspace_FK_MANAGED {
if fkMode != vschemapb.Keyspace_managed {
continue
}

Expand Down
28 changes: 14 additions & 14 deletions go/vt/vtgate/semantics/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1574,8 +1574,8 @@ func TestGetAllManagedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks_unmanaged": vschemapb.Keyspace_FK_UNMANAGED,
"ks": vschemapb.Keyspace_managed,
"ks_unmanaged": vschemapb.Keyspace_unmanaged,
},
},
},
Expand Down Expand Up @@ -1603,7 +1603,7 @@ func TestGetAllManagedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks": vschemapb.Keyspace_managed,
},
},
},
Expand Down Expand Up @@ -1752,8 +1752,8 @@ func TestGetInvolvedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks_unmanaged": vschemapb.Keyspace_FK_UNMANAGED,
"ks": vschemapb.Keyspace_managed,
"ks_unmanaged": vschemapb.Keyspace_unmanaged,
},
},
},
Expand Down Expand Up @@ -1837,7 +1837,7 @@ func TestGetInvolvedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks": vschemapb.Keyspace_managed,
},
},
},
Expand Down Expand Up @@ -1875,8 +1875,8 @@ func TestGetInvolvedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks_unmanaged": vschemapb.Keyspace_FK_UNMANAGED,
"ks": vschemapb.Keyspace_managed,
"ks_unmanaged": vschemapb.Keyspace_unmanaged,
},
},
},
Expand Down Expand Up @@ -1907,8 +1907,8 @@ func TestGetInvolvedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks_unmanaged": vschemapb.Keyspace_FK_UNMANAGED,
"ks": vschemapb.Keyspace_managed,
"ks_unmanaged": vschemapb.Keyspace_unmanaged,
},
},
},
Expand Down Expand Up @@ -1966,8 +1966,8 @@ func TestGetInvolvedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks_unmanaged": vschemapb.Keyspace_FK_UNMANAGED,
"ks": vschemapb.Keyspace_managed,
"ks_unmanaged": vschemapb.Keyspace_unmanaged,
},
},
},
Expand Down Expand Up @@ -1996,7 +1996,7 @@ func TestGetInvolvedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks": vschemapb.Keyspace_managed,
},
},
},
Expand All @@ -2014,7 +2014,7 @@ func TestGetInvolvedForeignKeys(t *testing.T) {
},
si: &FakeSI{
KsForeignKeyMode: map[string]vschemapb.Keyspace_ForeignKeyMode{
"ks": vschemapb.Keyspace_FK_MANAGED,
"ks": vschemapb.Keyspace_managed,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/vcursor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,7 @@ func (vc *vcursorImpl) PlannerWarning(message string) {
// ForeignKeyMode implements the VCursor interface
func (vc *vcursorImpl) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) {
if strings.ToLower(foreignKeyMode) == "disallow" {
return vschemapb.Keyspace_FK_DISALLOW, nil
return vschemapb.Keyspace_disallow, nil
}
ks := vc.vschema.Keyspaces[keyspace]
if ks == nil {
Expand Down
10 changes: 5 additions & 5 deletions go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func buildKeyspaces(source *vschemapb.SrvVSchema, vschema *VSchema) {
Name: ksname,
Sharded: ks.Sharded,
},
ForeignKeyMode: replaceDefaultForeignKeyMode(ks.ForeignKeyMode),
ForeignKeyMode: replaceUnspecifiedForeignKeyMode(ks.ForeignKeyMode),
Tables: make(map[string]*Table),
Vindexes: make(map[string]Vindex),
}
Expand All @@ -335,10 +335,10 @@ func buildKeyspaces(source *vschemapb.SrvVSchema, vschema *VSchema) {
}
}

// 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
// replaceUnspecifiedForeignKeyMode replaces the default value of the foreign key mode enum with the default we want to keep.
func replaceUnspecifiedForeignKeyMode(fkMode vschemapb.Keyspace_ForeignKeyMode) vschemapb.Keyspace_ForeignKeyMode {
if fkMode == vschemapb.Keyspace_unspecified {
return vschemapb.Keyspace_unmanaged
}
return fkMode
}
Expand Down
Loading

0 comments on commit a5e819d

Please sign in to comment.