2009-12-11 20:45:44 +01:00
|
|
|
/* Copyright (C) 2000-2006 MySQL AB, 2008-2009 Sun Microsystems, Inc
|
2000-08-21 02:07:54 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2000-08-21 02:07:54 +02:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
/*
|
|
|
|
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
|
|
|
|
*/
|
|
|
|
|
2010-03-31 16:05:33 +02:00
|
|
|
#include "sql_priv.h"
|
|
|
|
#include "unireg.h"
|
|
|
|
#include "sql_rename.h"
|
|
|
|
#include "sql_cache.h" // query_cache_*
|
|
|
|
#include "sql_table.h" // build_table_filename
|
|
|
|
#include "sql_view.h" // mysql_frm_type, mysql_rename_view
|
2006-02-24 21:50:36 +01:00
|
|
|
#include "sql_trigger.h"
|
2010-03-31 16:05:33 +02:00
|
|
|
#include "lock.h" // wait_if_global_read_lock, lock_table_names,
|
|
|
|
// unlock_table_names,
|
|
|
|
// start_waiting_global_read_lock
|
|
|
|
#include "sql_base.h" // tdc_remove_table
|
|
|
|
#include "sql_handler.h" // mysql_ha_rm_tables
|
2010-05-25 22:01:38 +02:00
|
|
|
#include "datadict.h"
|
2000-08-21 02:07:54 +02:00
|
|
|
|
2000-08-21 23:18:32 +02:00
|
|
|
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
|
|
|
|
bool skip_error);
|
2000-08-21 02:07:54 +02:00
|
|
|
|
2004-04-05 23:10:43 +02:00
|
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
|
|
|
|
|
2000-08-21 02:07:54 +02:00
|
|
|
/*
|
|
|
|
Every second entry in the table_list is the original name and every
|
|
|
|
second entry is the new name.
|
|
|
|
*/
|
|
|
|
|
2006-02-13 08:49:28 +01:00
|
|
|
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
2003-03-04 12:36:59 +01:00
|
|
|
bool error= 1;
|
2010-02-04 21:15:47 +01:00
|
|
|
bool binlog_error= 0;
|
2006-11-30 02:40:42 +01:00
|
|
|
TABLE_LIST *ren_table= 0;
|
2006-10-13 15:26:46 +02:00
|
|
|
int to_table;
|
|
|
|
char *rename_log_table[2]= {NULL, NULL};
|
2000-08-21 02:07:54 +02:00
|
|
|
DBUG_ENTER("mysql_rename_tables");
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2002-01-30 14:32:48 +01:00
|
|
|
/*
|
|
|
|
Avoid problems with a rename on a table that we have locked or
|
|
|
|
if the user is trying to to do this in a transcation context
|
|
|
|
*/
|
2000-08-21 02:07:54 +02:00
|
|
|
|
2010-05-06 00:02:08 +02:00
|
|
|
if (thd->locked_tables_mode || thd->in_active_multi_stmt_transaction())
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
2004-11-12 13:34:00 +01:00
|
|
|
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
|
|
|
|
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
|
2000-08-21 02:07:54 +02:00
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
2001-12-06 13:10:51 +01:00
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
mysql_ha_rm_tables(thd, table_list);
|
2007-11-20 18:17:53 +01:00
|
|
|
|
Implement new type-of-operation-aware metadata locks.
Add a wait-for graph based deadlock detector to the
MDL subsystem.
Fixes bug #46272 "MySQL 5.4.4, new MDL: unnecessary deadlock" and
bug #37346 "innodb does not detect deadlock between update and
alter table".
The first bug manifested itself as an unwarranted abort of a
transaction with ER_LOCK_DEADLOCK error by a concurrent ALTER
statement, when this transaction tried to repeat use of a
table, which it has already used in a similar fashion before
ALTER started.
The second bug showed up as a deadlock between table-level
locks and InnoDB row locks, which was "detected" only after
innodb_lock_wait_timeout timeout.
A transaction would start using the table and modify a few
rows.
Then ALTER TABLE would come in, and start copying rows
into a temporary table. Eventually it would stumble on
the modified records and get blocked on a row lock.
The first transaction would try to do more updates, and get
blocked on thr_lock.c lock.
This situation of circular wait would only get resolved
by a timeout.
Both these bugs stemmed from inadequate solutions to the
problem of deadlocks occurring between different
locking subsystems.
In the first case we tried to avoid deadlocks between metadata
locking and table-level locking subsystems, when upgrading shared
metadata lock to exclusive one.
Transactions holding the shared lock on the table and waiting for
some table-level lock used to be aborted too aggressively.
We also allowed ALTER TABLE to start in presence of transactions
that modify the subject table. ALTER TABLE acquires
TL_WRITE_ALLOW_READ lock at start, and that block all writes
against the table (naturally, we don't want any writes to be lost
when switching the old and the new table). TL_WRITE_ALLOW_READ
lock, in turn, would block the started transaction on thr_lock.c
lock, should they do more updates. This, again, lead to the need
to abort such transactions.
The second bug occurred simply because we didn't have any
mechanism to detect deadlocks between the table-level locks
in thr_lock.c and row-level locks in InnoDB, other than
innodb_lock_wait_timeout.
This patch solves both these problems by moving lock conflicts
which are causing these deadlocks into the metadata locking
subsystem, thus making it possible to avoid or detect such
deadlocks inside MDL.
To do this we introduce new type-of-operation-aware metadata
locks, which allow MDL subsystem to know not only the fact that
transaction has used or is going to use some object but also what
kind of operation it has carried out or going to carry out on the
object.
This, along with the addition of a special kind of upgradable
metadata lock, allows ALTER TABLE to wait until all
transactions which has updated the table to go away.
This solves the second issue.
Another special type of upgradable metadata lock is acquired
by LOCK TABLE WRITE. This second lock type allows to solve the
first issue, since abortion of table-level locks in event of
DDL under LOCK TABLES becomes also unnecessary.
Below follows the list of incompatible changes introduced by
this patch:
- From now on, ALTER TABLE and CREATE/DROP TRIGGER SQL (i.e. those
statements that acquire TL_WRITE_ALLOW_READ lock)
wait for all transactions which has *updated* the table to
complete.
- From now on, LOCK TABLES ... WRITE, REPAIR/OPTIMIZE TABLE
(i.e. all statements which acquire TL_WRITE table-level lock) wait
for all transaction which *updated or read* from the table
to complete.
As a consequence, innodb_table_locks=0 option no longer applies
to LOCK TABLES ... WRITE.
- DROP DATABASE, DROP TABLE, RENAME TABLE no longer abort
statements or transactions which use tables being dropped or
renamed, and instead wait for these transactions to complete.
- Since LOCK TABLES WRITE now takes a special metadata lock,
not compatible with with reads or writes against the subject table
and transaction-wide, thr_lock.c deadlock avoidance algorithm
that used to ensure absence of deadlocks between LOCK TABLES
WRITE and other statements is no longer sufficient, even for
MyISAM. The wait-for graph based deadlock detector of MDL
subsystem may sometimes be necessary and is involved. This may
lead to ER_LOCK_DEADLOCK error produced for multi-statement
transactions even if these only use MyISAM:
session 1: session 2:
begin;
update t1 ... lock table t2 write, t1 write;
-- gets a lock on t2, blocks on t1
update t2 ...
(ER_LOCK_DEADLOCK)
- Finally, support of LOW_PRIORITY option for LOCK TABLES ... WRITE
was abandoned.
LOCK TABLE ... LOW_PRIORITY WRITE from now on has the same
priority as the usual LOCK TABLE ... WRITE.
SELECT HIGH PRIORITY no longer trumps LOCK TABLE ... WRITE in
the wait queue.
- We do not take upgradable metadata locks on implicitly
locked tables. So if one has, say, a view v1 that uses
table t1, and issues:
LOCK TABLE v1 WRITE;
FLUSH TABLE t1; -- (or just 'FLUSH TABLES'),
an error is produced.
In order to be able to perform DDL on a table under LOCK TABLES,
the table must be locked explicitly in the LOCK TABLES list.
2010-02-01 12:43:06 +01:00
|
|
|
if (thd->global_read_lock.wait_if_global_read_lock(thd, FALSE, TRUE))
|
2004-04-01 19:47:09 +02:00
|
|
|
DBUG_RETURN(1);
|
2006-10-13 15:26:46 +02:00
|
|
|
|
|
|
|
if (logger.is_log_table_enabled(QUERY_LOG_GENERAL) ||
|
|
|
|
logger.is_log_table_enabled(QUERY_LOG_SLOW))
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
Rules for rename of a log table:
|
|
|
|
|
|
|
|
IF 1. Log tables are enabled
|
|
|
|
AND 2. Rename operates on the log table and nothing is being
|
|
|
|
renamed to the log table.
|
|
|
|
DO 3. Throw an error message.
|
|
|
|
ELSE 4. Perform rename.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (to_table= 0, ren_table= table_list; ren_table;
|
|
|
|
to_table= 1 - to_table, ren_table= ren_table->next_local)
|
|
|
|
{
|
|
|
|
int log_table_rename= 0;
|
|
|
|
|
|
|
|
if ((log_table_rename=
|
|
|
|
check_if_log_table(ren_table->db_length, ren_table->db,
|
|
|
|
ren_table->table_name_length,
|
|
|
|
ren_table->table_name, 1)))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
as we use log_table_rename as an array index, we need it to start
|
|
|
|
with 0, while QUERY_LOG_SLOW == 1 and QUERY_LOG_GENERAL == 2.
|
|
|
|
So, we shift the value to start with 0;
|
|
|
|
*/
|
|
|
|
log_table_rename--;
|
|
|
|
if (rename_log_table[log_table_rename])
|
|
|
|
{
|
|
|
|
if (to_table)
|
|
|
|
rename_log_table[log_table_rename]= NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Two renames of "log_table TO" w/o rename "TO log_table" in
|
|
|
|
between.
|
|
|
|
*/
|
|
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
|
|
|
|
ren_table->table_name);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (to_table)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Attempt to rename a table TO log_table w/o renaming
|
|
|
|
log_table TO some table.
|
|
|
|
*/
|
|
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
|
|
|
|
ren_table->table_name);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* save the name of the log table to report an error */
|
|
|
|
rename_log_table[log_table_rename]= ren_table->table_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rename_log_table[0] || rename_log_table[1])
|
|
|
|
{
|
|
|
|
if (rename_log_table[0])
|
|
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[0],
|
|
|
|
rename_log_table[0]);
|
|
|
|
else
|
|
|
|
my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[1],
|
|
|
|
rename_log_table[1]);
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
if (lock_table_names(thd, table_list))
|
2003-03-03 19:42:49 +01:00
|
|
|
goto err;
|
2009-11-30 16:55:03 +01:00
|
|
|
|
2010-02-02 00:22:16 +01:00
|
|
|
mysql_mutex_lock(&LOCK_open);
|
2009-11-30 16:55:03 +01:00
|
|
|
|
|
|
|
for (ren_table= table_list; ren_table; ren_table= ren_table->next_local)
|
2009-12-01 20:13:01 +01:00
|
|
|
tdc_remove_table(thd, TDC_RT_REMOVE_ALL, ren_table->db,
|
|
|
|
ren_table->table_name);
|
2007-07-02 19:14:48 +02:00
|
|
|
|
2003-03-04 12:36:59 +01:00
|
|
|
error=0;
|
2003-03-03 19:42:49 +01:00
|
|
|
if ((ren_table=rename_tables(thd,table_list,0)))
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
|
|
|
/* Rename didn't succeed; rename back the tables in reverse order */
|
2004-04-05 23:10:43 +02:00
|
|
|
TABLE_LIST *table;
|
2000-08-21 23:18:32 +02:00
|
|
|
|
2004-04-05 23:10:43 +02:00
|
|
|
/* Reverse the table list */
|
|
|
|
table_list= reverse_table_list(table_list);
|
2000-08-21 02:07:54 +02:00
|
|
|
|
|
|
|
/* Find the last renamed table */
|
2004-07-16 00:15:55 +02:00
|
|
|
for (table= table_list;
|
|
|
|
table->next_local != ren_table ;
|
|
|
|
table= table->next_local->next_local) ;
|
|
|
|
table= table->next_local->next_local; // Skip error table
|
2000-08-21 02:07:54 +02:00
|
|
|
/* Revert to old names */
|
2000-08-21 23:18:32 +02:00
|
|
|
rename_tables(thd, table, 1);
|
2004-04-05 23:10:43 +02:00
|
|
|
|
|
|
|
/* Revert the table list (for prepared statements) */
|
|
|
|
table_list= reverse_table_list(table_list);
|
|
|
|
|
2003-03-03 19:42:49 +01:00
|
|
|
error= 1;
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|
2007-07-16 13:57:20 +02:00
|
|
|
/*
|
|
|
|
An exclusive lock on table names is satisfactory to ensure
|
|
|
|
no other thread accesses this table.
|
|
|
|
However, NDB assumes that handler::rename_tables is called under
|
|
|
|
LOCK_open. And it indeed is, from ALTER TABLE.
|
|
|
|
TODO: remove this limitation.
|
|
|
|
We still should unlock LOCK_open as early as possible, to provide
|
|
|
|
higher concurrency - query_cache_invalidate can take minutes to
|
|
|
|
complete.
|
|
|
|
*/
|
2009-12-10 04:19:51 +01:00
|
|
|
mysql_mutex_unlock(&LOCK_open);
|
2001-03-21 21:34:16 +01:00
|
|
|
|
2006-02-13 08:49:28 +01:00
|
|
|
if (!silent && !error)
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
2010-02-04 21:15:47 +01:00
|
|
|
binlog_error= write_bin_log(thd, TRUE, thd->query(), thd->query_length());
|
|
|
|
if (!binlog_error)
|
|
|
|
my_ok(thd);
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|
2001-03-21 21:34:16 +01:00
|
|
|
|
2007-07-02 19:14:48 +02:00
|
|
|
if (!error)
|
|
|
|
query_cache_invalidate3(thd, table_list, 0);
|
|
|
|
|
2009-11-30 16:55:03 +01:00
|
|
|
unlock_table_names(thd);
|
2003-03-03 19:42:49 +01:00
|
|
|
|
|
|
|
err:
|
Implement new type-of-operation-aware metadata locks.
Add a wait-for graph based deadlock detector to the
MDL subsystem.
Fixes bug #46272 "MySQL 5.4.4, new MDL: unnecessary deadlock" and
bug #37346 "innodb does not detect deadlock between update and
alter table".
The first bug manifested itself as an unwarranted abort of a
transaction with ER_LOCK_DEADLOCK error by a concurrent ALTER
statement, when this transaction tried to repeat use of a
table, which it has already used in a similar fashion before
ALTER started.
The second bug showed up as a deadlock between table-level
locks and InnoDB row locks, which was "detected" only after
innodb_lock_wait_timeout timeout.
A transaction would start using the table and modify a few
rows.
Then ALTER TABLE would come in, and start copying rows
into a temporary table. Eventually it would stumble on
the modified records and get blocked on a row lock.
The first transaction would try to do more updates, and get
blocked on thr_lock.c lock.
This situation of circular wait would only get resolved
by a timeout.
Both these bugs stemmed from inadequate solutions to the
problem of deadlocks occurring between different
locking subsystems.
In the first case we tried to avoid deadlocks between metadata
locking and table-level locking subsystems, when upgrading shared
metadata lock to exclusive one.
Transactions holding the shared lock on the table and waiting for
some table-level lock used to be aborted too aggressively.
We also allowed ALTER TABLE to start in presence of transactions
that modify the subject table. ALTER TABLE acquires
TL_WRITE_ALLOW_READ lock at start, and that block all writes
against the table (naturally, we don't want any writes to be lost
when switching the old and the new table). TL_WRITE_ALLOW_READ
lock, in turn, would block the started transaction on thr_lock.c
lock, should they do more updates. This, again, lead to the need
to abort such transactions.
The second bug occurred simply because we didn't have any
mechanism to detect deadlocks between the table-level locks
in thr_lock.c and row-level locks in InnoDB, other than
innodb_lock_wait_timeout.
This patch solves both these problems by moving lock conflicts
which are causing these deadlocks into the metadata locking
subsystem, thus making it possible to avoid or detect such
deadlocks inside MDL.
To do this we introduce new type-of-operation-aware metadata
locks, which allow MDL subsystem to know not only the fact that
transaction has used or is going to use some object but also what
kind of operation it has carried out or going to carry out on the
object.
This, along with the addition of a special kind of upgradable
metadata lock, allows ALTER TABLE to wait until all
transactions which has updated the table to go away.
This solves the second issue.
Another special type of upgradable metadata lock is acquired
by LOCK TABLE WRITE. This second lock type allows to solve the
first issue, since abortion of table-level locks in event of
DDL under LOCK TABLES becomes also unnecessary.
Below follows the list of incompatible changes introduced by
this patch:
- From now on, ALTER TABLE and CREATE/DROP TRIGGER SQL (i.e. those
statements that acquire TL_WRITE_ALLOW_READ lock)
wait for all transactions which has *updated* the table to
complete.
- From now on, LOCK TABLES ... WRITE, REPAIR/OPTIMIZE TABLE
(i.e. all statements which acquire TL_WRITE table-level lock) wait
for all transaction which *updated or read* from the table
to complete.
As a consequence, innodb_table_locks=0 option no longer applies
to LOCK TABLES ... WRITE.
- DROP DATABASE, DROP TABLE, RENAME TABLE no longer abort
statements or transactions which use tables being dropped or
renamed, and instead wait for these transactions to complete.
- Since LOCK TABLES WRITE now takes a special metadata lock,
not compatible with with reads or writes against the subject table
and transaction-wide, thr_lock.c deadlock avoidance algorithm
that used to ensure absence of deadlocks between LOCK TABLES
WRITE and other statements is no longer sufficient, even for
MyISAM. The wait-for graph based deadlock detector of MDL
subsystem may sometimes be necessary and is involved. This may
lead to ER_LOCK_DEADLOCK error produced for multi-statement
transactions even if these only use MyISAM:
session 1: session 2:
begin;
update t1 ... lock table t2 write, t1 write;
-- gets a lock on t2, blocks on t1
update t2 ...
(ER_LOCK_DEADLOCK)
- Finally, support of LOW_PRIORITY option for LOCK TABLES ... WRITE
was abandoned.
LOCK TABLE ... LOW_PRIORITY WRITE from now on has the same
priority as the usual LOCK TABLE ... WRITE.
SELECT HIGH PRIORITY no longer trumps LOCK TABLE ... WRITE in
the wait queue.
- We do not take upgradable metadata locks on implicitly
locked tables. So if one has, say, a view v1 that uses
table t1, and issues:
LOCK TABLE v1 WRITE;
FLUSH TABLE t1; -- (or just 'FLUSH TABLES'),
an error is produced.
In order to be able to perform DDL on a table under LOCK TABLES,
the table must be locked explicitly in the LOCK TABLES list.
2010-02-01 12:43:06 +01:00
|
|
|
thd->global_read_lock.start_waiting_global_read_lock(thd);
|
2010-02-04 21:15:47 +01:00
|
|
|
DBUG_RETURN(error || binlog_error);
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-05 23:10:43 +02:00
|
|
|
/*
|
|
|
|
reverse table list
|
|
|
|
|
|
|
|
SYNOPSIS
|
|
|
|
reverse_table_list()
|
|
|
|
table_list pointer to table _list
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
pointer to new (reversed) list
|
|
|
|
*/
|
|
|
|
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
|
|
|
|
{
|
|
|
|
TABLE_LIST *prev= 0;
|
|
|
|
|
|
|
|
while (table_list)
|
|
|
|
{
|
2004-07-16 00:15:55 +02:00
|
|
|
TABLE_LIST *next= table_list->next_local;
|
|
|
|
table_list->next_local= prev;
|
2004-04-05 23:10:43 +02:00
|
|
|
prev= table_list;
|
|
|
|
table_list= next;
|
|
|
|
}
|
|
|
|
return (prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-21 02:07:54 +02:00
|
|
|
/*
|
2006-10-13 19:59:52 +02:00
|
|
|
Rename a single table or a view
|
|
|
|
|
|
|
|
SYNPOSIS
|
|
|
|
do_rename()
|
|
|
|
thd Thread handle
|
|
|
|
ren_table A table/view to be renamed
|
|
|
|
new_db The database to which the table to be moved to
|
|
|
|
new_table_name The new table/view name
|
|
|
|
new_table_alias The new table/view alias
|
|
|
|
skip_error Whether to skip error
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Rename a single table or a view.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
false Ok
|
|
|
|
true rename failed
|
2000-08-21 02:07:54 +02:00
|
|
|
*/
|
|
|
|
|
2006-10-13 19:59:52 +02:00
|
|
|
bool
|
|
|
|
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
|
|
|
|
char *new_table_alias, bool skip_error)
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
2006-10-13 19:59:52 +02:00
|
|
|
int rc= 1;
|
2009-06-19 10:24:43 +02:00
|
|
|
char name[FN_REFLEN + 1];
|
2006-10-13 19:59:52 +02:00
|
|
|
const char *new_alias, *old_alias;
|
2005-09-16 17:13:21 +02:00
|
|
|
frm_type_enum frm_type;
|
2005-12-21 19:18:40 +01:00
|
|
|
enum legacy_db_type table_type;
|
2005-11-03 15:10:11 +01:00
|
|
|
|
2006-10-13 19:59:52 +02:00
|
|
|
DBUG_ENTER("do_rename");
|
2000-08-21 23:18:32 +02:00
|
|
|
|
2006-10-13 19:59:52 +02:00
|
|
|
if (lower_case_table_names == 2)
|
2000-08-21 02:07:54 +02:00
|
|
|
{
|
2006-10-13 19:59:52 +02:00
|
|
|
old_alias= ren_table->alias;
|
|
|
|
new_alias= new_table_alias;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
old_alias= ren_table->table_name;
|
2006-10-20 10:12:38 +02:00
|
|
|
new_alias= new_table_name;
|
2006-10-13 19:59:52 +02:00
|
|
|
}
|
2009-05-19 06:25:36 +02:00
|
|
|
DBUG_ASSERT(new_alias);
|
|
|
|
|
2009-06-19 10:24:43 +02:00
|
|
|
build_table_filename(name, sizeof(name) - 1,
|
2006-10-19 16:43:46 +02:00
|
|
|
new_db, new_alias, reg_ext, 0);
|
2006-10-13 19:59:52 +02:00
|
|
|
if (!access(name,F_OK))
|
|
|
|
{
|
|
|
|
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
|
2006-10-17 22:14:14 +02:00
|
|
|
DBUG_RETURN(1); // This can't be skipped
|
2006-10-13 19:59:52 +02:00
|
|
|
}
|
2009-06-19 10:24:43 +02:00
|
|
|
build_table_filename(name, sizeof(name) - 1,
|
2006-10-19 16:43:46 +02:00
|
|
|
ren_table->db, old_alias, reg_ext, 0);
|
2005-09-18 21:43:28 +02:00
|
|
|
|
2010-05-25 22:01:38 +02:00
|
|
|
frm_type= dd_frm_type(thd, name, &table_type);
|
2006-10-13 19:59:52 +02:00
|
|
|
switch (frm_type)
|
|
|
|
{
|
|
|
|
case FRMTYPE_TABLE:
|
2005-09-16 17:13:21 +02:00
|
|
|
{
|
2006-10-20 10:12:38 +02:00
|
|
|
if (!(rc= mysql_rename_table(ha_resolve_by_legacy_type(thd,
|
|
|
|
table_type),
|
|
|
|
ren_table->db, old_alias,
|
|
|
|
new_db, new_alias, 0)))
|
2006-02-24 21:50:36 +01:00
|
|
|
{
|
2006-10-13 19:59:52 +02:00
|
|
|
if ((rc= Table_triggers_list::change_table_name(thd, ren_table->db,
|
|
|
|
old_alias,
|
|
|
|
new_db,
|
|
|
|
new_alias)))
|
2006-02-24 21:50:36 +01:00
|
|
|
{
|
2006-10-13 19:59:52 +02:00
|
|
|
/*
|
|
|
|
We've succeeded in renaming table's .frm and in updating
|
|
|
|
corresponding handler data, but have failed to update table's
|
|
|
|
triggers appropriately. So let us revert operations on .frm
|
|
|
|
and handler's data and report about failure to rename table.
|
|
|
|
*/
|
2006-10-19 16:43:46 +02:00
|
|
|
(void) mysql_rename_table(ha_resolve_by_legacy_type(thd,
|
|
|
|
table_type),
|
|
|
|
new_db, new_alias,
|
|
|
|
ren_table->db, old_alias, 0);
|
2006-02-24 21:50:36 +01:00
|
|
|
}
|
|
|
|
}
|
2005-09-16 17:13:21 +02:00
|
|
|
}
|
2006-10-13 19:59:52 +02:00
|
|
|
break;
|
|
|
|
case FRMTYPE_VIEW:
|
2009-04-10 11:25:48 +02:00
|
|
|
/*
|
|
|
|
change of schema is not allowed
|
|
|
|
except of ALTER ...UPGRADE DATA DIRECTORY NAME command
|
|
|
|
because a view has valid internal db&table names in this case.
|
|
|
|
*/
|
|
|
|
if (thd->lex->sql_command != SQLCOM_ALTER_DB_UPGRADE &&
|
|
|
|
strcmp(ren_table->db, new_db))
|
2006-10-13 19:59:52 +02:00
|
|
|
my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db,
|
|
|
|
new_db);
|
|
|
|
else
|
2009-04-13 15:09:10 +02:00
|
|
|
rc= mysql_rename_view(thd, new_db, new_alias, ren_table);
|
2006-10-13 19:59:52 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DBUG_ASSERT(0); // should never happen
|
|
|
|
case FRMTYPE_ERROR:
|
|
|
|
my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (rc && !skip_error)
|
|
|
|
DBUG_RETURN(1);
|
|
|
|
|
|
|
|
DBUG_RETURN(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Rename all tables in list; Return pointer to wrong entry if something goes
|
|
|
|
wrong. Note that the table_list may be empty!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Rename tables/views in the list
|
|
|
|
|
|
|
|
SYNPOSIS
|
|
|
|
rename_tables()
|
|
|
|
thd Thread handle
|
|
|
|
table_list List of tables to rename
|
|
|
|
skip_error Whether to skip errors
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Take a table/view name from and odd list element and rename it to a
|
|
|
|
the name taken from list element+1. Note that the table_list may be
|
|
|
|
empty.
|
|
|
|
|
|
|
|
RETURN
|
|
|
|
false Ok
|
|
|
|
true rename failed
|
|
|
|
*/
|
|
|
|
|
|
|
|
static TABLE_LIST *
|
|
|
|
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
|
|
|
|
{
|
2006-11-30 17:25:05 +01:00
|
|
|
TABLE_LIST *ren_table, *new_table;
|
2006-10-13 19:59:52 +02:00
|
|
|
|
|
|
|
DBUG_ENTER("rename_tables");
|
|
|
|
|
|
|
|
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
|
|
|
|
{
|
|
|
|
new_table= ren_table->next_local;
|
|
|
|
if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
|
|
|
|
new_table->alias, skip_error))
|
2005-09-18 21:43:28 +02:00
|
|
|
DBUG_RETURN(ren_table);
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|
2000-08-21 23:18:32 +02:00
|
|
|
DBUG_RETURN(0);
|
2000-08-21 02:07:54 +02:00
|
|
|
}
|