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

Online DDL: support DROP FOREIGN KEY statement #14338

Merged
merged 3 commits into from
Oct 25, 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
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
Loading