You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
10 KiB

21 years ago
18 years ago
22 years ago
  1. /* Copyright (C) 2000-2006 MySQL AB
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /*
  13. Atomic rename of table; RENAME TABLE t1 to t2, tmp to t1 [,...]
  14. */
  15. #include "mysql_priv.h"
  16. #include "sql_trigger.h"
  17. static TABLE_LIST *rename_tables(THD *thd, TABLE_LIST *table_list,
  18. bool skip_error);
  19. static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list);
  20. /*
  21. Every second entry in the table_list is the original name and every
  22. second entry is the new name.
  23. */
  24. bool mysql_rename_tables(THD *thd, TABLE_LIST *table_list, bool silent)
  25. {
  26. bool error= 1;
  27. TABLE_LIST *ren_table= 0;
  28. int to_table;
  29. char *rename_log_table[2]= {NULL, NULL};
  30. DBUG_ENTER("mysql_rename_tables");
  31. /*
  32. Avoid problems with a rename on a table that we have locked or
  33. if the user is trying to to do this in a transcation context
  34. */
  35. if (thd->locked_tables || thd->active_transaction())
  36. {
  37. my_message(ER_LOCK_OR_ACTIVE_TRANSACTION,
  38. ER(ER_LOCK_OR_ACTIVE_TRANSACTION), MYF(0));
  39. DBUG_RETURN(1);
  40. }
  41. mysql_ha_rm_tables(thd, table_list, FALSE);
  42. if (wait_if_global_read_lock(thd,0,1))
  43. DBUG_RETURN(1);
  44. if (logger.is_log_table_enabled(QUERY_LOG_GENERAL) ||
  45. logger.is_log_table_enabled(QUERY_LOG_SLOW))
  46. {
  47. /*
  48. Rules for rename of a log table:
  49. IF 1. Log tables are enabled
  50. AND 2. Rename operates on the log table and nothing is being
  51. renamed to the log table.
  52. DO 3. Throw an error message.
  53. ELSE 4. Perform rename.
  54. */
  55. for (to_table= 0, ren_table= table_list; ren_table;
  56. to_table= 1 - to_table, ren_table= ren_table->next_local)
  57. {
  58. int log_table_rename= 0;
  59. if ((log_table_rename=
  60. check_if_log_table(ren_table->db_length, ren_table->db,
  61. ren_table->table_name_length,
  62. ren_table->table_name, 1)))
  63. {
  64. /*
  65. as we use log_table_rename as an array index, we need it to start
  66. with 0, while QUERY_LOG_SLOW == 1 and QUERY_LOG_GENERAL == 2.
  67. So, we shift the value to start with 0;
  68. */
  69. log_table_rename--;
  70. if (rename_log_table[log_table_rename])
  71. {
  72. if (to_table)
  73. rename_log_table[log_table_rename]= NULL;
  74. else
  75. {
  76. /*
  77. Two renames of "log_table TO" w/o rename "TO log_table" in
  78. between.
  79. */
  80. my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
  81. ren_table->table_name);
  82. DBUG_RETURN(1);
  83. }
  84. }
  85. else
  86. {
  87. if (to_table)
  88. {
  89. /*
  90. Attempt to rename a table TO log_table w/o renaming
  91. log_table TO some table.
  92. */
  93. my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), ren_table->table_name,
  94. ren_table->table_name);
  95. DBUG_RETURN(1);
  96. }
  97. else
  98. {
  99. /* save the name of the log table to report an error */
  100. rename_log_table[log_table_rename]= ren_table->table_name;
  101. }
  102. }
  103. }
  104. }
  105. if (rename_log_table[0] || rename_log_table[1])
  106. {
  107. if (rename_log_table[0])
  108. my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[0],
  109. rename_log_table[0]);
  110. else
  111. my_error(ER_CANT_RENAME_LOG_TABLE, MYF(0), rename_log_table[1],
  112. rename_log_table[1]);
  113. DBUG_RETURN(1);
  114. }
  115. }
  116. pthread_mutex_lock(&LOCK_open);
  117. if (lock_table_names_exclusively(thd, table_list))
  118. {
  119. pthread_mutex_unlock(&LOCK_open);
  120. goto err;
  121. }
  122. error=0;
  123. if ((ren_table=rename_tables(thd,table_list,0)))
  124. {
  125. /* Rename didn't succeed; rename back the tables in reverse order */
  126. TABLE_LIST *table;
  127. /* Reverse the table list */
  128. table_list= reverse_table_list(table_list);
  129. /* Find the last renamed table */
  130. for (table= table_list;
  131. table->next_local != ren_table ;
  132. table= table->next_local->next_local) ;
  133. table= table->next_local->next_local; // Skip error table
  134. /* Revert to old names */
  135. rename_tables(thd, table, 1);
  136. /* Revert the table list (for prepared statements) */
  137. table_list= reverse_table_list(table_list);
  138. error= 1;
  139. }
  140. /*
  141. An exclusive lock on table names is satisfactory to ensure
  142. no other thread accesses this table.
  143. However, NDB assumes that handler::rename_tables is called under
  144. LOCK_open. And it indeed is, from ALTER TABLE.
  145. TODO: remove this limitation.
  146. We still should unlock LOCK_open as early as possible, to provide
  147. higher concurrency - query_cache_invalidate can take minutes to
  148. complete.
  149. */
  150. pthread_mutex_unlock(&LOCK_open);
  151. /* Lets hope this doesn't fail as the result will be messy */
  152. if (!silent && !error)
  153. {
  154. write_bin_log(thd, TRUE, thd->query(), thd->query_length());
  155. my_ok(thd);
  156. }
  157. if (!error)
  158. query_cache_invalidate3(thd, table_list, 0);
  159. pthread_mutex_lock(&LOCK_open);
  160. unlock_table_names(thd, table_list, (TABLE_LIST*) 0);
  161. pthread_mutex_unlock(&LOCK_open);
  162. err:
  163. start_waiting_global_read_lock(thd);
  164. DBUG_RETURN(error);
  165. }
  166. /*
  167. reverse table list
  168. SYNOPSIS
  169. reverse_table_list()
  170. table_list pointer to table _list
  171. RETURN
  172. pointer to new (reversed) list
  173. */
  174. static TABLE_LIST *reverse_table_list(TABLE_LIST *table_list)
  175. {
  176. TABLE_LIST *prev= 0;
  177. while (table_list)
  178. {
  179. TABLE_LIST *next= table_list->next_local;
  180. table_list->next_local= prev;
  181. prev= table_list;
  182. table_list= next;
  183. }
  184. return (prev);
  185. }
  186. /*
  187. Rename a single table or a view
  188. SYNPOSIS
  189. do_rename()
  190. thd Thread handle
  191. ren_table A table/view to be renamed
  192. new_db The database to which the table to be moved to
  193. new_table_name The new table/view name
  194. new_table_alias The new table/view alias
  195. skip_error Whether to skip error
  196. DESCRIPTION
  197. Rename a single table or a view.
  198. RETURN
  199. false Ok
  200. true rename failed
  201. */
  202. bool
  203. do_rename(THD *thd, TABLE_LIST *ren_table, char *new_db, char *new_table_name,
  204. char *new_table_alias, bool skip_error)
  205. {
  206. int rc= 1;
  207. char name[FN_REFLEN + 1];
  208. const char *new_alias, *old_alias;
  209. frm_type_enum frm_type;
  210. enum legacy_db_type table_type;
  211. DBUG_ENTER("do_rename");
  212. if (lower_case_table_names == 2)
  213. {
  214. old_alias= ren_table->alias;
  215. new_alias= new_table_alias;
  216. }
  217. else
  218. {
  219. old_alias= ren_table->table_name;
  220. new_alias= new_table_name;
  221. }
  222. DBUG_ASSERT(new_alias);
  223. build_table_filename(name, sizeof(name) - 1,
  224. new_db, new_alias, reg_ext, 0);
  225. if (!access(name,F_OK))
  226. {
  227. my_error(ER_TABLE_EXISTS_ERROR, MYF(0), new_alias);
  228. DBUG_RETURN(1); // This can't be skipped
  229. }
  230. build_table_filename(name, sizeof(name) - 1,
  231. ren_table->db, old_alias, reg_ext, 0);
  232. frm_type= mysql_frm_type(thd, name, &table_type);
  233. switch (frm_type)
  234. {
  235. case FRMTYPE_TABLE:
  236. {
  237. if (!(rc= mysql_rename_table(ha_resolve_by_legacy_type(thd,
  238. table_type),
  239. ren_table->db, old_alias,
  240. new_db, new_alias, 0)))
  241. {
  242. if ((rc= Table_triggers_list::change_table_name(thd, ren_table->db,
  243. old_alias,
  244. new_db,
  245. new_alias)))
  246. {
  247. /*
  248. We've succeeded in renaming table's .frm and in updating
  249. corresponding handler data, but have failed to update table's
  250. triggers appropriately. So let us revert operations on .frm
  251. and handler's data and report about failure to rename table.
  252. */
  253. (void) mysql_rename_table(ha_resolve_by_legacy_type(thd,
  254. table_type),
  255. new_db, new_alias,
  256. ren_table->db, old_alias, 0);
  257. }
  258. }
  259. }
  260. break;
  261. case FRMTYPE_VIEW:
  262. /*
  263. change of schema is not allowed
  264. except of ALTER ...UPGRADE DATA DIRECTORY NAME command
  265. because a view has valid internal db&table names in this case.
  266. */
  267. if (thd->lex->sql_command != SQLCOM_ALTER_DB_UPGRADE &&
  268. strcmp(ren_table->db, new_db))
  269. my_error(ER_FORBID_SCHEMA_CHANGE, MYF(0), ren_table->db,
  270. new_db);
  271. else
  272. rc= mysql_rename_view(thd, new_db, new_alias, ren_table);
  273. break;
  274. default:
  275. DBUG_ASSERT(0); // should never happen
  276. case FRMTYPE_ERROR:
  277. my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
  278. break;
  279. }
  280. if (rc && !skip_error)
  281. DBUG_RETURN(1);
  282. DBUG_RETURN(0);
  283. }
  284. /*
  285. Rename all tables in list; Return pointer to wrong entry if something goes
  286. wrong. Note that the table_list may be empty!
  287. */
  288. /*
  289. Rename tables/views in the list
  290. SYNPOSIS
  291. rename_tables()
  292. thd Thread handle
  293. table_list List of tables to rename
  294. skip_error Whether to skip errors
  295. DESCRIPTION
  296. Take a table/view name from and odd list element and rename it to a
  297. the name taken from list element+1. Note that the table_list may be
  298. empty.
  299. RETURN
  300. false Ok
  301. true rename failed
  302. */
  303. static TABLE_LIST *
  304. rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
  305. {
  306. TABLE_LIST *ren_table, *new_table;
  307. DBUG_ENTER("rename_tables");
  308. for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
  309. {
  310. new_table= ren_table->next_local;
  311. if (do_rename(thd, ren_table, new_table->db, new_table->table_name,
  312. new_table->alias, skip_error))
  313. DBUG_RETURN(ren_table);
  314. }
  315. DBUG_RETURN(0);
  316. }