@@ -45,11 +45,12 @@ func TestGetConstraintType(t *testing.T) {
45
45
func TestValidateAndEditCreateTableStatement (t * testing.T ) {
46
46
e := Executor {}
47
47
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
53
54
}{
54
55
{
55
56
name : "table with FK, not allowed" ,
@@ -59,11 +60,10 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
59
60
i int not null,
60
61
parent_id int not null,
61
62
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
63
64
)
64
65
` ,
65
- countConstraints : 1 ,
66
- expectError : schema .ErrForeignKeyFound .Error (),
66
+ expectError : schema .ErrForeignKeyFound .Error (),
67
67
},
68
68
{
69
69
name : "table with FK, allowed" ,
@@ -73,11 +73,28 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
73
73
i int not null,
74
74
parent_id int not null,
75
75
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
77
77
)
78
78
` ,
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
+ ` ,
79
94
strategyOptions : "--unsafe-allow-foreign-keys" ,
80
95
countConstraints : 1 ,
96
+ // we want 'onlineddl_test_' to be stripped out:
97
+ expectConstraintMap : map [string ]string {"onlineddl_test_ibfk_1" : "ibfk_1_2wtivm6zk4lthpz14g9uoyaqk" },
81
98
},
82
99
{
83
100
name : "table with anonymous FK, allowed" ,
@@ -90,8 +107,9 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
90
107
foreign key (parent_id) references onlineddl_test_parent (id) on delete no action
91
108
)
92
109
` ,
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" },
95
113
},
96
114
{
97
115
name : "table with CHECK constraints" ,
@@ -107,6 +125,12 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
107
125
)
108
126
` ,
109
127
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
+ },
110
134
},
111
135
{
112
136
name : "table with both FOREIGN and CHECK constraints" ,
@@ -116,12 +140,17 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
116
140
i int not null,
117
141
primary key(id),
118
142
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,
120
144
constraint chk_1111033c1d2d5908bf1f956ba900b192_check_4 CHECK ((i >= 0))
121
145
)
122
146
` ,
123
147
strategyOptions : "--unsafe-allow-foreign-keys" ,
124
148
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
+ },
125
154
},
126
155
}
127
156
for _ , tc := range tt {
@@ -134,11 +163,12 @@ func TestValidateAndEditCreateTableStatement(t *testing.T) {
134
163
onlineDDL := & schema.OnlineDDL {UUID : "a5a563da_dc1a_11ec_a416_0a43f95f28a3" , Table : "onlineddl_test" , Options : tc .strategyOptions }
135
164
constraintMap , err := e .validateAndEditCreateTableStatement (context .Background (), onlineDDL , createTable )
136
165
if tc .expectError != "" {
137
- require .Error (t , err )
138
166
assert .ErrorContains (t , err , tc .expectError )
139
- } else {
140
- assert .NoError (t , err )
167
+ return
141
168
}
169
+ assert .NoError (t , err )
170
+ assert .Equal (t , tc .expectConstraintMap , constraintMap )
171
+
142
172
uniqueConstraintNames := map [string ]bool {}
143
173
err = sqlparser .Walk (func (node sqlparser.SQLNode ) (kontinue bool , err error ) {
144
174
switch node := node .(type ) {
@@ -202,19 +232,25 @@ func TestValidateAndEditAlterTableStatement(t *testing.T) {
202
232
expect : []string {"alter table t add constraint myfk_6fmhzdlya89128u5j3xapq34i foreign key (parent_id) references onlineddl_test_parent (id) on delete no action, algorithm = copy" },
203
233
},
204
234
{
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" },
207
243
},
208
244
{
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" },
211
247
},
212
248
{
213
- alter : "alter table t drop foreign key t_fk_1 " ,
249
+ alter : "alter table t drop foreign key t_ibfk_1 " ,
214
250
m : map [string ]string {
215
- "t_fk_1 " : "fk_1_aaaaaaaaaaaaaa " ,
251
+ "t_ibfk_1 " : "ibfk_1_aaaaaaaaaaaaaa " ,
216
252
},
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" },
218
254
},
219
255
}
220
256
for _ , tc := range tt {
0 commit comments