Skip to content

Commit

Permalink
ADD CONSTRAINT IF NOT EXISTS didn't work in SP
Browse files Browse the repository at this point in the history
"if not exists" must be stored in a separate read-only property
  • Loading branch information
vuvova committed Jan 17, 2023
1 parent a5eff04 commit cce76fe
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
34 changes: 34 additions & 0 deletions mysql-test/main/constraints.result
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ t1 CREATE TABLE `t1` (
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
DROP PROCEDURE sp;
DROP TABLE t1;
#
# End of 10.2 tests
#
create table t1 (a int check (a>10)) select 100 as 'a';
show create table t1;
Table Create Table
Expand All @@ -201,3 +203,35 @@ a
19
ccc
drop table t1;
create table t1 (a int, b int);
create procedure sp() alter table t1 add constraint if not exists foo check (b > 0);
call sp;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
CONSTRAINT `foo` CHECK (`b` > 0)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
call sp;
Warnings:
Note 1826 Duplicate CHECK constraint name 'foo'
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
CONSTRAINT `foo` CHECK (`b` > 0)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
call sp;
Warnings:
Note 1826 Duplicate CHECK constraint name 'foo'
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
CONSTRAINT `foo` CHECK (`b` > 0)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci
drop procedure sp;
drop table t1;
17 changes: 17 additions & 0 deletions mysql-test/main/constraints.test
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ show create table t1;
DROP PROCEDURE sp;
DROP TABLE t1;

--echo #
--echo # End of 10.2 tests
--echo #

#
# Check that we don't lose constraints as part of CREATE ... SELECT
Expand All @@ -172,3 +174,18 @@ insert into t1 values ("ccc");
insert into t1 values ("");
select * from t1;
drop table t1;

#
# add if not exists in SP
#

create table t1 (a int, b int);
create procedure sp() alter table t1 add constraint if not exists foo check (b > 0);
call sp;
show create table t1;
call sp;
show create table t1;
call sp;
show create table t1;
drop procedure sp;
drop table t1;
2 changes: 1 addition & 1 deletion sql/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,6 @@ static inline const char *vcol_type_name(enum_vcol_info_type type)
#define VCOL_AUTO_INC 16
#define VCOL_IMPOSSIBLE 32
#define VCOL_NEXTVAL 64 /* NEXTVAL is not implemented for vcols */
#define VCOL_CHECK_CONSTRAINT_IF_NOT_EXISTS 128

#define VCOL_NOT_STRICTLY_DETERMINISTIC \
(VCOL_NON_DETERMINISTIC | VCOL_TIME_FUNC | VCOL_SESSION_FUNC)
Expand Down Expand Up @@ -590,6 +589,7 @@ class Virtual_column_info: public Sql_alloc,
bool stored_in_db;
bool utf8; /* Already in utf8 */
bool automatic_name;
bool if_not_exists;
Item *expr;
Lex_ident name; /* Name of constraint */
/* see VCOL_* (VCOL_FIELD_REF, ...) */
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -4375,7 +4375,7 @@ struct LEX: public Query_tables_list
bool if_not_exists)
{
constr->name= name;
constr->flags= if_not_exists ? VCOL_CHECK_CONSTRAINT_IF_NOT_EXISTS : 0;
constr->if_not_exists= if_not_exists;
alter_info.check_constraint_list.push_back(constr);
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6928,10 +6928,8 @@ handle_if_exists_options(THD *thd, TABLE *table, Alter_info *alter_info,

while ((check=it++))
{
if (!(check->flags & VCOL_CHECK_CONSTRAINT_IF_NOT_EXISTS) &&
check->name.length)
if (!check->if_not_exists && check->name.length)
continue;
check->flags= 0;
for (c= share->field_check_constraints;
c < share->table_check_constraints ; c++)
{
Expand Down

0 comments on commit cce76fe

Please sign in to comment.