Skip to content

Commit b08b78c

Browse files
FooBarriorvuvova
authored andcommitted
MDEV-29068 Cascade foreign key updates do not apply in online alter
1 parent 84ed2e7 commit b08b78c

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

mysql-test/main/alter_table_online.result

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,43 @@ XA END 'xid';
8080
XA COMMIT 'xid' ONE PHASE;
8181
DROP TABLE t;
8282
disconnect con1;
83+
connection default;
84+
#
85+
# MDEV-29068 Cascade foreign key updates do not apply in online alter
86+
#
87+
create table t1 (a int primary key) engine=InnoDB;
88+
insert into t1 values (1),(2),(3);
89+
create table t2 (b int, foreign key (b)
90+
references t1 (a)
91+
on update cascade) engine=InnoDB;
92+
insert into t2 values (1),(2),(3);
93+
alter table t2 add c int, algorithm=copy, lock=none;
94+
ERROR 0A000: LOCK=NONE is not supported. Reason: COPY algorithm requires a lock. Try LOCK=SHARED
95+
alter table t2 add c int, algorithm=inplace, lock=none;
96+
create or replace table t2 (b int, foreign key (b)
97+
references t1 (a)
98+
on delete set null) engine=InnoDB;
99+
alter table t2 add c int, algorithm=copy, lock=none;
100+
ERROR 0A000: LOCK=NONE is not supported. Reason: COPY algorithm requires a lock. Try LOCK=SHARED
101+
alter table t2 add c int, algorithm=inplace, lock=none;
102+
create or replace table t2 (b int, foreign key (b)
103+
references t1 (a)
104+
on delete no action) engine=InnoDB;
105+
insert into t2 values (1),(2),(3);
106+
alter table t2 add c int, algorithm=copy, lock=none;
107+
create or replace table t2 (b int, foreign key (b)
108+
references t1 (a)
109+
on update restrict) engine=InnoDB;
110+
insert into t2 values (1),(2),(3);
111+
alter table t2 add c int, algorithm=copy, lock=none;
112+
drop table t2, t1;
113+
create table t1 (a int primary key, b int unique) engine=InnoDB;
114+
insert into t1 values (1, 1),(2, 2),(3, 3);
115+
create table t2 (a int references t1 (a),
116+
b int references t1 (b) on update cascade) engine=InnoDB;
117+
insert into t2 values (1, 1),(2, 2);
118+
alter table t2 add c int, algorithm=copy, lock=none;
119+
ERROR 0A000: LOCK=NONE is not supported. Reason: COPY algorithm requires a lock. Try LOCK=SHARED
120+
alter table t2 add c int, algorithm=copy;
121+
alter table t2 add d int, algorithm=inplace;
122+
drop table t2, t1;

mysql-test/main/alter_table_online.test

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,54 @@ XA COMMIT 'xid' ONE PHASE;
6464

6565
DROP TABLE t;
6666
--disconnect con1
67+
--connection default
68+
69+
--echo #
70+
--echo # MDEV-29068 Cascade foreign key updates do not apply in online alter
71+
--echo #
72+
create table t1 (a int primary key) engine=InnoDB;
73+
insert into t1 values (1),(2),(3);
74+
create table t2 (b int, foreign key (b)
75+
references t1 (a)
76+
on update cascade) engine=InnoDB;
77+
insert into t2 values (1),(2),(3);
78+
79+
--error ER_ALTER_OPERATION_NOT_SUPPORTED_REASON
80+
alter table t2 add c int, algorithm=copy, lock=none;
81+
alter table t2 add c int, algorithm=inplace, lock=none;
82+
83+
create or replace table t2 (b int, foreign key (b)
84+
references t1 (a)
85+
on delete set null) engine=InnoDB;
86+
87+
--error ER_ALTER_OPERATION_NOT_SUPPORTED_REASON
88+
alter table t2 add c int, algorithm=copy, lock=none;
89+
alter table t2 add c int, algorithm=inplace, lock=none;
90+
91+
create or replace table t2 (b int, foreign key (b)
92+
references t1 (a)
93+
on delete no action) engine=InnoDB;
94+
95+
insert into t2 values (1),(2),(3);
96+
alter table t2 add c int, algorithm=copy, lock=none;
97+
98+
create or replace table t2 (b int, foreign key (b)
99+
references t1 (a)
100+
on update restrict) engine=InnoDB;
101+
102+
insert into t2 values (1),(2),(3);
103+
alter table t2 add c int, algorithm=copy, lock=none;
104+
drop table t2, t1;
105+
106+
create table t1 (a int primary key, b int unique) engine=InnoDB;
107+
insert into t1 values (1, 1),(2, 2),(3, 3);
108+
create table t2 (a int references t1 (a),
109+
b int references t1 (b) on update cascade) engine=InnoDB;
110+
insert into t2 values (1, 1),(2, 2);
111+
112+
--error ER_ALTER_OPERATION_NOT_SUPPORTED_REASON
113+
alter table t2 add c int, algorithm=copy, lock=none;
114+
alter table t2 add c int, algorithm=copy;
115+
alter table t2 add d int, algorithm=inplace;
116+
# Cleanup
117+
drop table t2, t1;

sql/sql_table.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10071,6 +10071,18 @@ bool mysql_alter_table(THD *thd, const LEX_CSTRING *new_db,
1007110071

1007210072
online= online && !table->s->tmp_table;
1007310073

10074+
List<FOREIGN_KEY_INFO> fk_list;
10075+
table->file->get_foreign_key_list(thd, &fk_list);
10076+
for (auto &fk: fk_list)
10077+
{
10078+
if (fk_modifies_child(fk.delete_method)
10079+
|| fk_modifies_child(fk.update_method))
10080+
{
10081+
online= false;
10082+
break;
10083+
}
10084+
}
10085+
1007410086
#ifdef WITH_WSREP
1007510087
if (WSREP(thd) &&
1007610088
(thd->lex->sql_command == SQLCOM_ALTER_TABLE ||

0 commit comments

Comments
 (0)