Skip to content

Commit 64085d8

Browse files
authored
Online DDL: support DROP FOREIGN KEY statement (#14338)
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
1 parent 5af661e commit 64085d8

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

go/test/endtoend/onlineddl/scheduler/onlineddl_scheduler_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,6 +2122,12 @@ func testForeignKeys(t *testing.T) {
21222122
allowForeignKeys: true,
21232123
expectHint: "new_fk",
21242124
},
2125+
{
2126+
name: "drop foreign key from a child",
2127+
sql: "alter table child_table DROP FOREIGN KEY child_parent_fk",
2128+
allowForeignKeys: true,
2129+
expectHint: "child_hint",
2130+
},
21252131
}
21262132

21272133
createParams := func(ddlStatement string, ddlStrategy string, executeStrategy string, expectHint string, expectError string, skipWait bool) *testOnlineDDLStatementParams {

go/vt/vttablet/onlineddl/executor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,8 +1195,8 @@ func (e *Executor) validateAndEditAlterTableStatement(ctx context.Context, onlin
11951195
validateWalk := func(node sqlparser.SQLNode) (kontinue bool, err error) {
11961196
switch node := node.(type) {
11971197
case *sqlparser.DropKey:
1198-
if node.Type == sqlparser.CheckKeyType {
1199-
// drop a check constraint
1198+
if node.Type == sqlparser.CheckKeyType || node.Type == sqlparser.ForeignKeyType {
1199+
// drop a check or a foreign key constraint
12001200
mappedName, ok := constraintMap[node.Name.String()]
12011201
if !ok {
12021202
return false, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "Found DROP CONSTRAINT: %v, but could not find constraint name in map", sqlparser.CanonicalString(node))

go/vt/vttablet/onlineddl/executor_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
158158
e := Executor{}
159159
tt := []struct {
160160
alter string
161+
m map[string]string
161162
expect []string
162163
}{
163164
{
@@ -208,6 +209,13 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
208209
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)",
209210
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"},
210211
},
212+
{
213+
alter: "alter table t drop foreign key t_fk_1",
214+
m: map[string]string{
215+
"t_fk_1": "fk_1_aaaaaaaaaaaaaa",
216+
},
217+
expect: []string{"alter table t drop foreign key fk_1_aaaaaaaaaaaaaa, algorithm = copy"},
218+
},
211219
}
212220
for _, tc := range tt {
213221
t.Run(tc.alter, func(t *testing.T) {
@@ -217,6 +225,9 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
217225
require.True(t, ok)
218226

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

0 commit comments

Comments
 (0)