Commit bf6d480a authored by unknown's avatar unknown
Browse files

Bug #16997 Table rename that changes database does not rename indexes: Moved...

Bug #16997 Table rename that changes database does not rename indexes: Moved index tables to system database


parent d4aa0b8d
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
DROP TABLE IF EXISTS t1,t2;
drop database if exists mysqltest;
CREATE TABLE t1 (
pk1 INT NOT NULL PRIMARY KEY,
attr1 INT NOT NULL,
attr2 INT,
attr3 VARCHAR(10),
INDEX i1(attr1)
) ENGINE=ndbcluster;
INSERT INTO t1 VALUES (0,0,0,"zero"),(1,1,1,"one"),(2,2,2,"two");
SELECT * FROM t1 WHERE attr1 = 1;
pk1	attr1	attr2	attr3
1	1	1	one
alter table t1 rename t2;
SELECT * FROM t2 WHERE attr1 = 1;
pk1	attr1	attr2	attr3
1	1	1	one
create database ndbtest;
alter table t2 rename ndbtest.t2;
SELECT * FROM ndbtest.t2 WHERE attr1 = 1;
pk1	attr1	attr2	attr3
1	1	1	one
drop table ndbtest.t2;
drop database ndbtest;
+36 −0
Original line number Diff line number Diff line
-- source include/have_ndb.inc
-- source include/not_embedded.inc

--disable_warnings
DROP TABLE IF EXISTS t1,t2;
drop database if exists mysqltest;
--enable_warnings

#
# Table rename tests
#

#
# Create a normal table with primary key
#
CREATE TABLE t1 (
  pk1 INT NOT NULL PRIMARY KEY,
  attr1 INT NOT NULL,
  attr2 INT,
  attr3 VARCHAR(10),
  INDEX i1(attr1)
) ENGINE=ndbcluster;

INSERT INTO t1 VALUES (0,0,0,"zero"),(1,1,1,"one"),(2,2,2,"two");
SELECT * FROM t1 WHERE attr1 = 1;
alter table t1 rename t2;
SELECT * FROM t2 WHERE attr1 = 1;

create database ndbtest;
alter table t2 rename ndbtest.t2;
SELECT * FROM ndbtest.t2 WHERE attr1 = 1;

drop table ndbtest.t2;
drop database ndbtest;

# End of 4.1 tests
+35 −0
Original line number Diff line number Diff line
@@ -4922,13 +4922,17 @@ int ha_ndbcluster::rename_table(const char *from, const char *to)
{
  NDBDICT *dict;
  char old_dbname[FN_HEADLEN];
  char new_dbname[FN_HEADLEN];
  char new_tabname[FN_HEADLEN];
  const NDBTAB *orig_tab;
  int result;
  bool recreate_indexes= FALSE;
  NDBDICT::List index_list;

  DBUG_ENTER("ha_ndbcluster::rename_table");
  DBUG_PRINT("info", ("Renaming %s to %s", from, to));
  set_dbname(from, old_dbname);
  set_dbname(to, new_dbname);
  set_tabname(from);
  set_tabname(to, new_tabname);

@@ -4953,6 +4957,11 @@ int ha_ndbcluster::rename_table(const char *from, const char *to)
    DBUG_ASSERT(r == 0);
  }
#endif
  if (my_strcasecmp(system_charset_info, new_dbname, old_dbname))
  {
    dict->listIndexes(index_list, *orig_tab);    
    recreate_indexes= TRUE;
  }
  // Change current database to that of target table
  set_dbname(to);
  ndb->setDatabaseName(m_dbname);
@@ -5033,6 +5042,32 @@ int ha_ndbcluster::rename_table(const char *from, const char *to)
                               SOT_RENAME_TABLE,
                               m_dbname, new_tabname);
  }

  // If we are moving tables between databases, we need to recreate
  // indexes
  if (recreate_indexes)
  {
    for (unsigned i = 0; i < index_list.count; i++) 
    {
        NDBDICT::List::Element& index_el = index_list.elements[i];
	// Recreate any indexes not stored in the system database
	if (my_strcasecmp(system_charset_info, 
			  index_el.database, NDB_SYSTEM_DATABASE))
	{
	  set_dbname(from);
	  ndb->setDatabaseName(m_dbname);
	  const NDBINDEX * index= dict->getIndexGlobal(index_el.name,  new_tab);
	  DBUG_PRINT("info", ("Creating index %s/%s",
			      index_el.database, index->getName()));
	  dict->createIndex(*index, new_tab);
	  DBUG_PRINT("info", ("Dropping index %s/%s",
			      index_el.database, index->getName()));
	  set_dbname(from);
	  ndb->setDatabaseName(m_dbname);
	  dict->dropIndexGlobal(*index);
	}
    }
  }
  if (share)
    free_share(&share);
#endif
+5 −0
Original line number Diff line number Diff line
@@ -1001,6 +1001,9 @@ typedef void (* NdbEventCallback)(NdbEventOperation*, Ndb*, void*);
#define WAITFOR_RESPONSE_TIMEOUT 120000 // Milliseconds
#endif

#define NDB_SYSTEM_DATABASE "sys"
#define NDB_SYSTEM_SCHEMA "def"

/**
 * @class Ndb 
 * @brief Represents the NDB kernel and is the main class of the NDB API.
@@ -1648,6 +1651,8 @@ private:
  const char * externalizeIndexName(const char * internalIndexName,
                                    bool fullyQualifiedNames);
  const char * externalizeIndexName(const char * internalIndexName);
  const BaseString old_internalize_index_name(const NdbTableImpl * table,
					      const char * external_name) const;
  const BaseString internalize_index_name(const NdbTableImpl * table,
                                          const char * external_name) const;

+10 −0
Original line number Diff line number Diff line
@@ -1635,6 +1635,16 @@ public:
    int listIndexes(List & list, const char * tableName);
    int listIndexes(List & list, const char * tableName) const;

#ifndef DOXYGEN_SHOULD_SKIP_INTERNAL
    /**
     * Fetch list of indexes of given table.
     * @param list  Reference to list where to store the listed indexes
     * @param table  Reference to table that index belongs to.
     * @return  0 if successful, otherwise -1
     */
    int listIndexes(List & list, const Table &table) const;
#endif

    /** @} *******************************************************************/
    /** 
     * @name Events
Loading