From 64085d8a348161fbb09f7ad65f5c00a346a35eac Mon Sep 17 00:00:00 2001 From: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> Date: Wed, 25 Oct 2023 11:52:08 +0300 Subject: [PATCH] Online DDL: support DROP FOREIGN KEY statement (#14338) Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com> --- .../onlineddl/scheduler/onlineddl_scheduler_test.go | 6 ++++++ go/vt/vttablet/onlineddl/executor.go | 4 ++-- go/vt/vttablet/onlineddl/executor_test.go | 11 +++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/go/test/endtoend/onlineddl/scheduler/onlineddl_scheduler_test.go b/go/test/endtoend/onlineddl/scheduler/onlineddl_scheduler_test.go index 057c937d6cf..031751bc775 100644 --- a/go/test/endtoend/onlineddl/scheduler/onlineddl_scheduler_test.go +++ b/go/test/endtoend/onlineddl/scheduler/onlineddl_scheduler_test.go @@ -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 { diff --git a/go/vt/vttablet/onlineddl/executor.go b/go/vt/vttablet/onlineddl/executor.go index 746edbd6948..e296bf026d8 100644 --- a/go/vt/vttablet/onlineddl/executor.go +++ b/go/vt/vttablet/onlineddl/executor.go @@ -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)) diff --git a/go/vt/vttablet/onlineddl/executor_test.go b/go/vt/vttablet/onlineddl/executor_test.go index b642921d831..691891c4af7 100644 --- a/go/vt/vttablet/onlineddl/executor_test.go +++ b/go/vt/vttablet/onlineddl/executor_test.go @@ -158,6 +158,7 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) { e := Executor{} tt := []struct { alter string + m map[string]string expect []string }{ { @@ -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) { @@ -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)