1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2006 MySQL AB
|
2 |
||
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.
|
|
6 |
||
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.
|
|
11 |
||
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 */
|
|
15 |
||
16 |
/*
|
|
17 |
Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
|
|
18 |
*/
|
|
19 |
||
20 |
#include "mysql_priv.h" |
|
21 |
||
22 |
static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list, |
|
23 |
bool skip_error); |
|
24 |
||
25 |
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list); |
|
26 |
||
27 |
/*
|
|
28 |
Every second entry in the table_list is the original name and every
|
|
29 |
second entry is the new name.
|
|
30 |
*/
|
|
31 |
||
32 |
bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent) |
|
33 |
{
|
|
34 |
bool error= 1; |
|
35 |
TABLE_LIST *ren_table= 0; |
|
36 |
DBUG_ENTER("mysql_rename_tables"); |
|
37 |
||
38 |
/*
|
|
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
|
|
41 |
*/
|
|
42 |
||
43 |
if (thd->locked_tables || thd->active_transaction()) |
|
44 |
{
|
|
45 |
my_message(ER_LOCK_OR_ACTIVE_TRANSACTION, |
|
46 |
ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0)); |
|
47 |
DBUG_RETURN(1); |
|
48 |
}
|
|
49 |
||
50 |
mysql_ha_rm_tables(thd, table_list, FALSE); |
|
51 |
||
52 |
if (wait_if_global_read_lock(thd,0,1)) |
|
53 |
DBUG_RETURN(1); |
|
54 |
||
55 |
pthread_mutex_lock(&LOCK_open); |
|
56 |
if (lock_table_names_exclusively(thd, table_list)) |
|
57 |
{
|
|
58 |
pthread_mutex_unlock(&LOCK_open); |
|
59 |
goto err; |
|
60 |
}
|
|
61 |
||
62 |
error=0; |
|
63 |
if ((ren_table=rename_tables(thd,table_list,0))) |
|
64 |
{
|
|
65 |
/* Rename didn't succeed; rename back the tables in reverse order */
|
|
66 |
TABLE_LIST *table; |
|
67 |
||
68 |
/* Reverse the table list */
|
|
69 |
table_list= reverse_table_list(table_list); |
|
70 |
||
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); |
|
78 |
||
79 |
/* Revert the table list (for prepared statements) */
|
|
80 |
table_list= reverse_table_list(table_list); |
|
81 |
||
82 |
error= 1; |
|
83 |
}
|
|
84 |
/*
|
|
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
|
|
89 |
complete.
|
|
90 |
*/
|
|
91 |
pthread_mutex_unlock(&LOCK_open); |
|
92 |
||
93 |
/* Lets hope this doesn't fail as the result will be messy */
|
|
94 |
if (!silent && !error) |
|
95 |
{
|
|
96 |
write_bin_log(thd, TRUE, thd->query, thd->query_length); |
|
97 |
my_ok(thd); |
|
98 |
}
|
|
99 |
||
100 |
pthread_mutex_lock(&LOCK_open); |
|
101 |
unlock_table_names(thd, table_list, (TABLE_LIST*) 0); |
|
102 |
pthread_mutex_unlock(&LOCK_open); |
|
103 |
||
104 |
err: |
|
105 |
start_waiting_global_read_lock(thd); |
|
106 |
DBUG_RETURN(error); |
|
107 |
}
|
|
108 |
||
109 |
||
110 |
/*
|
|
111 |
reverse table list
|
|
112 |
||
113 |
SYNOPSIS
|
|
114 |
reverse_table_list()
|
|
115 |
table_list pointer to table _list
|
|
116 |
||
117 |
RETURN
|
|
118 |
pointer to new (reversed) list
|
|
119 |
*/
|
|
120 |
static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list) |
|
121 |
{
|
|
122 |
TABLE_LIST *prev= 0; |
|
123 |
||
124 |
while (table_list) |
|
125 |
{
|
|
126 |
TABLE_LIST *next= table_list->next_local; |
|
127 |
table_list->next_local= prev; |
|
128 |
prev= table_list; |
|
129 |
table_list= next; |
|
130 |
}
|
|
131 |
return (prev); |
|
132 |
}
|
|
133 |
||
134 |
||
135 |
/*
|
|
136 |
Rename a single table or a view
|
|
137 |
||
138 |
SYNPOSIS
|
|
139 |
do_rename()
|
|
140 |
thd Thread handle
|
|
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
|
|
146 |
||
147 |
DESCRIPTION
|
|
148 |
Rename a single table or a view.
|
|
149 |
||
150 |
RETURN
|
|
151 |
false Ok
|
|
152 |
true rename failed
|
|
153 |
*/
|
|
154 |
||
155 |
bool
|
|
156 |
do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name, |
|
157 |
char *new_table_alias, bool skip_error) |
|
158 |
{
|
|
159 |
int rc= 1; |
|
160 |
char name[FN_REFLEN]; |
|
161 |
const char *new_alias, *old_alias; |
|
162 |
enum legacy_db_type table_type; |
|
163 |
||
164 |
DBUG_ENTER("do_rename"); |
|
165 |
||
166 |
if (lower_case_table_names == 2) |
|
167 |
{
|
|
168 |
old_alias= ren_table->alias; |
|
169 |
new_alias= new_table_alias; |
|
170 |
}
|
|
171 |
else
|
|
172 |
{
|
|
173 |
old_alias= ren_table->table_name; |
|
174 |
new_alias= new_table_name; |
|
175 |
}
|
|
176 |
build_table_filename(name, sizeof(name), |
|
177 |
new_db, new_alias, reg_ext, 0); |
|
178 |
if (!access(name,F_OK)) |
|
179 |
{
|
|
180 |
my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias); |
|
181 |
DBUG_RETURN(1); // This can't be skipped |
|
182 |
}
|
|
183 |
build_table_filename(name, sizeof(name), |
|
184 |
ren_table->db, old_alias, reg_ext, 0); |
|
185 |
||
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) |
|
190 |
DBUG_RETURN(1); |
|
191 |
||
192 |
DBUG_RETURN(0); |
|
193 |
||
194 |
}
|
|
195 |
/*
|
|
196 |
Rename all tables in list; Return pointer to wrong entry if something goes
|
|
197 |
wrong. Note that the table_list may be empty!
|
|
198 |
*/
|
|
199 |
||
200 |
/*
|
|
201 |
Rename tables/views in the list
|
|
202 |
||
203 |
SYNPOSIS
|
|
204 |
rename_tables()
|
|
205 |
thd Thread handle
|
|
206 |
table_list List of tables to rename
|
|
207 |
skip_error Whether to skip errors
|
|
208 |
||
209 |
DESCRIPTION
|
|
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
|
|
212 |
empty.
|
|
213 |
||
214 |
RETURN
|
|
215 |
false Ok
|
|
216 |
true rename failed
|
|
217 |
*/
|
|
218 |
||
219 |
static TABLE_LIST * |
|
220 |
rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error) |
|
221 |
{
|
|
222 |
TABLE_LIST *ren_table, *new_table; |
|
223 |
||
224 |
DBUG_ENTER("rename_tables"); |
|
225 |
||
226 |
for (ren_table= table_list; ren_table; ren_table= new_table->next_local) |
|
227 |
{
|
|
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); |
|
232 |
}
|
|
233 |
DBUG_RETURN(0); |
|
234 |
}
|