Skip to content

Commit

Permalink
SQL: VTMD for OR REPLACE [fixes #270]
Browse files Browse the repository at this point in the history
  • Loading branch information
midenok committed Sep 20, 2017
1 parent 9ba635f commit 6c9b71d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 31 deletions.
12 changes: 8 additions & 4 deletions mysql-test/suite/versioning/r/vtmd.result
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ set @start= null;
select start from tmp_vtmd for system_time all order by start limit 1 into @start;
select @start > 0 and @start < @inf;
select
start + 0 = @start as A_start,
start >= @start as A_start,
(@start:= end) and end = @inf as B_end,
name,
substr(archive_name, 1, instr(archive_name, '_')) as C_archive_name
from tmp_vtmd for system_time all;
drop table tmp_vtmd;
end~~
set versioning_alter_history= keep;
create or replace table t0 (x int) with system versioning;
create table t0 (z int) with system versioning;
show tables;
Tables_in_test
t0
set versioning_alter_history= survive;
create or replace table t0 (x int) with system versioning;
create or replace table t0 (y int) with system versioning;
show tables;
Tables_in_test
t0
Expand All @@ -75,14 +75,18 @@ A_start B_end name C_archive_name
set versioning_alter_history= keep;
drop table t0;
set versioning_alter_history= survive;
create or replace table t0 (x int) with system versioning;
create table t0 (x int) with system versioning;
ERROR HY000: VTMD error: `test.t0_vtmd` exists and not empty!
drop table t0_vtmd;
create table t0 (y int) with system versioning;
create or replace table t0 (x int) with system versioning;
alter table t0 add column (y int);
call check_vtmd('t0_vtmd');
@start > 0 and @start < @inf
1
A_start B_end name C_archive_name
1 0 t0 t0_
1 0 t0 t0_
1 1 t0 NULL
call drop_archives('t0_vtmd');
drop table t0_vtmd;
Expand Down
10 changes: 7 additions & 3 deletions mysql-test/suite/versioning/t/vtmd.test
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ begin
select start from tmp_vtmd for system_time all order by start limit 1 into @start;
select @start > 0 and @start < @inf;
select
start + 0 = @start as A_start,
start >= @start as A_start,
(@start:= end) and end = @inf as B_end,
name,
substr(archive_name, 1, instr(archive_name, '_')) as C_archive_name
Expand All @@ -58,10 +58,10 @@ delimiter ;~~

# create
set versioning_alter_history= keep;
create or replace table t0 (x int) with system versioning;
create table t0 (z int) with system versioning;
show tables;
set versioning_alter_history= survive;
create or replace table t0 (x int) with system versioning;
create or replace table t0 (y int) with system versioning;
show tables;
show create table t0_vtmd;
call check_vtmd('t0_vtmd');
Expand All @@ -70,6 +70,10 @@ set versioning_alter_history= keep;
drop table t0;
set versioning_alter_history= survive;
--error ER_VERS_VTMD_ERROR
create table t0 (x int) with system versioning;

drop table t0_vtmd;
create table t0 (y int) with system versioning;
create or replace table t0 (x int) with system versioning;

# alter
Expand Down
2 changes: 1 addition & 1 deletion sql/mysqld.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ enum vers_hide_enum

enum vers_alter_history_enum
{
VERS_ALTER_HISTORY_KEEP,
VERS_ALTER_HISTORY_KEEP= 0,
VERS_ALTER_HISTORY_SURVIVE,
VERS_ALTER_HISTORY_DROP
};
Expand Down
52 changes: 32 additions & 20 deletions sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2438,6 +2438,7 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
else
{
char *end;
int frm_delete_error= 0;
/*
It could happen that table's share in the table definition cache
is the only thing that keeps the engine plugin loaded
Expand Down Expand Up @@ -2476,7 +2477,8 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
// Remove extension for delete
*(end= path + path_length - reg_ext_length)= '\0';

if (thd->lex->sql_command == SQLCOM_DROP_TABLE &&
if ((thd->lex->sql_command == SQLCOM_DROP_TABLE ||
thd->lex->sql_command == SQLCOM_CREATE_TABLE) &&
thd->variables.vers_alter_history == VERS_ALTER_HISTORY_SURVIVE &&
table_type && table_type != view_pseudo_hton)
{
Expand All @@ -2493,29 +2495,33 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
drop_table:
error= ha_delete_table(thd, table_type, path, db, table->table_name,
!dont_log_query);
if (!error)
{
/* Delete the table definition file */
strmov(end,reg_ext);
if (table_type && table_type != view_pseudo_hton &&
table_type->discover_table)
{
/*
Table type is using discovery and may not need a .frm file.
Delete it silently if it exists
*/
(void) mysql_file_delete(key_file_frm, path, MYF(0));
}
else if (mysql_file_delete(key_file_frm, path,
MYF(MY_WME)))
{
frm_delete_error= my_errno;
DBUG_ASSERT(frm_delete_error);
}
}
}

if (!error)
{
int frm_delete_error, trigger_drop_error= 0;
/* Delete the table definition file */
strmov(end,reg_ext);
if (table_type && table_type != view_pseudo_hton &&
table_type->discover_table)
{
/*
Table type is using discovery and may not need a .frm file.
Delete it silently if it exists
*/
(void) mysql_file_delete(key_file_frm, path, MYF(0));
frm_delete_error= 0;
}
else
frm_delete_error= mysql_file_delete(key_file_frm, path,
MYF(MY_WME));
if (frm_delete_error)
frm_delete_error= my_errno;
else
int trigger_drop_error= 0;

if (!frm_delete_error)
{
non_tmp_table_deleted= TRUE;
trigger_drop_error=
Expand All @@ -2534,7 +2540,10 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,

if (!error && vtmd.exists)
{
enum_sql_command sql_command= thd->lex->sql_command;
thd->lex->sql_command= SQLCOM_DROP_TABLE;
error= vtmd.update(thd);
thd->lex->sql_command= sql_command;
if (error)
mysql_rename_table(table_type, table->db, vtmd.archive_name(),
table->db, table->table_name, NO_FK_CHECKS);
Expand Down Expand Up @@ -5081,6 +5090,9 @@ bool mysql_create_table(THD *thd, TABLE_LIST *create_table,
VTMD_table vtmd(*create_table);
if (vtmd.update(thd))
{
thd->variables.vers_alter_history = VERS_ALTER_HISTORY_KEEP;
mysql_rm_table_no_locks(thd, create_table, 0, 0, 0, 0, 1, 1);
thd->variables.vers_alter_history = VERS_ALTER_HISTORY_SURVIVE;
result= 1;
goto err;
}
Expand Down
7 changes: 4 additions & 3 deletions sql/vtmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,9 @@ VTMD_table::update(THD *thd, const char* archive_name)
{
err:
vtmd->file->print_error(error, MYF(0));
goto quit;
}

result= false;
else
result= local_da.is_error();
}

quit:
Expand Down Expand Up @@ -436,6 +435,8 @@ VTMD_rename::try_rename(THD *thd, LString new_db, LString new_alias, const char
if (lock_table_names(thd, &vtmd_tl, 0, thd->variables.lock_wait_timeout, 0))
return true;
tdc_remove_table(thd, TDC_RT_REMOVE_ALL, about.db, vtmd_name, false);
if (local_da.is_error()) // just safety check
return true;
bool rc= mysql_rename_table(hton,
about.db, vtmd_name,
new_db, vtmd_new_name,
Expand Down

0 comments on commit 6c9b71d

Please sign in to comment.