Skip to content

Commit 6c9b71d

Browse files
committed
SQL: VTMD for OR REPLACE [fixes #270]
1 parent 9ba635f commit 6c9b71d

File tree

5 files changed

+52
-31
lines changed

5 files changed

+52
-31
lines changed

mysql-test/suite/versioning/r/vtmd.result

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ set @start= null;
3737
select start from tmp_vtmd for system_time all order by start limit 1 into @start;
3838
select @start > 0 and @start < @inf;
3939
select
40-
start + 0 = @start as A_start,
40+
start >= @start as A_start,
4141
(@start:= end) and end = @inf as B_end,
4242
name,
4343
substr(archive_name, 1, instr(archive_name, '_')) as C_archive_name
4444
from tmp_vtmd for system_time all;
4545
drop table tmp_vtmd;
4646
end~~
4747
set versioning_alter_history= keep;
48-
create or replace table t0 (x int) with system versioning;
48+
create table t0 (z int) with system versioning;
4949
show tables;
5050
Tables_in_test
5151
t0
5252
set versioning_alter_history= survive;
53-
create or replace table t0 (x int) with system versioning;
53+
create or replace table t0 (y int) with system versioning;
5454
show tables;
5555
Tables_in_test
5656
t0
@@ -75,14 +75,18 @@ A_start B_end name C_archive_name
7575
set versioning_alter_history= keep;
7676
drop table t0;
7777
set versioning_alter_history= survive;
78-
create or replace table t0 (x int) with system versioning;
78+
create table t0 (x int) with system versioning;
7979
ERROR HY000: VTMD error: `test.t0_vtmd` exists and not empty!
80+
drop table t0_vtmd;
81+
create table t0 (y int) with system versioning;
82+
create or replace table t0 (x int) with system versioning;
8083
alter table t0 add column (y int);
8184
call check_vtmd('t0_vtmd');
8285
@start > 0 and @start < @inf
8386
1
8487
A_start B_end name C_archive_name
8588
1 0 t0 t0_
89+
1 0 t0 t0_
8690
1 1 t0 NULL
8791
call drop_archives('t0_vtmd');
8892
drop table t0_vtmd;

mysql-test/suite/versioning/t/vtmd.test

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ begin
4646
select start from tmp_vtmd for system_time all order by start limit 1 into @start;
4747
select @start > 0 and @start < @inf;
4848
select
49-
start + 0 = @start as A_start,
49+
start >= @start as A_start,
5050
(@start:= end) and end = @inf as B_end,
5151
name,
5252
substr(archive_name, 1, instr(archive_name, '_')) as C_archive_name
@@ -58,10 +58,10 @@ delimiter ;~~
5858

5959
# create
6060
set versioning_alter_history= keep;
61-
create or replace table t0 (x int) with system versioning;
61+
create table t0 (z int) with system versioning;
6262
show tables;
6363
set versioning_alter_history= survive;
64-
create or replace table t0 (x int) with system versioning;
64+
create or replace table t0 (y int) with system versioning;
6565
show tables;
6666
show create table t0_vtmd;
6767
call check_vtmd('t0_vtmd');
@@ -70,6 +70,10 @@ set versioning_alter_history= keep;
7070
drop table t0;
7171
set versioning_alter_history= survive;
7272
--error ER_VERS_VTMD_ERROR
73+
create table t0 (x int) with system versioning;
74+
75+
drop table t0_vtmd;
76+
create table t0 (y int) with system versioning;
7377
create or replace table t0 (x int) with system versioning;
7478

7579
# alter

sql/mysqld.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ enum vers_hide_enum
211211

212212
enum vers_alter_history_enum
213213
{
214-
VERS_ALTER_HISTORY_KEEP,
214+
VERS_ALTER_HISTORY_KEEP= 0,
215215
VERS_ALTER_HISTORY_SURVIVE,
216216
VERS_ALTER_HISTORY_DROP
217217
};

sql/sql_table.cc

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,7 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
24382438
else
24392439
{
24402440
char *end;
2441+
int frm_delete_error= 0;
24412442
/*
24422443
It could happen that table's share in the table definition cache
24432444
is the only thing that keeps the engine plugin loaded
@@ -2476,7 +2477,8 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
24762477
// Remove extension for delete
24772478
*(end= path + path_length - reg_ext_length)= '\0';
24782479

2479-
if (thd->lex->sql_command == SQLCOM_DROP_TABLE &&
2480+
if ((thd->lex->sql_command == SQLCOM_DROP_TABLE ||
2481+
thd->lex->sql_command == SQLCOM_CREATE_TABLE) &&
24802482
thd->variables.vers_alter_history == VERS_ALTER_HISTORY_SURVIVE &&
24812483
table_type && table_type != view_pseudo_hton)
24822484
{
@@ -2493,29 +2495,33 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
24932495
drop_table:
24942496
error= ha_delete_table(thd, table_type, path, db, table->table_name,
24952497
!dont_log_query);
2498+
if (!error)
2499+
{
2500+
/* Delete the table definition file */
2501+
strmov(end,reg_ext);
2502+
if (table_type && table_type != view_pseudo_hton &&
2503+
table_type->discover_table)
2504+
{
2505+
/*
2506+
Table type is using discovery and may not need a .frm file.
2507+
Delete it silently if it exists
2508+
*/
2509+
(void) mysql_file_delete(key_file_frm, path, MYF(0));
2510+
}
2511+
else if (mysql_file_delete(key_file_frm, path,
2512+
MYF(MY_WME)))
2513+
{
2514+
frm_delete_error= my_errno;
2515+
DBUG_ASSERT(frm_delete_error);
2516+
}
2517+
}
24962518
}
24972519

24982520
if (!error)
24992521
{
2500-
int frm_delete_error, trigger_drop_error= 0;
2501-
/* Delete the table definition file */
2502-
strmov(end,reg_ext);
2503-
if (table_type && table_type != view_pseudo_hton &&
2504-
table_type->discover_table)
2505-
{
2506-
/*
2507-
Table type is using discovery and may not need a .frm file.
2508-
Delete it silently if it exists
2509-
*/
2510-
(void) mysql_file_delete(key_file_frm, path, MYF(0));
2511-
frm_delete_error= 0;
2512-
}
2513-
else
2514-
frm_delete_error= mysql_file_delete(key_file_frm, path,
2515-
MYF(MY_WME));
2516-
if (frm_delete_error)
2517-
frm_delete_error= my_errno;
2518-
else
2522+
int trigger_drop_error= 0;
2523+
2524+
if (!frm_delete_error)
25192525
{
25202526
non_tmp_table_deleted= TRUE;
25212527
trigger_drop_error=
@@ -2534,7 +2540,10 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists,
25342540

25352541
if (!error && vtmd.exists)
25362542
{
2543+
enum_sql_command sql_command= thd->lex->sql_command;
2544+
thd->lex->sql_command= SQLCOM_DROP_TABLE;
25372545
error= vtmd.update(thd);
2546+
thd->lex->sql_command= sql_command;
25382547
if (error)
25392548
mysql_rename_table(table_type, table->db, vtmd.archive_name(),
25402549
table->db, table->table_name, NO_FK_CHECKS);
@@ -5081,6 +5090,9 @@ bool mysql_create_table(THD *thd, TABLE_LIST *create_table,
50815090
VTMD_table vtmd(*create_table);
50825091
if (vtmd.update(thd))
50835092
{
5093+
thd->variables.vers_alter_history = VERS_ALTER_HISTORY_KEEP;
5094+
mysql_rm_table_no_locks(thd, create_table, 0, 0, 0, 0, 1, 1);
5095+
thd->variables.vers_alter_history = VERS_ALTER_HISTORY_SURVIVE;
50845096
result= 1;
50855097
goto err;
50865098
}

sql/vtmd.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,9 @@ VTMD_table::update(THD *thd, const char* archive_name)
237237
{
238238
err:
239239
vtmd->file->print_error(error, MYF(0));
240-
goto quit;
241240
}
242-
243-
result= false;
241+
else
242+
result= local_da.is_error();
244243
}
245244

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

0 commit comments

Comments
 (0)