@@ -5042,6 +5042,7 @@ make_unique_key_name(const char *field_name,KEY *start,KEY *end)
50425042 NO_FRM_RENAME Don't rename the FRM file
50435043 but only the table in the storage engine.
50445044 NO_HA_TABLE Don't rename table in engine.
5045+ NO_FK_CHECKS Don't check FK constraints during rename.
50455046
50465047 @return false OK
50475048 @return true Error
@@ -5059,12 +5060,17 @@ mysql_rename_table(handlerton *base, const char *old_db,
50595060 char tmp_name[NAME_LEN+1 ];
50605061 handler *file;
50615062 int error=0 ;
5063+ ulonglong save_bits= thd->variables .option_bits ;
50625064 int length;
50635065 bool was_truncated;
50645066 DBUG_ENTER (" mysql_rename_table" );
50655067 DBUG_PRINT (" enter" , (" old: '%s'.'%s' new: '%s'.'%s'" ,
50665068 old_db, old_name, new_db, new_name));
50675069
5070+ // Temporarily disable foreign key checks
5071+ if (flags & NO_FK_CHECKS)
5072+ thd->variables .option_bits |= OPTION_NO_FOREIGN_KEY_CHECKS;
5073+
50685074 file= (base == NULL ? 0 :
50695075 get_new_handler ((TABLE_SHARE*) 0 , thd->mem_root , base));
50705076
@@ -5139,6 +5145,8 @@ mysql_rename_table(handlerton *base, const char *old_db,
51395145 }
51405146#endif
51415147
5148+ // Restore options bits to the original value
5149+ thd->variables .option_bits = save_bits;
51425150
51435151 DBUG_RETURN (error != 0 );
51445152}
@@ -6653,7 +6661,7 @@ static bool mysql_inplace_alter_table(THD *thd,
66536661 */
66546662 (void ) mysql_rename_table (db_type,
66556663 alter_ctx->new_db , alter_ctx->new_alias ,
6656- alter_ctx->db , alter_ctx->alias , 0 );
6664+ alter_ctx->db , alter_ctx->alias , NO_FK_CHECKS );
66576665 DBUG_RETURN (true );
66586666 }
66596667 }
@@ -7638,7 +7646,8 @@ simple_rename_or_index_change(THD *thd, TABLE_LIST *table_list,
76387646 {
76397647 (void ) mysql_rename_table (old_db_type,
76407648 alter_ctx->new_db , alter_ctx->new_alias ,
7641- alter_ctx->db , alter_ctx->table_name , 0 );
7649+ alter_ctx->db , alter_ctx->table_name ,
7650+ NO_FK_CHECKS);
76427651 error= -1 ;
76437652 }
76447653 }
@@ -8512,9 +8521,12 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
85128521 // Rename failed, delete the temporary table.
85138522 (void ) quick_rm_table (thd, new_db_type, alter_ctx.new_db ,
85148523 alter_ctx.tmp_name , FN_IS_TMP);
8524+
85158525 // Restore the backup of the original table to the old name.
85168526 (void ) mysql_rename_table (old_db_type, alter_ctx.db , backup_name,
8517- alter_ctx.db , alter_ctx.alias , FN_FROM_IS_TMP);
8527+ alter_ctx.db , alter_ctx.alias ,
8528+ FN_FROM_IS_TMP | NO_FK_CHECKS);
8529+
85188530 goto err_with_mdl;
85198531 }
85208532
@@ -8532,7 +8544,8 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
85328544 alter_ctx.new_db , alter_ctx.new_alias , 0 );
85338545 // Restore the backup of the original table to the old name.
85348546 (void ) mysql_rename_table (old_db_type, alter_ctx.db , backup_name,
8535- alter_ctx.db , alter_ctx.alias , FN_FROM_IS_TMP);
8547+ alter_ctx.db , alter_ctx.alias ,
8548+ FN_FROM_IS_TMP | NO_FK_CHECKS);
85368549 goto err_with_mdl;
85378550 }
85388551
0 commit comments