Skip to content

Commit

Permalink
Online DDL: support DROP FOREIGN KEY statement (#14338)
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach authored Oct 25, 2023
1 parent 5af661e commit 64085d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2122,6 +2122,12 @@ func testForeignKeys(t *testing.T) {
allowForeignKeys: true,
expectHint: "new_fk",
},
{
name: "drop foreign key from a child",
sql: "alter table child_table DROP FOREIGN KEY child_parent_fk",
allowForeignKeys: true,
expectHint: "child_hint",
},
}

createParams := func(ddlStatement string, ddlStrategy string, executeStrategy string, expectHint string, expectError string, skipWait bool) *testOnlineDDLStatementParams {
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vttablet/onlineddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,8 @@ func (e *Executor) validateAndEditAlterTableStatement(ctx context.Context, onlin
validateWalk := func(node sqlparser.SQLNode) (kontinue bool, err error) {
switch node := node.(type) {
case *sqlparser.DropKey:
if node.Type == sqlparser.CheckKeyType {
// drop a check constraint
if node.Type == sqlparser.CheckKeyType || node.Type == sqlparser.ForeignKeyType {
// drop a check or a foreign key constraint
mappedName, ok := constraintMap[node.Name.String()]
if !ok {
return false, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "Found DROP CONSTRAINT: %v, but could not find constraint name in map", sqlparser.CanonicalString(node))
Expand Down
11 changes: 11 additions & 0 deletions go/vt/vttablet/onlineddl/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
e := Executor{}
tt := []struct {
alter string
m map[string]string
expect []string
}{
{
Expand Down Expand Up @@ -208,6 +209,13 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
alter: "alter table t add constraint t_fk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, add constraint some_check check (id != 1)",
expect: []string{"alter table t add constraint fk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, add constraint some_check_aulpn7bjeortljhguy86phdn9 check (id != 1), algorithm = copy"},
},
{
alter: "alter table t drop foreign key t_fk_1",
m: map[string]string{
"t_fk_1": "fk_1_aaaaaaaaaaaaaa",
},
expect: []string{"alter table t drop foreign key fk_1_aaaaaaaaaaaaaa, algorithm = copy"},
},
}
for _, tc := range tt {
t.Run(tc.alter, func(t *testing.T) {
Expand All @@ -217,6 +225,9 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
require.True(t, ok)

m := map[string]string{}
for k, v := range tc.m {
m[k] = v
}
onlineDDL := &schema.OnlineDDL{UUID: "a5a563da_dc1a_11ec_a416_0a43f95f28a3", Table: "t", Options: "--unsafe-allow-foreign-keys"}
alters, err := e.validateAndEditAlterTableStatement(context.Background(), onlineDDL, alterTable, m)
assert.NoError(t, err)
Expand Down

0 comments on commit 64085d8

Please sign in to comment.