From 14c0751c30c4f495bddbd0aefe2787897b806fa5 Mon Sep 17 00:00:00 2001
From: unknown <mikael@c-0409e253.1238-1-64736c10.cust.bredbandsbolaget.se>
Date: Mon, 12 Jun 2006 17:30:32 -0400
Subject: [PATCH] BUG#19122: Need to close all handlers before rename/drop
 phase in ALTER TABLE ADD/DROP/REORGANIZE partitions After review fix

mysql-test/r/partition.result:
  Fixes for test
mysql-test/t/partition.test:
  Fixes for test
sql/lock.cc:
  After review fix
sql/mysql_priv.h:
  After review fix
sql/sql_partition.cc:
  After review fix
---
 mysql-test/r/partition.result |  9 ++++---
 mysql-test/t/partition.test   |  2 ++
 sql/lock.cc                   | 13 +++-------
 sql/mysql_priv.h              |  2 +-
 sql/sql_partition.cc          | 49 ++++++++++++++++++-----------------
 5 files changed, 36 insertions(+), 39 deletions(-)

diff --git a/mysql-test/r/partition.result b/mysql-test/r/partition.result
index 8bf86056e2..967e30104c 100644
--- a/mysql-test/r/partition.result
+++ b/mysql-test/r/partition.result
@@ -890,6 +890,11 @@ s1
 2
 3
 drop table t1;
+create table t1 (a int) engine=memory
+partition by key(a);
+insert into t1 values (1);
+create index inx1 on t1(a);
+drop table t1;
 create table t1 (a int)
 partition by key (a)
 (partition p1 engine = innodb);
@@ -900,10 +905,6 @@ alter table t1 rebuild partition p1;
 alter table t1 rebuild partition p1;
 alter table t1 rebuild partition p1;
 alter table t1 rebuild partition p1;
-create table t1 (a int) engine=memory
-partition by key(a);
-insert into t1 values (1);
-create index inx1 on t1(a);
 drop table t1;
 create table t1 (a int)
 partition by key (a)
diff --git a/mysql-test/t/partition.test b/mysql-test/t/partition.test
index 03c3f6e0a7..1d3e38a6e1 100644
--- a/mysql-test/t/partition.test
+++ b/mysql-test/t/partition.test
@@ -1038,6 +1038,8 @@ alter table t1 rebuild partition p1;
 alter table t1 rebuild partition p1;
 alter table t1 rebuild partition p1;
 
+drop table t1;
+
 #
 # BUG 19304 Partitions: MERGE handler not allowed in partitioned tables
 #
diff --git a/sql/lock.cc b/sql/lock.cc
index 3a0aa99218..efb4d696e6 100644
--- a/sql/lock.cc
+++ b/sql/lock.cc
@@ -924,16 +924,9 @@ int lock_table_name(THD *thd, TABLE_LIST *table_list, bool check_in_use)
     DBUG_RETURN(-1);
   }
 
-  if (!check_in_use)
-  {
-    DBUG_RETURN(0);
-  }
-  else
-  {
-    /* Return 1 if table is in use */
-    DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
-                                             RTFC_NO_FLAG)));
-  }
+  /* Return 1 if table is in use */
+  DBUG_RETURN(test(remove_table_from_cache(thd, db, table_list->table_name,
+             check_in_use ? RTFC_NO_FLAG : RTFC_WAIT_OTHER_THREAD_FLAG)));
 }
 
 
diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h
index 443066c5f0..8c48a84b9f 100644
--- a/sql/mysql_priv.h
+++ b/sql/mysql_priv.h
@@ -1206,7 +1206,7 @@ typedef struct st_lock_param_type
   List<Key> new_key_list;
   TABLE *table;
   KEY *key_info_buffer;
-  char *db;
+  const char *db;
   const char *table_name;
   const void *pack_frm_data;
   enum thr_lock_type old_lock_type;
diff --git a/sql/sql_partition.cc b/sql/sql_partition.cc
index 36f4144aaf..15e1c42bbf 100644
--- a/sql/sql_partition.cc
+++ b/sql/sql_partition.cc
@@ -5389,7 +5389,7 @@ static int get_name_lock(ALTER_PARTITION_PARAM_TYPE *lpt)
   DBUG_ENTER("get_name_lock");
 
   bzero(&lpt->table_list, sizeof(lpt->table_list));
-  lpt->table_list.db= lpt->db;
+  lpt->table_list.db= (char*)lpt->db;
   lpt->table_list.table= lpt->table;
   lpt->table_list.table_name= (char*)lpt->table_name;
   pthread_mutex_lock(&LOCK_open);
@@ -5775,24 +5775,22 @@ uint fast_alter_partition_table(THD *thd, TABLE *table,
         (not_completed= FALSE) ||
         abort_and_upgrade_lock(lpt) || /* Always returns 0 */
         ERROR_INJECT_CRASH("crash_drop_partition_4") ||
+        get_name_lock(lpt) ||
+        ERROR_INJECT_CRASH("crash_drop_partition_5") ||
+        alter_close_tables(lpt) ||
+        ERROR_INJECT_CRASH("crash_drop_partition_6") ||
         ((!thd->lex->no_write_to_binlog) &&
          (write_bin_log(thd, FALSE,
                         thd->query, thd->query_length), FALSE)) ||
-        ERROR_INJECT_CRASH("crash_drop_partition_5") ||
+        ERROR_INJECT_CRASH("crash_drop_partition_7") ||
         ((frm_install= TRUE), FALSE) ||
         mysql_write_frm(lpt, WFRM_INSTALL_SHADOW) ||
         ((frm_install= FALSE), FALSE) ||
-        ERROR_INJECT_CRASH("crash_drop_partition_6") ||
-        get_name_lock(lpt) || /* Always returns 0 */
-        ERROR_INJECT_CRASH("crash_drop_partition_7") ||
-        (close_open_tables_and_downgrade(lpt), FALSE) ||
         ERROR_INJECT_CRASH("crash_drop_partition_8") ||
-        alter_close_tables(lpt) ||
-        ERROR_INJECT_CRASH("crash_drop_partition_9") ||
         mysql_drop_partitions(lpt) ||
-        ERROR_INJECT_CRASH("crash_drop_partition_10") ||
+        ERROR_INJECT_CRASH("crash_drop_partition_9") ||
         (write_log_completed(lpt, FALSE), FALSE) ||
-        ERROR_INJECT_CRASH("crash_drop_partition_11") ||
+        ERROR_INJECT_CRASH("crash_drop_partition_10") ||
         (release_name_lock(lpt), FALSE)) 
     {
       handle_alter_part_error(lpt, not_completed, TRUE, frm_install);
@@ -5836,19 +5834,24 @@ uint fast_alter_partition_table(THD *thd, TABLE *table,
         mysql_change_partitions(lpt) ||
         ERROR_INJECT_CRASH("crash_add_partition_3") ||
         abort_and_upgrade_lock(lpt) || /* Always returns 0 */
+        ERROR_INJECT_CRASH("crash_add_partition_3") ||
+        get_name_lock(lpt) ||
+        ERROR_INJECT_CRASH("crash_add_partition_4") ||
+        alter_close_tables(lpt) ||
+        ERROR_INJECT_CRASH("crash_add_partition_5") ||
         ((!thd->lex->no_write_to_binlog) &&
          (write_bin_log(thd, FALSE,
                         thd->query, thd->query_length), FALSE)) ||
-        ERROR_INJECT_CRASH("crash_add_partition_4") ||
+        ERROR_INJECT_CRASH("crash_add_partition_6") ||
         write_log_rename_frm(lpt) ||
         (not_completed= FALSE) ||
-        ERROR_INJECT_CRASH("crash_add_partition_5") ||
+        ERROR_INJECT_CRASH("crash_add_partition_7") ||
         ((frm_install= TRUE), FALSE) ||
         mysql_write_frm(lpt, WFRM_INSTALL_SHADOW) ||
-        ERROR_INJECT_CRASH("crash_add_partition_6") ||
-        (close_open_tables_and_downgrade(lpt), FALSE) ||
+        ERROR_INJECT_CRASH("crash_add_partition_8") ||
         (write_log_completed(lpt, FALSE), FALSE) ||
-        ERROR_INJECT_CRASH("crash_add_partition_7")) 
+        ERROR_INJECT_CRASH("crash_add_partition_9") ||
+        (release_name_lock(lpt), FALSE)) 
     {
       handle_alter_part_error(lpt, not_completed, FALSE, frm_install);
       DBUG_RETURN(TRUE);
@@ -5924,23 +5927,21 @@ uint fast_alter_partition_table(THD *thd, TABLE *table,
         (not_completed= FALSE) ||
         abort_and_upgrade_lock(lpt) || /* Always returns 0 */
         ERROR_INJECT_CRASH("crash_change_partition_5") ||
+        get_name_lock(lpt) ||
+        ERROR_INJECT_CRASH("crash_change_partition_6") ||
+        alter_close_tables(lpt) ||
+        ERROR_INJECT_CRASH("crash_change_partition_7") ||
         ((!thd->lex->no_write_to_binlog) &&
          (write_bin_log(thd, FALSE,
                         thd->query, thd->query_length), FALSE)) ||
-        ERROR_INJECT_CRASH("crash_change_partition_6") ||
-        mysql_write_frm(lpt, WFRM_INSTALL_SHADOW) ||
-        ERROR_INJECT_CRASH("crash_change_partition_7") ||
-        get_name_lock(lpt) ||
         ERROR_INJECT_CRASH("crash_change_partition_8") ||
-        (close_open_tables_and_downgrade(lpt), FALSE) ||
+        mysql_write_frm(lpt, WFRM_INSTALL_SHADOW) ||
         ERROR_INJECT_CRASH("crash_change_partition_9") ||
-        alter_close_tables(lpt) ||
-        ERROR_INJECT_CRASH("crash_change_partition_10") ||
         mysql_rename_partitions(lpt) ||
         ((frm_install= TRUE), FALSE) ||
-        ERROR_INJECT_CRASH("crash_change_partition_11") ||
+        ERROR_INJECT_CRASH("crash_change_partition_10") ||
         (write_log_completed(lpt, FALSE), FALSE) ||
-        ERROR_INJECT_CRASH("crash_change_partition_12") ||
+        ERROR_INJECT_CRASH("crash_change_partition_11") ||
         (release_name_lock(lpt), FALSE))
     {
       handle_alter_part_error(lpt, not_completed, FALSE, frm_install);
-- 
2.30.9