1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
20
#include "mysql_priv.h"
22
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
25
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
28
Every second entry in the table_list is the original name and every
29
second entry is the new name.
32
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
35
TABLE_LIST *ren_table= 0;
36
DBUG_ENTER("mysql_rename_tables");
39
Avoid problems with a rename on a table that we have locked or
40
if the user is trying to to do this in a transcation context
43
if (thd->locked_tables || thd->active_transaction())
45
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
46
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
50
mysql_ha_rm_tables(thd, table_list, FALSE);
52
if (wait_if_global_read_lock(thd,0,1))
55
pthread_mutex_lock(&LOCK_open);
56
if (lock_table_names_exclusively(thd, table_list))
58
pthread_mutex_unlock(&LOCK_open);
63
if ((ren_table=rename_tables(thd,table_list,0)))
65
/* Rename didn't succeed; rename back the tables in reverse order */
68
/* Reverse the table list */
69
table_list= reverse_table_list(table_list);
71
/* Find the last renamed table */
72
for (table= table_list;
73
table->next_local != ren_table ;
74
table= table->next_local->next_local) ;
75
table= table->next_local->next_local; // Skip error table
76
/* Revert to old names */
77
rename_tables(thd, table, 1);
79
/* Revert the table list (for prepared statements) */
80
table_list= reverse_table_list(table_list);
85
An exclusive lock on table names is satisfactory to ensure
86
no other thread accesses this table.
87
We still should unlock LOCK_open as early as possible, to provide
88
higher concurrency - query_cache_invalidate can take minutes to
91
pthread_mutex_unlock(&LOCK_open);
93
/* Lets hope this doesn't fail as the result will be messy */
94
if (!silent && !error)
96
write_bin_log(thd, TRUE, thd->query, thd->query_length);
100
pthread_mutex_lock(&LOCK_open);
101
unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
102
pthread_mutex_unlock(&LOCK_open);
105
start_waiting_global_read_lock(thd);
115
table_list pointer to table _list
118
pointer to new (reversed) list
120
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
126
TABLE_LIST *next= table_list->next_local;
127
table_list->next_local= prev;
136
Rename a single table or a view
141
ren_table A table/view to be renamed
142
new_db The database to which the table to be moved to
143
new_table_name The new table/view name
144
new_table_alias The new table/view alias
145
skip_error Whether to skip error
148
Rename a single table or a view.
156
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
157
char *new_table_alias, bool skip_error)
160
char name[FN_REFLEN];
161
const char *new_alias, *old_alias;
162
enum legacy_db_type table_type;
164
DBUG_ENTER("do_rename");
166
if (lower_case_table_names == 2)
168
old_alias= ren_table->alias;
169
new_alias= new_table_alias;
173
old_alias= ren_table->table_name;
174
new_alias= new_table_name;
176
build_table_filename(name, sizeof(name),
177
new_db, new_alias, reg_ext, 0);
178
if (!access(name,F_OK))
180
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
181
DBUG_RETURN(1); // This can't be skipped
183
build_table_filename(name, sizeof(name),
184
ren_table->db, old_alias, reg_ext, 0);
186
rc= mysql_rename_table(ha_resolve_by_legacy_type(thd, table_type),
187
ren_table->db, old_alias,
188
new_db, new_alias, 0);
189
if (rc && !skip_error)
196
Rename all tables in list; Return pointer to wrong entry if something goes
197
wrong. Note that the table_list may be empty!
201
Rename tables/views in the list
206
table_list List of tables to rename
207
skip_error Whether to skip errors
210
Take a table/view name from and odd list element and rename it to a
211
the name taken from list element+1. Note that the table_list may be
220
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
222
TABLE_LIST *ren_table, *new_table;
224
DBUG_ENTER("rename_tables");
226
for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
228
new_table= ren_table->next_local;
229
if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
230
new_table->alias, skip_error))
231
DBUG_RETURN(ren_table);