List:Commits« Previous MessageNext Message »
From:He Zhenxing Date:January 15 2009 6:24am
Subject:bzr commit into mysql-5.1-bugteam branch (zhenxing.he:2733) Bug#33094
Bug#36428 Bug#38364 Bug#41986
View as plain text  
#At file:///media/sdb2/hezx/work/mysql/bzrwork/b35583/5.1-bugteam/

 2733 He Zhenxing	2009-01-15 [merge]
      auto merge
modified:
  client/mysqlcheck.c
  mysql-test/extra/rpl_tests/rpl_auto_increment.test
  mysql-test/r/mysqlcheck.result
  mysql-test/suite/rpl/r/rpl_auto_increment.result
  mysql-test/t/mysqlcheck.test
  mysys/my_init.c
  sql/log_event.cc
  sql/mysql_priv.h
  sql/sql_table.cc
  sql/sql_trigger.cc
  sql/sql_trigger.h

=== modified file 'client/mysqlcheck.c'
--- a/client/mysqlcheck.c	2008-11-14 09:48:01 +0000
+++ b/client/mysqlcheck.c	2009-01-14 14:50:51 +0000
@@ -40,15 +40,13 @@ static uint verbose = 0, opt_mysql_port=
 static int my_end_arg;
 static char * opt_mysql_unix_port = 0;
 static char *opt_password = 0, *current_user = 0, 
-	    *default_charset = (char *)MYSQL_DEFAULT_CHARSET_NAME,
-	    *current_host = 0;
+	    *default_charset= 0, *current_host= 0;
 static int first_error = 0;
 DYNAMIC_ARRAY tables4repair;
 #ifdef HAVE_SMEM
 static char *shared_memory_base_name=0;
 #endif
 static uint opt_protocol=0;
-static CHARSET_INFO *charset_info= &my_charset_latin1;
 
 enum operations { DO_CHECK, DO_REPAIR, DO_ANALYZE, DO_OPTIMIZE, DO_UPGRADE };
 
@@ -282,12 +280,10 @@ get_one_option(int optid, const struct m
     break;
   case OPT_FIX_DB_NAMES:
     what_to_do= DO_UPGRADE;
-    default_charset= (char*) "utf8";
     opt_databases= 1;
     break;
   case OPT_FIX_TABLE_NAMES:
     what_to_do= DO_UPGRADE;
-    default_charset= (char*) "utf8";
     break;
   case 'p':
     if (argument)
@@ -367,11 +363,20 @@ static int get_options(int *argc, char *
       what_to_do = DO_CHECK;
   }
 
-  /* TODO: This variable is not yet used */
-  if (strcmp(default_charset, charset_info->csname) &&
-      !(charset_info= get_charset_by_csname(default_charset, 
-  					    MY_CS_PRIMARY, MYF(MY_WME))))
-      exit(1);
+  /*
+    If there's no --default-character-set option given with
+    --fix-table-name or --fix-db-name set the default character set to "utf8".
+  */
+  if (!default_charset && (opt_fix_db_names || opt_fix_table_names))
+  {
+    default_charset= (char*) "utf8";
+  }
+  if (default_charset && !get_charset_by_csname(default_charset, MY_CS_PRIMARY,
+                                                MYF(MY_WME)))
+  {
+    printf("Unsupported character set: %s\n", default_charset);
+    return 1;
+  }
   if (*argc > 0 && opt_alldbs)
   {
     printf("You should give only options, no arguments at all, with option\n");
@@ -779,6 +784,8 @@ static int dbConnect(char *host, char *u
   if (shared_memory_base_name)
     mysql_options(&mysql_connection,MYSQL_SHARED_MEMORY_BASE_NAME,shared_memory_base_name);
 #endif
+  if (default_charset)
+    mysql_options(&mysql_connection, MYSQL_SET_CHARSET_NAME, default_charset);
   if (!(sock = mysql_real_connect(&mysql_connection, host, user, passwd,
          NULL, opt_mysql_port, opt_mysql_unix_port, 0)))
   {

=== modified file 'mysql-test/extra/rpl_tests/rpl_auto_increment.test'
--- a/mysql-test/extra/rpl_tests/rpl_auto_increment.test	2007-06-06 17:57:07 +0000
+++ b/mysql-test/extra/rpl_tests/rpl_auto_increment.test	2009-01-14 08:27:32 +0000
@@ -145,6 +145,23 @@ select * from t3 order by a;
 connection master;
 
 drop table t1,t2,t3;
+sync_slave_with_master;
+
+#
+# BUG#41986 Replication slave does not pick up proper AUTO_INCREMENT value for Innodb tables
+#
+connection master;
+set auto_increment_increment=1;
+set auto_increment_offset=1;
+CREATE TABLE t1 (id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY) ENGINE=innodb;
+INSERT INTO t1 VALUES (NULL), (NULL), (NULL);
+show create table t1;
+
+sync_slave_with_master;
+show create table t1;
+
+connection master;
+drop table t1;
 
 # End cleanup
 sync_slave_with_master;

=== modified file 'mysql-test/r/mysqlcheck.result'
--- a/mysql-test/r/mysqlcheck.result	2008-11-14 09:48:01 +0000
+++ b/mysql-test/r/mysqlcheck.result	2009-01-14 20:54:25 +0000
@@ -130,3 +130,58 @@ v1
 v-1
 drop view v1, `v-1`;
 drop table t1;
+SET NAMES utf8;
+CREATE TABLE `#mysql50#@` (a INT);
+SHOW TABLES;
+Tables_in_test
+#mysql50#@
+SET NAMES DEFAULT;
+mysqlcheck --fix-table-names --databases test
+SET NAMES utf8;
+SHOW TABLES;
+Tables_in_test
+@
+DROP TABLE `@`;
+CREATE TABLE `я` (a INT);
+SET NAMES DEFAULT;
+mysqlcheck --default-character-set="latin1" --databases test
+test.?
+Error    : Table doesn't exist
+error    : Corrupt
+mysqlcheck --default-character-set="utf8" --databases test
+test.я                                            OK
+SET NAMES utf8;
+DROP TABLE `я`;
+SET NAMES DEFAULT;
+CREATE DATABASE `#mysql50#a@b`;
+USE `#mysql50#a@b`;
+CREATE TABLE `#mysql50#c@d` (a INT);
+CREATE TABLE t1 (a INT);
+SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
+WHERE TRIGGER_SCHEMA="#mysql50#a@b" ORDER BY trigger_name;
+TRIGGER_CATALOG	TRIGGER_SCHEMA	TRIGGER_NAME	EVENT_MANIPULATION	EVENT_OBJECT_CATALOG	EVENT_OBJECT_SCHEMA	EVENT_OBJECT_TABLE	ACTION_ORDER	ACTION_CONDITION	ACTION_STATEMENT	ACTION_ORIENTATION	ACTION_TIMING	ACTION_REFERENCE_OLD_TABLE	ACTION_REFERENCE_NEW_TABLE	ACTION_REFERENCE_OLD_ROW	ACTION_REFERENCE_NEW_ROW	CREATED	SQL_MODE	DEFINER	CHARACTER_SET_CLIENT	COLLATION_CONNECTION	DATABASE_COLLATION
+NULL	#mysql50#a@b	tr1	INSERT	NULL	#mysql50#a@b	#mysql50#c@d	0	NULL	SET NEW.a = 10 * NEW.a	ROW	BEFORE	NULL	NULL	OLD	NEW	NULL		root@localhost	latin1	latin1_swedish_ci	latin1_swedish_ci
+NULL	#mysql50#a@b	tr2	INSERT	NULL	#mysql50#a@b	t1	0	NULL	SET NEW.a = 100 * NEW.a	ROW	BEFORE	NULL	NULL	OLD	NEW	NULL		root@localhost	latin1	latin1_swedish_ci	latin1_swedish_ci
+Warnings:
+Warning	1603	Triggers for table `#mysql50#a@b`.`#mysql50#c@d` have no creation context
+Warning	1603	Triggers for table `#mysql50#a@b`.`t1` have no creation context
+mysqlcheck --fix-db-names --fix-table-names --all-databases
+USE `a@b`;
+SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
+WHERE TRIGGER_SCHEMA="a@b" ORDER BY trigger_name;
+TRIGGER_CATALOG	TRIGGER_SCHEMA	TRIGGER_NAME	EVENT_MANIPULATION	EVENT_OBJECT_CATALOG	EVENT_OBJECT_SCHEMA	EVENT_OBJECT_TABLE	ACTION_ORDER	ACTION_CONDITION	ACTION_STATEMENT	ACTION_ORIENTATION	ACTION_TIMING	ACTION_REFERENCE_OLD_TABLE	ACTION_REFERENCE_NEW_TABLE	ACTION_REFERENCE_OLD_ROW	ACTION_REFERENCE_NEW_ROW	CREATED	SQL_MODE	DEFINER	CHARACTER_SET_CLIENT	COLLATION_CONNECTION	DATABASE_COLLATION
+NULL	a@b	tr1	INSERT	NULL	a@b	c@d	0	NULL	SET NEW.a = 10 * NEW.a	ROW	BEFORE	NULL	NULL	OLD	NEW	NULL		root@localhost	utf8	utf8_general_ci	latin1_swedish_ci
+NULL	a@b	tr2	INSERT	NULL	a@b	t1	0	NULL	SET NEW.a = 100 * NEW.a	ROW	BEFORE	NULL	NULL	OLD	NEW	NULL		root@localhost	utf8	utf8_general_ci	latin1_swedish_ci
+INSERT INTO `c@d` VALUES (2), (1);
+SELECT * FROM `c@d`;
+a
+20
+10
+INSERT INTO t1 VALUES (3), (5);
+SELECT * FROM t1;
+a
+300
+500
+DROP DATABASE `a@b`;
+USE test;
+End of 5.1 tests

=== modified file 'mysql-test/suite/rpl/r/rpl_auto_increment.result'
--- a/mysql-test/suite/rpl/r/rpl_auto_increment.result	2007-06-27 12:28:02 +0000
+++ b/mysql-test/suite/rpl/r/rpl_auto_increment.result	2009-01-14 08:27:32 +0000
@@ -227,3 +227,20 @@ select * from t3 order by a;
 a
 127
 drop table t1,t2,t3;
+set auto_increment_increment=1;
+set auto_increment_offset=1;
+CREATE TABLE t1 (id MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY) ENGINE=innodb;
+INSERT INTO t1 VALUES (NULL), (NULL), (NULL);
+show create table t1;
+Table	Create Table
+t1	CREATE TABLE `t1` (
+  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
+show create table t1;
+Table	Create Table
+t1	CREATE TABLE `t1` (
+  `id` mediumint(9) NOT NULL AUTO_INCREMENT,
+  PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
+drop table t1;

=== modified file 'mysql-test/t/mysqlcheck.test'
--- a/mysql-test/t/mysqlcheck.test	2008-11-14 09:48:01 +0000
+++ b/mysql-test/t/mysqlcheck.test	2009-01-14 20:54:25 +0000
@@ -112,3 +112,78 @@ show tables;
 show tables;
 drop view v1, `v-1`;
 drop table t1;
+
+#
+# Bug #33094: Error in upgrading from 5.0 to 5.1 when table contains triggers
+# Bug #41385: Crash when attempting to repair a #mysql50# upgraded table with
+#             triggers
+#
+SET NAMES utf8;
+CREATE TABLE `#mysql50#@` (a INT);
+SHOW TABLES;
+SET NAMES DEFAULT;
+--echo mysqlcheck --fix-table-names --databases test
+--exec $MYSQL_CHECK --fix-table-names --databases test
+SET NAMES utf8;
+SHOW TABLES;
+DROP TABLE `@`;
+
+CREATE TABLE `я` (a INT);
+SET NAMES DEFAULT;
+--echo mysqlcheck --default-character-set="latin1" --databases test
+# Error returned depends on platform, replace it with "Table doesn't exist"
+--replace_result "Can't find file: './test/@003f.frm' (errno: 22)" "Table doesn't exist" "Table 'test.?' doesn't exist" "Table doesn't exist"
+--exec $MYSQL_CHECK --default-character-set="latin1" --databases test
+--echo mysqlcheck --default-character-set="utf8" --databases test
+--exec $MYSQL_CHECK --default-character-set="utf8" --databases test
+SET NAMES utf8;
+DROP TABLE `я`;
+SET NAMES DEFAULT;
+
+CREATE DATABASE `#mysql50#a@b`;
+USE `#mysql50#a@b`;
+CREATE TABLE `#mysql50#c@d` (a INT);
+CREATE TABLE t1 (a INT);
+
+# Create 5.0 like triggers
+--write_file $MYSQLTEST_VARDIR/master-data/a@b/c@stripped
+TYPE=TRIGGERS
+triggers='CREATE DEFINER=`root`@`localhost` TRIGGER tr1 BEFORE INSERT ON `c@d` FOR EACH ROW SET NEW.a = 10 * NEW.a'
+sql_modes=0
+definers='root@localhost'
+EOF
+--write_file $MYSQLTEST_VARDIR/master-data/a@b/tr1.TRN
+TYPE=TRIGGERNAME
+trigger_table=c@d
+EOF
+--write_file $MYSQLTEST_VARDIR/master-data/a@b/t1.TRG
+TYPE=TRIGGERS
+triggers='CREATE DEFINER=`root`@`localhost` TRIGGER tr2 BEFORE INSERT ON `a@b`.t1 FOR EACH ROW SET NEW.a = 100 * NEW.a'
+sql_modes=0
+definers='root@localhost'
+EOF
+--write_file $MYSQLTEST_VARDIR/master-data/a@b/tr2.TRN
+TYPE=TRIGGERNAME
+trigger_table=t1
+EOF
+
+SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
+  WHERE TRIGGER_SCHEMA="#mysql50#a@b" ORDER BY trigger_name;
+
+--echo mysqlcheck --fix-db-names --fix-table-names --all-databases
+--exec $MYSQL_CHECK --fix-db-names --fix-table-names --all-databases
+
+USE `a@b`;
+SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
+  WHERE TRIGGER_SCHEMA="a@b" ORDER BY trigger_name;
+
+INSERT INTO `c@d` VALUES (2), (1);
+SELECT * FROM `c@d`;
+INSERT INTO t1 VALUES (3), (5);
+SELECT * FROM t1;
+
+DROP DATABASE `a@b`;
+
+USE test;
+
+--echo End of 5.1 tests

=== modified file 'mysys/my_init.c'
--- a/mysys/my_init.c	2008-11-10 20:38:45 +0000
+++ b/mysys/my_init.c	2009-01-12 19:48:02 +0000
@@ -78,8 +78,12 @@ my_bool my_init(void)
   my_umask= 0660;                       /* Default umask for new files */
   my_umask_dir= 0700;                   /* Default umask for new directories */
   init_glob_errs();
-#if defined(THREAD) && defined(SAFE_MUTEX)
+#if defined(THREAD)
+  if (my_thread_global_init())
+    return 1;
+#  if defined(SAFE_MUTEX)
   safe_mutex_global_init();		/* Must be called early */
+#  endif
 #endif
 #if defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
   fastmutex_global_init();              /* Must be called early */
@@ -89,8 +93,6 @@ my_bool my_init(void)
 #if defined(HAVE_PTHREAD_INIT)
   pthread_init();			/* Must be called before DBUG_ENTER */
 #endif
-  if (my_thread_global_init())
-    return 1;
 #if !defined( __WIN__) && !defined(__NETWARE__)
   sigfillset(&my_signals);		/* signals blocked by mf_brkhant */
 #endif

=== modified file 'sql/log_event.cc'
--- a/sql/log_event.cc	2009-01-09 10:35:26 +0000
+++ b/sql/log_event.cc	2009-01-14 09:32:25 +0000
@@ -8129,6 +8129,9 @@ Write_rows_log_event::do_before_row_oper
     analyze if explicit data is provided for slave's TIMESTAMP columns).
   */
   m_table->timestamp_field_type= TIMESTAMP_NO_AUTO_SET;
+  
+  /* Honor next number column if present */
+  m_table->next_number_field= m_table->found_next_number_field;
   return error;
 }
 
@@ -8137,6 +8140,7 @@ Write_rows_log_event::do_after_row_opera
                                               int error)
 {
   int local_error= 0;
+  m_table->next_number_field=0;
   if (bit_is_set(slave_exec_mode, SLAVE_EXEC_MODE_IDEMPOTENT) == 1 ||
       m_table->s->db_type()->db_type == DB_TYPE_NDBCLUSTER)
   {

=== modified file 'sql/mysql_priv.h'
--- a/sql/mysql_priv.h	2009-01-05 16:10:20 +0000
+++ b/sql/mysql_priv.h	2009-01-14 14:50:51 +0000
@@ -2230,6 +2230,7 @@ uint strconvert(CHARSET_INFO *from_cs, c
                 CHARSET_INFO *to_cs, char *to, uint to_length, uint *errors);
 uint filename_to_tablename(const char *from, char *to, uint to_length);
 uint tablename_to_filename(const char *from, char *to, uint to_length);
+uint check_n_cut_mysql50_prefix(const char *from, char *to, uint to_length);
 #endif /* MYSQL_SERVER || INNODB_COMPATIBILITY_HOOKS */
 #ifdef MYSQL_SERVER
 uint build_table_filename(char *buff, size_t bufflen, const char *db,

=== modified file 'sql/sql_table.cc'
--- a/sql/sql_table.cc	2009-01-15 05:42:02 +0000
+++ b/sql/sql_table.cc	2009-01-15 06:24:37 +0000
@@ -114,6 +114,30 @@ uint filename_to_tablename(const char *f
 }
 
 
+/**
+  Check if given string begins with "#mysql50#" prefix, cut it if so.
+  
+  @param   from          string to check and cut 
+  @param   to[out]       buffer for result string
+  @param   to_length     its size
+  
+  @retval
+    0      no prefix found
+  @retval
+    non-0  result string length
+*/
+
+uint check_n_cut_mysql50_prefix(const char *from, char *to, uint to_length)
+{
+  if (from[0] == '#' && 
+      !strncmp(from, MYSQL50_TABLE_NAME_PREFIX,
+               MYSQL50_TABLE_NAME_PREFIX_LENGTH))
+    return (uint) (strmake(to, from + MYSQL50_TABLE_NAME_PREFIX_LENGTH,
+                           to_length - 1) - to);
+  return 0;
+}
+
+
 /*
   Translate a table name to a file name (WL #1324).
 
@@ -133,11 +157,8 @@ uint tablename_to_filename(const char *f
   DBUG_ENTER("tablename_to_filename");
   DBUG_PRINT("enter", ("from '%s'", from));
 
-  if (from[0] == '#' && !strncmp(from, MYSQL50_TABLE_NAME_PREFIX,
-                                 MYSQL50_TABLE_NAME_PREFIX_LENGTH))
-    DBUG_RETURN((uint) (strmake(to, from+MYSQL50_TABLE_NAME_PREFIX_LENGTH,
-                                to_length-1) -
-                        (from + MYSQL50_TABLE_NAME_PREFIX_LENGTH)));
+  if ((length= check_n_cut_mysql50_prefix(from, to, to_length)))
+    DBUG_RETURN(length);
   length= strconvert(system_charset_info, from,
                      &my_charset_filename, to, to_length, &errors);
   if (check_if_legal_tablename(to) &&

=== modified file 'sql/sql_trigger.cc'
--- a/sql/sql_trigger.cc	2008-11-14 17:37:27 +0000
+++ b/sql/sql_trigger.cc	2009-01-14 14:50:51 +0000
@@ -1368,15 +1368,27 @@ bool Table_triggers_list::check_n_load(T
 
         if (triggers->on_table_names_list.push_back(on_table_name, &table->mem_root))
           goto err_with_lex_cleanup;
-
+#ifndef DBUG_OFF
         /*
           Let us check that we correctly update trigger definitions when we
           rename tables with triggers.
+          
+          In special cases like "RENAME TABLE `#mysql50#somename` TO `somename`"
+          or "ALTER DATABASE `#mysql50#somename` UPGRADE DATA DIRECTORY NAME"
+          we might be given table or database name with "#mysql50#" prefix (and
+          trigger's definiton contains un-prefixed version of the same name).
+          To remove this prefix we use check_n_cut_mysql50_prefix().
         */
-        DBUG_ASSERT(!my_strcasecmp(table_alias_charset, lex.query_tables->db, db) &&
-                    !my_strcasecmp(table_alias_charset, lex.query_tables->table_name,
-                                   table_name));
 
+        char fname[NAME_LEN + 1];
+        DBUG_ASSERT((!my_strcasecmp(table_alias_charset, lex.query_tables->db, db) ||
+                     (check_n_cut_mysql50_prefix(db, fname, sizeof(fname)) &&
+                      !my_strcasecmp(table_alias_charset, lex.query_tables->db, fname))) &&
+                    (!my_strcasecmp(table_alias_charset, lex.query_tables->table_name,
+                                    table_name) ||
+                     (check_n_cut_mysql50_prefix(table_name, fname, sizeof(fname)) &&
+                      !my_strcasecmp(table_alias_charset, lex.query_tables->table_name, fname))));
+#endif
         if (names_only)
         {
           lex_end(&lex);
@@ -1692,7 +1704,8 @@ end:
   (change name of table in triggers' definitions).
 
   @param thd                 Thread context
-  @param db_name             Database of subject table
+  @param old_db_name         Old database of subject table
+  @param new_db_name         New database of subject table
   @param old_table_name      Old subject table's name
   @param new_table_name      New subject table's name
 
@@ -1704,7 +1717,8 @@ end:
 
 bool
 Table_triggers_list::change_table_name_in_triggers(THD *thd,
-                                                   const char *db_name,
+                                                   const char *old_db_name,
+                                                   const char *new_db_name,
                                                    LEX_STRING *old_table_name,
                                                    LEX_STRING *new_table_name)
 {
@@ -1757,11 +1771,11 @@ Table_triggers_list::change_table_name_i
   if (thd->is_fatal_error)
     return TRUE; /* OOM */
 
-  if (save_trigger_file(this, db_name, new_table_name->str))
+  if (save_trigger_file(this, new_db_name, new_table_name->str))
     return TRUE;
-  if (rm_trigger_file(path_buff, db_name, old_table_name->str))
+  if (rm_trigger_file(path_buff, old_db_name, old_table_name->str))
   {
-    (void) rm_trigger_file(path_buff, db_name, new_table_name->str);
+    (void) rm_trigger_file(path_buff, new_db_name, new_table_name->str);
     return TRUE;
   }
   return FALSE;
@@ -1772,7 +1786,8 @@ Table_triggers_list::change_table_name_i
   Iterate though Table_triggers_list::names_list list and update
   .TRN files after renaming triggers' subject table.
 
-  @param db_name             Database of subject table
+  @param old_db_name         Old database of subject table
+  @param new_db_name         New database of subject table
   @param new_table_name      New subject table's name
   @param stopper             Pointer to Table_triggers_list::names_list at
                              which we should stop updating.
@@ -1785,7 +1800,8 @@ Table_triggers_list::change_table_name_i
 */
 
 LEX_STRING*
-Table_triggers_list::change_table_name_in_trignames(const char *db_name,
+Table_triggers_list::change_table_name_in_trignames(const char *old_db_name,
+                                                    const char *new_db_name,
                                                     LEX_STRING *new_table_name,
                                                     LEX_STRING *stopper)
 {
@@ -1798,7 +1814,7 @@ Table_triggers_list::change_table_name_i
   while ((trigger= it_name++) != stopper)
   {
     trigname_file.length= build_table_filename(trigname_buff, FN_REFLEN-1,
-                                               db_name, trigger->str,
+                                               new_db_name, trigger->str,
                                                TRN_EXT, 0);
     trigname_file.str= trigname_buff;
 
@@ -1807,6 +1823,16 @@ Table_triggers_list::change_table_name_i
     if (sql_create_definition_file(NULL, &trigname_file, &trigname_file_type,
                                    (uchar*)&trigname, trigname_file_parameters))
       return trigger;
+      
+    /* Remove stale .TRN file in case of database upgrade */
+    if (old_db_name)
+    {
+      if (rm_trigname_file(trigname_buff, old_db_name, trigger->str))
+      {
+        (void) rm_trigname_file(trigname_buff, new_db_name, trigger->str);
+        return trigger;
+      }
+    }
   }
 
   return 0;
@@ -1840,6 +1866,7 @@ bool Table_triggers_list::change_table_n
 {
   TABLE table;
   bool result= 0;
+  bool upgrading50to51= FALSE; 
   LEX_STRING *err_trigname;
   DBUG_ENTER("change_table_name");
 
@@ -1877,14 +1904,27 @@ bool Table_triggers_list::change_table_n
       moving table with them between two schemas raises too many questions.
       (E.g. what should happen if in new schema we already have trigger
        with same name ?).
+       
+      In case of "ALTER DATABASE `#mysql50#db1` UPGRADE DATA DIRECTORY NAME"
+      we will be given table name with "#mysql50#" prefix
+      To remove this prefix we use check_n_cut_mysql50_prefix().
     */
     if (my_strcasecmp(table_alias_charset, db, new_db))
     {
-      my_error(ER_TRG_IN_WRONG_SCHEMA, MYF(0));
-      result= 1;
-      goto end;
+      char dbname[NAME_LEN + 1];
+      if (check_n_cut_mysql50_prefix(db, dbname, sizeof(dbname)) && 
+          !my_strcasecmp(table_alias_charset, dbname, new_db))
+      {
+        upgrading50to51= TRUE;
+      }
+      else
+      {
+        my_error(ER_TRG_IN_WRONG_SCHEMA, MYF(0));
+        result= 1;
+        goto end;
+      }
     }
-    if (table.triggers->change_table_name_in_triggers(thd, db,
+    if (table.triggers->change_table_name_in_triggers(thd, db, new_db,
                                                       &old_table_name,
                                                       &new_table_name))
     {
@@ -1892,7 +1932,8 @@ bool Table_triggers_list::change_table_n
       goto end;
     }
     if ((err_trigname= table.triggers->change_table_name_in_trignames(
-                                         db, &new_table_name, 0)))
+                                         upgrading50to51 ? db : NULL,
+                                         new_db, &new_table_name, 0)))
     {
       /*
         If we were unable to update one of .TRN files properly we will
@@ -1900,16 +1941,17 @@ bool Table_triggers_list::change_table_n
         We assume that we will be able to undo our changes without errors
         (we can't do much if there will be an error anyway).
       */
-      (void) table.triggers->change_table_name_in_trignames(db,
-                                                            &old_table_name,
-                                                            err_trigname);
-      (void) table.triggers->change_table_name_in_triggers(thd, db,
-                                                           &new_table_name,
-                                                           &old_table_name);
+      (void) table.triggers->change_table_name_in_trignames(
+                               upgrading50to51 ? new_db : NULL, db,
+                               &old_table_name, err_trigname);
+      (void) table.triggers->change_table_name_in_triggers(
+                               thd, db, new_db,
+                               &new_table_name, &old_table_name);
       result= 1;
       goto end;
     }
   }
+  
 end:
   delete table.triggers;
   free_root(&table.mem_root, MYF(0));

=== modified file 'sql/sql_trigger.h'
--- a/sql/sql_trigger.h	2007-10-16 20:11:50 +0000
+++ b/sql/sql_trigger.h	2009-01-14 14:50:51 +0000
@@ -146,11 +146,13 @@ public:
 
 private:
   bool prepare_record1_accessors(TABLE *table);
-  LEX_STRING* change_table_name_in_trignames(const char *db_name,
+  LEX_STRING* change_table_name_in_trignames(const char *old_db_name,
+                                             const char *new_db_name,
                                              LEX_STRING *new_table_name,
                                              LEX_STRING *stopper);
   bool change_table_name_in_triggers(THD *thd,
-                                     const char *db_name,
+                                     const char *old_db_name,
+                                     const char *new_db_name,
                                      LEX_STRING *old_table_name,
                                      LEX_STRING *new_table_name);
 };

Thread
bzr commit into mysql-5.1-bugteam branch (zhenxing.he:2733) Bug#33094Bug#36428 Bug#38364 Bug#41986He Zhenxing15 Jan