Skip to content

Instantly share code, notes, and snippets.

@nirbhayc
Last active June 20, 2016 16:59
Show Gist options
  • Save nirbhayc/442a0c269ce48b283543cac434aaf44e to your computer and use it in GitHub Desktop.
Save nirbhayc/442a0c269ce48b283543cac434aaf44e to your computer and use it in GitHub Desktop.
diff --git a/sql/sql_table.cc b/sql/sql_table.cc
index 0be329e..dcf6ab8 100644
--- a/sql/sql_table.cc
+++ b/sql/sql_table.cc
@@ -8151,12 +8151,15 @@ static bool fk_prepare_copy_alter_table(THD *thd, TABLE *table,
if (keys_onoff != Alter_info::LEAVE_AS_IS)
{
- if (wait_while_table_is_used(thd, table, extra_func))
- DBUG_RETURN(true);
+ if (!table->s->tmp_table)
+ {
+ if (wait_while_table_is_used(thd, table, extra_func))
+ DBUG_RETURN(true);
- // It's now safe to take the table level lock.
- if (lock_tables(thd, table_list, alter_ctx->tables_opened, 0))
- DBUG_RETURN(true);
+ // It's now safe to take the table level lock.
+ if (lock_tables(thd, table_list, alter_ctx->tables_opened, 0))
+ DBUG_RETURN(true);
+ }
error= alter_table_manage_keys(table,
table->file->indexes_are_disabled(),
@@ -8166,43 +8169,57 @@ static bool fk_prepare_copy_alter_table(THD *thd, TABLE *table,
if (!error && alter_ctx->is_table_renamed())
{
THD_STAGE_INFO(thd, stage_rename);
- handlerton *old_db_type= table->s->db_type();
- /*
- Then do a 'simple' rename of the table. First we need to close all
- instances of 'source' table.
- Note that if wait_while_table_is_used() returns error here (i.e. if
- this thread was killed) then it must be that previous step of
- simple rename did nothing and therefore we can safely return
- without additional clean-up.
- */
- if (wait_while_table_is_used(thd, table, extra_func))
- DBUG_RETURN(true);
- close_all_tables_for_name(thd, table->s, HA_EXTRA_PREPARE_FOR_RENAME, NULL);
-
LEX_STRING old_db_name= { alter_ctx->db, strlen(alter_ctx->db) };
LEX_STRING old_table_name=
{ alter_ctx->table_name, strlen(alter_ctx->table_name) };
LEX_STRING new_db_name= { alter_ctx->new_db, strlen(alter_ctx->new_db) };
LEX_STRING new_table_name=
{ alter_ctx->new_alias, strlen(alter_ctx->new_alias) };
- (void) rename_table_in_stat_tables(thd, &old_db_name, &old_table_name,
- &new_db_name, &new_table_name);
- if (mysql_rename_table(old_db_type, alter_ctx->db, alter_ctx->table_name,
- alter_ctx->new_db, alter_ctx->new_alias, 0))
- error= -1;
- else if (Table_triggers_list::change_table_name(thd,
- alter_ctx->db,
- alter_ctx->alias,
- alter_ctx->table_name,
- alter_ctx->new_db,
- alter_ctx->new_alias))
- {
- (void) mysql_rename_table(old_db_type,
- alter_ctx->new_db, alter_ctx->new_alias,
- alter_ctx->db, alter_ctx->table_name,
- NO_FK_CHECKS);
- error= -1;
+ if (!table->s->tmp_table)
+ {
+ handlerton *old_db_type= table->s->db_type();
+ /*
+ Then do a 'simple' rename of the table. First we need to close all
+ instances of 'source' table.
+ Note that if wait_while_table_is_used() returns error here (i.e. if
+ this thread was killed) then it must be that previous step of
+ simple rename did nothing and therefore we can safely return
+ without additional clean-up.
+ */
+ if (wait_while_table_is_used(thd, table, extra_func))
+ DBUG_RETURN(true);
+ close_all_tables_for_name(thd, table->s, HA_EXTRA_PREPARE_FOR_RENAME,
+ NULL);
+
+ (void) rename_table_in_stat_tables(thd, &old_db_name, &old_table_name,
+ &new_db_name, &new_table_name);
+
+ if (mysql_rename_table(old_db_type, alter_ctx->db, alter_ctx->table_name,
+ alter_ctx->new_db, alter_ctx->new_alias, 0))
+ error= -1;
+ else if (Table_triggers_list::change_table_name(thd,
+ alter_ctx->db,
+ alter_ctx->alias,
+ alter_ctx->table_name,
+ alter_ctx->new_db,
+ alter_ctx->new_alias))
+ {
+ (void) mysql_rename_table(old_db_type,
+ alter_ctx->new_db, alter_ctx->new_alias,
+ alter_ctx->db, alter_ctx->table_name,
+ NO_FK_CHECKS);
+ error= -1;
+ }
+ }
+ else
+ {
+ if ((thd->rename_temporary_table(table, alter_ctx->new_db,
+ alter_ctx->new_alias)))
+ {
+ /* Allocation error, no need to rename it back to the original name. */
+ error= -1;
+ }
}
}
@@ -8545,8 +8562,7 @@ bool mysql_alter_table(THD *thd,char *new_db, char *new_name,
if (!(alter_info->flags & ~(Alter_info::ALTER_RENAME |
Alter_info::ALTER_KEYS_ONOFF)) &&
alter_info->requested_algorithm !=
- Alter_info::ALTER_TABLE_ALGORITHM_COPY &&
- !table->s->tmp_table) // no need to touch frm
+ Alter_info::ALTER_TABLE_ALGORITHM_COPY) // no need to touch frm
{
// This requires X-lock, no other lock levels supported.
if (alter_info->requested_lock != Alter_info::ALTER_TABLE_LOCK_DEFAULT &&
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment