Changes after discussion/review with Sanja

parent afe8dcf2
...@@ -333,6 +333,59 @@ err_w_file: ...@@ -333,6 +333,59 @@ err_w_file:
DBUG_RETURN(TRUE); DBUG_RETURN(TRUE);
} }
/*
Renames a frm file (including backups) in same schema
SYNOPSIS
rename_in_schema_file
schema name of given schema
old_name original file name
new_name new file name
revision revision number
num_view_backups number of backups
RETURN
0 - OK
1 - Error (only if renaming of frm failed)
*/
my_bool rename_in_schema_file(const char *schema, const char *old_name,
const char *new_name, ulonglong revision,
uint num_view_backups)
{
char old_path[FN_REFLEN], new_path[FN_REFLEN], arc_path[FN_REFLEN];
strxnmov(old_path, FN_REFLEN, mysql_data_home, "/", schema, "/",
old_name, reg_ext, NullS);
(void) unpack_filename(old_path, old_path);
strxnmov(new_path, FN_REFLEN, mysql_data_home, "/", schema, "/",
new_name, reg_ext, NullS);
(void) unpack_filename(new_path, new_path);
if (my_rename(old_path, new_path, MYF(MY_WME)))
return 1;
/* check if arc_dir exists */
strxnmov(arc_path, FN_REFLEN, mysql_data_home, "/", schema, "/arc", NullS);
(void) unpack_filename(arc_path, arc_path);
if (revision > 0 && !access(arc_path, F_OK))
{
ulonglong limit= (revision > num_view_backups) ? revision - num_view_backups : 0;
while (revision > limit) {
my_snprintf(old_path, FN_REFLEN, "%s/%s%s-%04lu",
arc_path, old_name, reg_ext, (ulong)revision);
(void) unpack_filename(old_path, old_path);
my_snprintf(new_path, FN_REFLEN, "%s/%s%s-%04lu",
arc_path, new_name, reg_ext, (ulong)revision);
(void) unpack_filename(new_path, new_path);
my_rename(old_path, new_path, MYF(0));
revision--;
}
}
return 0;
}
/* /*
Prepare frm to parse (read to memory) Prepare frm to parse (read to memory)
......
...@@ -48,6 +48,9 @@ my_bool ...@@ -48,6 +48,9 @@ my_bool
sql_create_definition_file(const LEX_STRING *dir, const LEX_STRING *file_name, sql_create_definition_file(const LEX_STRING *dir, const LEX_STRING *file_name,
const LEX_STRING *type, const LEX_STRING *type,
gptr base, File_option *parameters, uint versions); gptr base, File_option *parameters, uint versions);
my_bool rename_in_schema_file(const char *schema, const char *old_name,
const char *new_name, ulonglong revision,
uint num_view_backups);
class File_parser: public Sql_alloc class File_parser: public Sql_alloc
{ {
......
...@@ -138,7 +138,7 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error) ...@@ -138,7 +138,7 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
for (ren_table= table_list; ren_table; ren_table= new_table->next_local) for (ren_table= table_list; ren_table; ren_table= new_table->next_local)
{ {
db_type table_type; int rc= 1;
char name[FN_REFLEN]; char name[FN_REFLEN];
const char *new_alias, *old_alias; const char *new_alias, *old_alias;
...@@ -165,21 +165,20 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error) ...@@ -165,21 +165,20 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
ren_table->db, old_alias, ren_table->db, old_alias,
reg_ext); reg_ext);
unpack_filename(name, name); unpack_filename(name, name);
if ((frm_type= mysql_frm_type(name)) == FRMTYPE_TABLE &&
(table_type= get_table_type(thd, name)) == DB_TYPE_UNKNOWN) frm_type= mysql_frm_type(name);
{
my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
if (!skip_error)
DBUG_RETURN(ren_table);
}
else {
int rc= 1;
switch (frm_type) switch (frm_type)
{ {
case FRMTYPE_TABLE: case FRMTYPE_TABLE:
{
db_type table_type;
if ((table_type= get_table_type(thd, name)) == DB_TYPE_UNKNOWN)
my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
else
rc= mysql_rename_table(table_type, ren_table->db, old_alias, rc= mysql_rename_table(table_type, ren_table->db, old_alias,
new_table->db, new_alias); new_table->db, new_alias);
break; break;
}
case FRMTYPE_VIEW: case FRMTYPE_VIEW:
/* change of schema is not allowed */ /* change of schema is not allowed */
if (strcmp(ren_table->db, new_table->db)) if (strcmp(ren_table->db, new_table->db))
...@@ -188,13 +187,14 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error) ...@@ -188,13 +187,14 @@ rename_tables(THD *thd, TABLE_LIST *table_list, bool skip_error)
else else
rc= mysql_rename_view(thd, new_alias, ren_table); rc= mysql_rename_view(thd, new_alias, ren_table);
break; break;
case FRMTYPE_ERROR:
default: default:
DBUG_ASSERT(0); // should never happen
case FRMTYPE_ERROR:
my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno); my_error(ER_FILE_NOT_FOUND, MYF(0), name, my_errno);
break;
} }
if (rc && !skip_error) if (rc && !skip_error)
DBUG_RETURN(ren_table); DBUG_RETURN(ren_table);
} }
}
DBUG_RETURN(0); DBUG_RETURN(0);
} }
...@@ -1167,7 +1167,7 @@ frm_type_enum mysql_frm_type(char *path) ...@@ -1167,7 +1167,7 @@ frm_type_enum mysql_frm_type(char *path)
int length; int length;
DBUG_ENTER("mysql_frm_type"); DBUG_ENTER("mysql_frm_type");
if ((file= my_open(path, O_RDONLY | O_SHARE, MYF(MY_WME))) < 0) if ((file= my_open(path, O_RDONLY | O_SHARE, MYF(0))) < 0)
{ {
DBUG_RETURN(FRMTYPE_ERROR); DBUG_RETURN(FRMTYPE_ERROR);
} }
...@@ -1372,43 +1372,21 @@ int view_checksum(THD *thd, TABLE_LIST *view) ...@@ -1372,43 +1372,21 @@ int view_checksum(THD *thd, TABLE_LIST *view)
HA_ADMIN_OK); HA_ADMIN_OK);
} }
bool rename_view_files(const char *schema, const char *old_name, /*
const char *new_name, ulonglong revision) rename view
{
char old_path[FN_REFLEN], new_path[FN_REFLEN], arc_path[FN_REFLEN];
strxnmov(old_path, FN_REFLEN, mysql_data_home, "/", schema, "/",
old_name, reg_ext, NullS);
(void) unpack_filename(old_path, old_path);
strxnmov(new_path, FN_REFLEN, mysql_data_home, "/", schema, "/",
new_name, reg_ext, NullS);
(void) unpack_filename(new_path, new_path);
if (my_rename(old_path, new_path, MYF(MY_WME)))
return 1;
/* check if arc_dir exists */ Synopsis:
strxnmov(arc_path, FN_REFLEN, mysql_data_home, "/", schema, "/arc", NullS); renames a view
(void) unpack_filename(arc_path, arc_path);
if (revision && !access(arc_path, F_OK)) Parameters:
{ thd thread handler
while (revision) { new_name new name of view
my_snprintf(old_path, FN_REFLEN, "%s/%s%s-%04lu", view view
arc_path, old_name, reg_ext, (ulong)revision);
(void) unpack_filename(old_path, old_path);
my_snprintf(new_path, FN_REFLEN, "%s/%s%s-%04lu",
arc_path, new_name, reg_ext, (ulong)revision);
(void) unpack_filename(new_path, new_path);
if (my_rename(old_path, new_path, MYF(0)))
return 0;
revision--;
}
}
return 0;
}
Return values:
FALSE Ok
TRUE Error
*/
bool bool
mysql_rename_view(THD *thd, mysql_rename_view(THD *thd,
const char *new_name, const char *new_name,
...@@ -1438,7 +1416,8 @@ mysql_rename_view(THD *thd, ...@@ -1438,7 +1416,8 @@ mysql_rename_view(THD *thd,
DBUG_RETURN(1); DBUG_RETURN(1);
/* rename view and it's backups */ /* rename view and it's backups */
if (rename_view_files(view->db, view->table_name, new_name, view->revision - 1)) if (rename_in_schema_file(view->db, view->table_name, new_name,
view->revision - 1, num_view_backups))
DBUG_RETURN(1); DBUG_RETURN(1);
strxnmov(dir_buff, FN_REFLEN, mysql_data_home, "/", view->db, "/", NullS); strxnmov(dir_buff, FN_REFLEN, mysql_data_home, "/", view->db, "/", NullS);
...@@ -1454,7 +1433,8 @@ mysql_rename_view(THD *thd, ...@@ -1454,7 +1433,8 @@ mysql_rename_view(THD *thd,
if (sql_create_definition_file(&pathstr, &file, view_file_type, if (sql_create_definition_file(&pathstr, &file, view_file_type,
(gptr)view, view_parameters, num_view_backups)) { (gptr)view, view_parameters, num_view_backups)) {
/* restore renamed view in case of error */ /* restore renamed view in case of error */
rename_view_files(view->db, new_name, view->table_name, view->revision - 1); rename_in_schema_file(view->db, new_name, view->table_name,
view->revision - 1, num_view_backups);
DBUG_RETURN(1); DBUG_RETURN(1);
} }
} else } else
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment