Skip to content

Commit 8268f26

Browse files
MDEV-22934 Table disappear after two alter table command
Problem: ======= InnoDB drops the column which has foreign key relations on it. So it tries to load the foreign key during rename process of copy algorithm even though the foreign_key_check is disabled. Solution: ======== During alter copy algorithm, InnoDB ignores the error while loading the foreign key constraint if foreign key check is disabled. It should throw the warning about failure of the foreign key constraint when foreign key check is disabled.
1 parent 362b18c commit 8268f26

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

mysql-test/suite/innodb/r/foreign_key.result

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ SET GLOBAL innodb_purge_rseg_truncate_frequency = @saved_frequency;
659659
# MDEV-17187 table doesn't exist in engine after ALTER other tables
660660
# with CONSTRAINTs
661661
#
662+
call mtr.add_suppression("\\[Warning\\] InnoDB: In ALTER TABLE `test`\\.`t2` has or is referenced in foreign key constraints which are not compatible with the new table definition.");
662663
set foreign_key_checks=on;
663664
create table t1 (id int not null primary key) engine=innodb;
664665
create table t2 (id int not null primary key, fid int not null,
@@ -714,4 +715,30 @@ drop table t1,t2;
714715
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails
715716
drop table t1,t2;
716717
ERROR 42S02: Unknown table 'test.t2'
718+
#
719+
# MDEV-22934 Table disappear after two alter table command
720+
#
721+
CREATE TABLE t1(f1 INT NOT NULL AUTO_INCREMENT,
722+
f2 INT NOT NULL,
723+
PRIMARY KEY (f1), INDEX (f2))ENGINE=InnoDB;
724+
CREATE TABLE t2(f1 INT NOT NULL,
725+
f2 INT NOT NULL, f3 INT NOT NULL,
726+
PRIMARY KEY(f1, f2), UNIQUE KEY(f2),
727+
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (f2) REFERENCES t1(f2) ON DELETE CASCADE,
728+
CONSTRAINT `t2_ibfk_2` FOREIGN KEY (f1) REFERENCES t1(f1) ON DELETE CASCADE
729+
) ENGINE=InnoDB;
730+
SET FOREIGN_KEY_CHECKS=0;
731+
ALTER TABLE t2 DROP PRIMARY KEY, ADD PRIMARY KEY(f3), ALGORITHM=INPLACE;
732+
ALTER TABLE t2 DROP INDEX `f2`, ALGORITHM=COPY;
733+
SHOW CREATE TABLE t2;
734+
Table Create Table
735+
t2 CREATE TABLE `t2` (
736+
`f1` int(11) NOT NULL,
737+
`f2` int(11) NOT NULL,
738+
`f3` int(11) NOT NULL,
739+
PRIMARY KEY (`f3`)
740+
) ENGINE=InnoDB DEFAULT CHARSET=latin1
741+
CREATE TABLE t2 (f1 INT NOT NULL)ENGINE=InnoDB;
742+
ERROR 42S01: Table 't2' already exists
743+
DROP TABLE t2, t1;
717744
# End of 10.2 tests

mysql-test/suite/innodb/r/innodb.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2598,7 +2598,6 @@ set foreign_key_checks=0;
25982598
create table t2 (a varchar(10), foreign key (a) references t1(a)) engine = innodb;
25992599
create table t1(a varchar(10) primary key) engine = innodb;
26002600
alter table t1 modify column a int;
2601-
Got one of the listed errors
26022601
set foreign_key_checks=1;
26032602
drop table t2,t1;
26042603
set foreign_key_checks=0;
@@ -2607,6 +2606,7 @@ create table t1(a varchar(10) primary key) engine = innodb DEFAULT CHARSET=latin
26072606
alter table t1 convert to character set utf8;
26082607
set foreign_key_checks=1;
26092608
drop table t2,t1;
2609+
call mtr.add_suppression("\\[Warning\\] InnoDB: In ALTER TABLE `test`.`t1` has or is referenced in foreign key constraints which are not compatible with the new table definition.");
26102610
set foreign_key_checks=0;
26112611
create table t2 (a varchar(10), foreign key (a) references t1(a)) engine = innodb DEFAULT CHARSET=latin1;
26122612
create table t3(a varchar(10) primary key) engine = innodb DEFAULT CHARSET=utf8;

mysql-test/suite/innodb/t/foreign_key.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ SET GLOBAL innodb_purge_rseg_truncate_frequency = @saved_frequency;
657657
--echo # with CONSTRAINTs
658658
--echo #
659659

660+
call mtr.add_suppression("\\[Warning\\] InnoDB: In ALTER TABLE `test`\\.`t2` has or is referenced in foreign key constraints which are not compatible with the new table definition.");
661+
660662
set foreign_key_checks=on;
661663
create table t1 (id int not null primary key) engine=innodb;
662664
create table t2 (id int not null primary key, fid int not null,
@@ -698,6 +700,27 @@ drop table t1,t2;
698700
--error ER_BAD_TABLE_ERROR
699701
drop table t1,t2;
700702

703+
--echo #
704+
--echo # MDEV-22934 Table disappear after two alter table command
705+
--echo #
706+
CREATE TABLE t1(f1 INT NOT NULL AUTO_INCREMENT,
707+
f2 INT NOT NULL,
708+
PRIMARY KEY (f1), INDEX (f2))ENGINE=InnoDB;
709+
CREATE TABLE t2(f1 INT NOT NULL,
710+
f2 INT NOT NULL, f3 INT NOT NULL,
711+
PRIMARY KEY(f1, f2), UNIQUE KEY(f2),
712+
CONSTRAINT `t2_ibfk_1` FOREIGN KEY (f2) REFERENCES t1(f2) ON DELETE CASCADE,
713+
CONSTRAINT `t2_ibfk_2` FOREIGN KEY (f1) REFERENCES t1(f1) ON DELETE CASCADE
714+
) ENGINE=InnoDB;
715+
716+
SET FOREIGN_KEY_CHECKS=0;
717+
ALTER TABLE t2 DROP PRIMARY KEY, ADD PRIMARY KEY(f3), ALGORITHM=INPLACE;
718+
ALTER TABLE t2 DROP INDEX `f2`, ALGORITHM=COPY;
719+
SHOW CREATE TABLE t2;
720+
--error ER_TABLE_EXISTS_ERROR
721+
CREATE TABLE t2 (f1 INT NOT NULL)ENGINE=InnoDB;
722+
DROP TABLE t2, t1;
723+
701724
--echo # End of 10.2 tests
702725

703726
--source include/wait_until_count_sessions.inc

mysql-test/suite/innodb/t/innodb.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,6 @@ drop table t1;
16621662
set foreign_key_checks=0;
16631663
create table t2 (a varchar(10), foreign key (a) references t1(a)) engine = innodb;
16641664
create table t1(a varchar(10) primary key) engine = innodb;
1665-
-- error 1025,1025
16661665
alter table t1 modify column a int;
16671666
set foreign_key_checks=1;
16681667
drop table t2,t1;
@@ -1678,6 +1677,7 @@ drop table t2,t1;
16781677

16791678
# test that RENAME does not allow invalid charsets when f_k_c is 0
16801679

1680+
call mtr.add_suppression("\\[Warning\\] InnoDB: In ALTER TABLE `test`.`t1` has or is referenced in foreign key constraints which are not compatible with the new table definition.");
16811681
set foreign_key_checks=0;
16821682
create table t2 (a varchar(10), foreign key (a) references t1(a)) engine = innodb DEFAULT CHARSET=latin1;
16831683
create table t3(a varchar(10) primary key) engine = innodb DEFAULT CHARSET=utf8;

storage/innobase/row/row0mysql.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4536,12 +4536,20 @@ row_rename_table_for_mysql(
45364536
if (err != DB_SUCCESS) {
45374537

45384538
if (old_is_tmp) {
4539-
ib::error() << "In ALTER TABLE "
4539+
/* In case of copy alter, ignore the
4540+
loading of foreign key constraint
4541+
when foreign_key_check is disabled */
4542+
ib::error_or_warn(trx->check_foreigns)
4543+
<< "In ALTER TABLE "
45404544
<< ut_get_name(trx, new_name)
45414545
<< " has or is referenced in foreign"
45424546
" key constraints which are not"
45434547
" compatible with the new table"
45444548
" definition.";
4549+
if (!trx->check_foreigns) {
4550+
err = DB_SUCCESS;
4551+
goto funct_exit;
4552+
}
45454553
} else {
45464554
ib::error() << "In RENAME TABLE table "
45474555
<< ut_get_name(trx, new_name)

0 commit comments

Comments
 (0)