Skip to content

Commit 62470fc

Browse files
kevgsmidenok
authored andcommitted
SQL: recreate PRIMARY KEY on DROP SYSTEM VERSIONING [#348]
1 parent 0c571f8 commit 62470fc

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,23 @@ alter table non_empty
320320
change column sys_trx_end sys_trx_end bigint(20) unsigned generated always as row end;
321321
drop table non_empty;
322322

323+
create or replace table t (a int primary key) with system versioning;
324+
insert into t values (1);
325+
update t set a=2;
326+
alter table t drop primary key, add primary key (a), drop system versioning;
327+
select * from t;
328+
--replace_result InnoDB INNODB_OR_MYISAM MyISAM INNODB_OR_MYISAM
329+
show create table t;
330+
331+
create or replace table t (a int primary key) with system versioning;
332+
insert into t values (1);
333+
update t set a=2;
334+
alter table t drop system versioning;
335+
select * from t;
336+
--replace_result InnoDB INNODB_OR_MYISAM MyISAM INNODB_OR_MYISAM
337+
show create table t;
338+
339+
323340
call verify_vtq;
324341
}
325342

sql/handler.cc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6927,6 +6927,29 @@ static bool add_field_to_drop_list(THD *thd, Alter_info *alter_info,
69276927
return !ad || alter_info->drop_list.push_back(ad, thd->mem_root);
69286928
}
69296929

6930+
static bool is_dropping_primary_key(Alter_info *alter_info)
6931+
{
6932+
List_iterator_fast<Alter_drop> it(alter_info->drop_list);
6933+
while (Alter_drop *ad= it++)
6934+
{
6935+
if (ad->type == Alter_drop::KEY &&
6936+
!my_strcasecmp(system_charset_info, ad->name, primary_key_name))
6937+
return true;
6938+
}
6939+
return false;
6940+
}
6941+
6942+
static bool is_adding_primary_key(Alter_info *alter_info)
6943+
{
6944+
List_iterator_fast<Key> it(alter_info->key_list);
6945+
while (Key *key= it++)
6946+
{
6947+
if (key->type == Key::PRIMARY)
6948+
return true;
6949+
}
6950+
return false;
6951+
}
6952+
69306953
bool Vers_parse_info::check_and_fix_alter(THD *thd, Alter_info *alter_info,
69316954
HA_CREATE_INFO *create_info,
69326955
TABLE *table)
@@ -6964,6 +6987,42 @@ bool Vers_parse_info::check_and_fix_alter(THD *thd, Alter_info *alter_info,
69646987
add_field_to_drop_list(thd, alter_info, share->vers_end_field()))
69656988
return true;
69666989

6990+
if (share->primary_key != MAX_KEY && !is_adding_primary_key(alter_info) &&
6991+
!is_dropping_primary_key(alter_info))
6992+
{
6993+
alter_info->flags|= Alter_info::ALTER_DROP_INDEX;
6994+
Alter_drop *ad= new (thd->mem_root)
6995+
Alter_drop(Alter_drop::KEY, primary_key_name, false);
6996+
if (!ad || alter_info->drop_list.push_back(ad, thd->mem_root))
6997+
return true;
6998+
6999+
alter_info->flags|= Alter_info::ALTER_ADD_INDEX;
7000+
LEX_CSTRING key_name= {NULL, 0};
7001+
DDL_options_st options;
7002+
options.init();
7003+
Key *pk= new (thd->mem_root)
7004+
Key(Key::PRIMARY, &key_name, HA_KEY_ALG_UNDEF, false, options);
7005+
if (!pk)
7006+
return true;
7007+
7008+
st_key &key= table->key_info[share->primary_key];
7009+
for (st_key_part_info *it= key.key_part,
7010+
*end= it + key.user_defined_key_parts;
7011+
it != end; ++it)
7012+
{
7013+
if (it->field->vers_sys_field())
7014+
continue;
7015+
7016+
Key_part_spec *key_part_spec= new (thd->mem_root)
7017+
Key_part_spec(&it->field->field_name, it->length);
7018+
if (!key_part_spec ||
7019+
pk->columns.push_back(key_part_spec, thd->mem_root))
7020+
return true;
7021+
}
7022+
7023+
alter_info->key_list.push_back(pk);
7024+
}
7025+
69677026
return false;
69687027
}
69697028

0 commit comments

Comments
 (0)