@@ -6927,6 +6927,29 @@ static bool add_field_to_drop_list(THD *thd, Alter_info *alter_info,
6927
6927
return !ad || alter_info->drop_list .push_back (ad, thd->mem_root );
6928
6928
}
6929
6929
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
+
6930
6953
bool Vers_parse_info::check_and_fix_alter (THD *thd, Alter_info *alter_info,
6931
6954
HA_CREATE_INFO *create_info,
6932
6955
TABLE *table)
@@ -6964,6 +6987,42 @@ bool Vers_parse_info::check_and_fix_alter(THD *thd, Alter_info *alter_info,
6964
6987
add_field_to_drop_list (thd, alter_info, share->vers_end_field ()))
6965
6988
return true ;
6966
6989
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
+
6967
7026
return false ;
6968
7027
}
6969
7028
0 commit comments