Skip to content

Commit dde1939

Browse files
authored
schemadiff: remove table name from auto-generated FK constraint name (#14385)
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
1 parent 54f2daf commit dde1939

File tree

3 files changed

+81
-29
lines changed

3 files changed

+81
-29
lines changed

go/vt/schemadiff/names.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ExtractConstraintOriginalName(tableName string, constraintName string) stri
3939
if strings.HasPrefix(constraintName, fmt.Sprintf("%s_chk_", tableName)) {
4040
return constraintName[len(tableName)+1:]
4141
}
42-
if strings.HasPrefix(constraintName, fmt.Sprintf("%s_fk_", tableName)) {
42+
if strings.HasPrefix(constraintName, fmt.Sprintf("%s_ibfk_", tableName)) {
4343
return constraintName[len(tableName)+1:]
4444
}
4545
if submatch := constraintVitessNameRegexp.FindStringSubmatch(constraintName); len(submatch) > 0 {

go/vt/schemadiff/names_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
func TestConstraintOriginalName(t *testing.T) {
26-
{
26+
t.Run("check", func(t *testing.T) {
2727
names := []string{
2828
"check1",
2929
"check1_7no794p1x6zw6je1gfqmt7bca",
@@ -36,8 +36,24 @@ func TestConstraintOriginalName(t *testing.T) {
3636
assert.Equal(t, "check1", originalName)
3737
})
3838
}
39-
}
40-
{
39+
})
40+
t.Run("ibfk", func(t *testing.T) {
41+
names := []string{
42+
"ibfk_1",
43+
"ibfk_1_7no794p1x6zw6je1gfqmt7bca",
44+
"ibfk_1_etne0g9fvf3la2myjfsdgx9bx",
45+
"mytable_ibfk_1",
46+
}
47+
for _, name := range names {
48+
t.Run(name, func(t *testing.T) {
49+
originalName := ExtractConstraintOriginalName("mytable", name)
50+
assert.NotEmpty(t, originalName)
51+
assert.Equal(t, "ibfk_1", originalName)
52+
})
53+
}
54+
})
55+
56+
t.Run("chk", func(t *testing.T) {
4157
names := []string{
4258
"chk_1",
4359
"chk_1_7no794p1x6zw6je1gfqmt7bca",
@@ -51,9 +67,9 @@ func TestConstraintOriginalName(t *testing.T) {
5167
assert.Equal(t, "chk_1", originalName)
5268
})
5369
}
54-
}
70+
})
5571

56-
{
72+
t.Run("no change", func(t *testing.T) {
5773
names := []string{
5874
"check1",
5975
"check_991ek3m5g69vcule23s9vnayd_check1",
@@ -70,5 +86,5 @@ func TestConstraintOriginalName(t *testing.T) {
7086
assert.Equal(t, name, originalName)
7187
})
7288
}
73-
}
89+
})
7490
}

go/vt/vttablet/onlineddl/executor_test.go

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ func TestGetConstraintType(t *testing.T) {
4545
func TestValidateAndEditCreateTableStatement(t *testing.T) {
4646
e := Executor{}
4747
tt := []struct {
48-
name string
49-
query string
50-
strategyOptions string
51-
expectError string
52-
countConstraints int
48+
name string
49+
query string
50+
strategyOptions string
51+
expectError string
52+
countConstraints int
53+
expectConstraintMap map[string]string
5354
}{
5455
{
5556
name: "table with FK, not allowed",
@@ -59,11 +60,10 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
5960
i int not null,
6061
parent_id int not null,
6162
primary key(id),
62-
constraint test_fk foreign key (parent_id) references onlineddl_test_parent (id) on delete no action
63+
constraint test_ibfk foreign key (parent_id) references onlineddl_test_parent (id) on delete no action
6364
)
6465
`,
65-
countConstraints: 1,
66-
expectError: schema.ErrForeignKeyFound.Error(),
66+
expectError: schema.ErrForeignKeyFound.Error(),
6767
},
6868
{
6969
name: "table with FK, allowed",
@@ -73,11 +73,28 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
7373
i int not null,
7474
parent_id int not null,
7575
primary key(id),
76-
constraint test_fk foreign key (parent_id) references onlineddl_test_parent (id) on delete no action
76+
constraint test_ibfk foreign key (parent_id) references onlineddl_test_parent (id) on delete no action
7777
)
7878
`,
79+
strategyOptions: "--unsafe-allow-foreign-keys",
80+
countConstraints: 1,
81+
expectConstraintMap: map[string]string{"test_ibfk": "test_ibfk_2wtivm6zk4lthpz14g9uoyaqk"},
82+
},
83+
{
84+
name: "table with default FK name, strip table name",
85+
query: `
86+
create table onlineddl_test (
87+
id int auto_increment,
88+
i int not null,
89+
parent_id int not null,
90+
primary key(id),
91+
constraint onlineddl_test_ibfk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action
92+
)
93+
`,
7994
strategyOptions: "--unsafe-allow-foreign-keys",
8095
countConstraints: 1,
96+
// we want 'onlineddl_test_' to be stripped out:
97+
expectConstraintMap: map[string]string{"onlineddl_test_ibfk_1": "ibfk_1_2wtivm6zk4lthpz14g9uoyaqk"},
8198
},
8299
{
83100
name: "table with anonymous FK, allowed",
@@ -90,8 +107,9 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
90107
foreign key (parent_id) references onlineddl_test_parent (id) on delete no action
91108
)
92109
`,
93-
strategyOptions: "--unsafe-allow-foreign-keys",
94-
countConstraints: 1,
110+
strategyOptions: "--unsafe-allow-foreign-keys",
111+
countConstraints: 1,
112+
expectConstraintMap: map[string]string{"": "fk_2wtivm6zk4lthpz14g9uoyaqk"},
95113
},
96114
{
97115
name: "table with CHECK constraints",
@@ -107,6 +125,12 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
107125
)
108126
`,
109127
countConstraints: 4,
128+
expectConstraintMap: map[string]string{
129+
"check_1": "check_1_7dbssrkwdaxhdunwi5zj53q83",
130+
"check_2": "check_2_ehg3rtk6ejvbxpucimeess30o",
131+
"check_3": "check_3_0se0t8x98mf8v7lqmj2la8j9u",
132+
"chk_1111033c1d2d5908bf1f956ba900b192_check_4": "chk_1111033c1d2d5908bf1f956ba900b192_c_0c2c3bxi9jp4evqrct44wg3xh",
133+
},
110134
},
111135
{
112136
name: "table with both FOREIGN and CHECK constraints",
@@ -116,12 +140,17 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
116140
i int not null,
117141
primary key(id),
118142
constraint check_1 CHECK ((i >= 0)),
119-
constraint test_fk foreign key (parent_id) references onlineddl_test_parent (id) on delete no action,
143+
constraint test_ibfk foreign key (parent_id) references onlineddl_test_parent (id) on delete no action,
120144
constraint chk_1111033c1d2d5908bf1f956ba900b192_check_4 CHECK ((i >= 0))
121145
)
122146
`,
123147
strategyOptions: "--unsafe-allow-foreign-keys",
124148
countConstraints: 3,
149+
expectConstraintMap: map[string]string{
150+
"check_1": "check_1_7dbssrkwdaxhdunwi5zj53q83",
151+
"chk_1111033c1d2d5908bf1f956ba900b192_check_4": "chk_1111033c1d2d5908bf1f956ba900b192_c_0se0t8x98mf8v7lqmj2la8j9u",
152+
"test_ibfk": "test_ibfk_2wtivm6zk4lthpz14g9uoyaqk",
153+
},
125154
},
126155
}
127156
for _, tc := range tt {
@@ -134,11 +163,12 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
134163
onlineDDL := &schema.OnlineDDL{UUID: "a5a563da_dc1a_11ec_a416_0a43f95f28a3", Table: "onlineddl_test", Options: tc.strategyOptions}
135164
constraintMap, err := e.validateAndEditCreateTableStatement(context.Background(), onlineDDL, createTable)
136165
if tc.expectError != "" {
137-
require.Error(t, err)
138166
assert.ErrorContains(t, err, tc.expectError)
139-
} else {
140-
assert.NoError(t, err)
167+
return
141168
}
169+
assert.NoError(t, err)
170+
assert.Equal(t, tc.expectConstraintMap, constraintMap)
171+
142172
uniqueConstraintNames := map[string]bool{}
143173
err = sqlparser.Walk(func(node sqlparser.SQLNode) (kontinue bool, err error) {
144174
switch node := node.(type) {
@@ -202,19 +232,25 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
202232
expect: []string{"alter table t add constraint myfk_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy"},
203233
},
204234
{
205-
alter: "alter table t add constraint t_fk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action",
206-
expect: []string{"alter table t add constraint fk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy"},
235+
// strip out table name
236+
alter: "alter table t add constraint t_ibfk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action",
237+
expect: []string{"alter table t add constraint ibfk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy"},
238+
},
239+
{
240+
// stript out table name
241+
alter: "alter table t add constraint t_ibfk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action",
242+
expect: []string{"alter table t add constraint ibfk_1_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy"},
207243
},
208244
{
209-
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)",
210-
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"},
245+
alter: "alter table t add constraint t_ibfk_1 foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, add constraint some_check check (id != 1)",
246+
expect: []string{"alter table t add constraint ibfk_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"},
211247
},
212248
{
213-
alter: "alter table t drop foreign key t_fk_1",
249+
alter: "alter table t drop foreign key t_ibfk_1",
214250
m: map[string]string{
215-
"t_fk_1": "fk_1_aaaaaaaaaaaaaa",
251+
"t_ibfk_1": "ibfk_1_aaaaaaaaaaaaaa",
216252
},
217-
expect: []string{"alter table t drop foreign key fk_1_aaaaaaaaaaaaaa, algorithm = copy"},
253+
expect: []string{"alter table t drop foreign key ibfk_1_aaaaaaaaaaaaaa, algorithm = copy"},
218254
},
219255
}
220256
for _, tc := range tt {

0 commit comments

Comments
 (0)