@@ -5063,6 +5063,7 @@ make_unique_key_name(const char *field_name,KEY *start,KEY *end)
50635063 NO_FRM_RENAME Don't rename the FRM file
50645064 but only the table in the storage engine.
50655065 NO_HA_TABLE Don't rename table in engine.
5066+ NO_FK_CHECKS Don't check FK constraints during rename.
50665067
50675068 @return false OK
50685069 @return true Error
@@ -5080,12 +5081,17 @@ mysql_rename_table(handlerton *base, const char *old_db,
50805081 char tmp_name[NAME_LEN+1 ];
50815082 handler *file;
50825083 int error=0 ;
5084+ ulonglong save_bits= thd->variables .option_bits ;
50835085 int length;
50845086 bool was_truncated;
50855087 DBUG_ENTER (" mysql_rename_table" );
50865088 DBUG_PRINT (" enter" , (" old: '%s'.'%s' new: '%s'.'%s'" ,
50875089 old_db, old_name, new_db, new_name));
50885090
5091+ // Temporarily disable foreign key checks
5092+ if (flags & NO_FK_CHECKS)
5093+ thd->variables .option_bits |= OPTION_NO_FOREIGN_KEY_CHECKS;
5094+
50895095 file= (base == NULL ? 0 :
50905096 get_new_handler ((TABLE_SHARE*) 0 , thd->mem_root , base));
50915097
@@ -5160,6 +5166,8 @@ mysql_rename_table(handlerton *base, const char *old_db,
51605166 }
51615167#endif
51625168
5169+ // Restore options bits to the original value
5170+ thd->variables .option_bits = save_bits;
51635171
51645172 DBUG_RETURN (error != 0 );
51655173}
@@ -6572,7 +6580,7 @@ static bool mysql_inplace_alter_table(THD *thd,
65726580 */
65736581 (void ) mysql_rename_table (db_type,
65746582 alter_ctx->new_db , alter_ctx->new_alias ,
6575- alter_ctx->db , alter_ctx->alias , 0 );
6583+ alter_ctx->db , alter_ctx->alias , NO_FK_CHECKS );
65766584 DBUG_RETURN (true );
65776585 }
65786586 }
@@ -7522,7 +7530,8 @@ simple_rename_or_index_change(THD *thd, TABLE_LIST *table_list,
75227530 {
75237531 (void ) mysql_rename_table (old_db_type,
75247532 alter_ctx->new_db , alter_ctx->new_alias ,
7525- alter_ctx->db , alter_ctx->table_name , 0 );
7533+ alter_ctx->db , alter_ctx->table_name ,
7534+ NO_FK_CHECKS);
75267535 error= -1 ;
75277536 }
75287537 }
@@ -8397,9 +8406,12 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
83978406 // Rename failed, delete the temporary table.
83988407 (void ) quick_rm_table (thd, new_db_type, alter_ctx.new_db ,
83998408 alter_ctx.tmp_name , FN_IS_TMP);
8409+
84008410 // Restore the backup of the original table to the old name.
84018411 (void ) mysql_rename_table (old_db_type, alter_ctx.db , backup_name,
8402- alter_ctx.db , alter_ctx.alias , FN_FROM_IS_TMP);
8412+ alter_ctx.db , alter_ctx.alias ,
8413+ FN_FROM_IS_TMP | NO_FK_CHECKS);
8414+
84038415 goto err_with_mdl;
84048416 }
84058417
@@ -8417,7 +8429,8 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
84178429 alter_ctx.new_db , alter_ctx.new_alias , 0 );
84188430 // Restore the backup of the original table to the old name.
84198431 (void ) mysql_rename_table (old_db_type, alter_ctx.db , backup_name,
8420- alter_ctx.db , alter_ctx.alias , FN_FROM_IS_TMP);
8432+ alter_ctx.db , alter_ctx.alias ,
8433+ FN_FROM_IS_TMP | NO_FK_CHECKS);
84218434 goto err_with_mdl;
84228435 }
84238436
0 commit comments