Skip to content

Commit e6e75d2

Browse files
shlomi-noachdbussink
authored andcommitted
Encode database and table names before resetting innodb cache (mysql#121) (mysql#122)
Signed-off-by: Shlomi Noach <[email protected]>
1 parent af57ee4 commit e6e75d2

File tree

3 files changed

+100
-2
lines changed

3 files changed

+100
-2
lines changed

mysql-test/r/rename_preserve_foreign_key_parent.result

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,62 @@ id hint
9393
delete from _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
9494
delete from parent_table where id=2;
9595
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`child_table`, CONSTRAINT `child_parent_fk` FOREIGN KEY (`parent_id`) REFERENCES `parent_table` (`id`))
96+
# Test on another schema
97+
select database();
98+
database()
99+
test
100+
create database `test_preserve_fk`;
101+
use `test_preserve_fk`;
102+
create table parent (id int primary key, i int);
103+
create table child1 (id int primary key, pid int, constraint child1_fk foreign key (pid) references parent(id));
104+
insert into parent values (1,10), (2,20);
105+
insert into child1 values (1,1), (2,2);
106+
insert into child1 values (4, 4);
107+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test_preserve_fk`.`child1`, CONSTRAINT `child1_fk` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`))
108+
create table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl like parent;
109+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from parent;
110+
rename table parent to _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl to parent, _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 to _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
111+
show create table child1;
112+
Table Create Table
113+
child1 CREATE TABLE `child1` (
114+
`id` int NOT NULL,
115+
`pid` int DEFAULT NULL,
116+
PRIMARY KEY (`id`),
117+
KEY `child1_fk` (`pid`),
118+
CONSTRAINT `child1_fk` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`)
119+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
120+
insert into child1 values (7, 7);
121+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test_preserve_fk`.`child1`, CONSTRAINT `child1_fk` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`))
122+
drop database `test_preserve_fk`;
123+
use test;
124+
# Test on schema with name that needs escaping
125+
select database();
126+
database()
127+
test
128+
create database `test-preserve-fk`;
129+
use `test-preserve-fk`;
130+
create table parent (id int primary key, i int);
131+
create table child1 (id int primary key, pid int, constraint child1_fk foreign key (pid) references parent(id));
132+
insert into parent values (1,10), (2,20);
133+
insert into child1 values (1,1), (2,2);
134+
insert into child1 values (4, 4);
135+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test-preserve-fk`.`child1`, CONSTRAINT `child1_fk` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`))
136+
create table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl like parent;
137+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from parent;
138+
rename table parent to _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl to parent, _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 to _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
139+
show create table child1;
140+
Table Create Table
141+
child1 CREATE TABLE `child1` (
142+
`id` int NOT NULL,
143+
`pid` int DEFAULT NULL,
144+
PRIMARY KEY (`id`),
145+
KEY `child1_fk` (`pid`),
146+
CONSTRAINT `child1_fk` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`)
147+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
148+
insert into child1 values (7, 7);
149+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test-preserve-fk`.`child1`, CONSTRAINT `child1_fk` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`))
150+
drop database `test-preserve-fk`;
151+
use test;
152+
# Cleanup
96153
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
97154
drop table if exists parent_table, parent_table_old, parent_table_new;

mysql-test/t/rename_preserve_foreign_key_parent.test

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,43 @@ delete from _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
6767
# This should error because of ON DELETE NO ACTION:
6868
--error ER_ROW_IS_REFERENCED_2
6969
delete from parent_table where id=2;
70-
# Cleanup
70+
--echo # Test on another schema
71+
select database();
72+
create database `test_preserve_fk`;
73+
use `test_preserve_fk`;
74+
create table parent (id int primary key, i int);
75+
create table child1 (id int primary key, pid int, constraint child1_fk foreign key (pid) references parent(id));
76+
insert into parent values (1,10), (2,20);
77+
insert into child1 values (1,1), (2,2);
78+
--error ER_NO_REFERENCED_ROW_2
79+
insert into child1 values (4, 4);
80+
create table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl like parent;
81+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from parent;
82+
rename table parent to _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl to parent, _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 to _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
83+
show create table child1;
84+
--error ER_NO_REFERENCED_ROW_2
85+
insert into child1 values (7, 7);
86+
drop database `test_preserve_fk`;
87+
use test;
88+
--echo # Test on schema with name that needs escaping
89+
select database();
90+
create database `test-preserve-fk`;
91+
use `test-preserve-fk`;
92+
create table parent (id int primary key, i int);
93+
create table child1 (id int primary key, pid int, constraint child1_fk foreign key (pid) references parent(id));
94+
insert into parent values (1,10), (2,20);
95+
insert into child1 values (1,1), (2,2);
96+
--error ER_NO_REFERENCED_ROW_2
97+
insert into child1 values (4, 4);
98+
create table _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl like parent;
99+
insert into _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl select * from parent;
100+
rename table parent to _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl to parent, _vt_DROP_6ace8bcef73211ea87e9f875a4d24e90_20200915120410 to _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl;
101+
show create table child1;
102+
--error ER_NO_REFERENCED_ROW_2
103+
insert into child1 values (7, 7);
104+
drop database `test-preserve-fk`;
105+
use test;
106+
--echo # Cleanup
71107
--disable_warnings
72108
drop table if exists _84371a37_6153_11eb_9917_f875a4d24e90_20210128122816_vrepl, _vt_HOLD_6ace8bcef73211ea87e9f875a4d24e90_20200915120410, child_table, child_table_old, child_table_new;
73109
drop table if exists parent_table, parent_table_old, parent_table_new;

sql/sql_table.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9875,7 +9875,12 @@ static bool reload_fk_children_after_parent_rename_preserve(
98759875
close_all_tables_for_name(thd, schema_name, table_name, false);
98769876

98779877
if (hton->dict_cache_reset) {
9878-
hton->dict_cache_reset(schema_name, table_name);
9878+
// Encode schema name and table name to "filename"; this is needed for InnoDB.
9879+
char tmp_schema[FN_REFLEN + 1];
9880+
char tmp_table[FN_REFLEN + 1];
9881+
tablename_to_filename(schema_name, tmp_schema, FN_REFLEN + 1);
9882+
tablename_to_filename(table_name, tmp_table, FN_REFLEN + 1);
9883+
hton->dict_cache_reset(tmp_schema, tmp_table);
98799884
}
98809885
}
98819886

0 commit comments

Comments
 (0)