Commit 1a8cf15d authored by Jan Lindström's avatar Jan Lindström

MDEV-8392: Couldn't alter field with default value for make it not nullable.

Analysis; Problem is that InnoDB does not have support for generating
CURRENT_TIMESTAMP or constant default.

Fix: Add additional check if column has changed from NULL -> NOT NULL
and column default has changed. If this is is first column definition
whose SQL type is TIMESTAMP and it is defined as NOT NULL and
it has either constant default or function default we must use
"Copy" method for alter table.
parent 8af5ab40
CREATE TABLE t1 (
`i1` INT(10) UNSIGNED NOT NULL,
`d1` TIMESTAMP NULL DEFAULT NULL
) ENGINE=innodb;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i1` int(10) unsigned NOT NULL,
`d1` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO t1 (i1) VALUES (1), (2), (3), (4), (5);
select * from t1;
i1 d1
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL
set sql_mode = 'STRICT_ALL_TABLES,NO_ZERO_DATE';
ALTER TABLE t1 CHANGE `d1` `d1` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
drop table t1;
CREATE TABLE t1 (
`i1` INT(10) UNSIGNED NOT NULL,
`d1` TIMESTAMP NULL DEFAULT NULL
) ENGINE=innodb;
INSERT INTO t1 (i1) VALUES (1), (2), (3), (4), (5);
ALTER TABLE t1 CHANGE `d1` `d1` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
drop table t1;
set sql_mode = '';
--source include/have_innodb.inc
CREATE TABLE t1 (
`i1` INT(10) UNSIGNED NOT NULL,
`d1` TIMESTAMP NULL DEFAULT NULL
) ENGINE=innodb;
show create table t1;
INSERT INTO t1 (i1) VALUES (1), (2), (3), (4), (5);
select * from t1;
set sql_mode = 'STRICT_ALL_TABLES,NO_ZERO_DATE';
ALTER TABLE t1 CHANGE `d1` `d1` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL;
drop table t1;
CREATE TABLE t1 (
`i1` INT(10) UNSIGNED NOT NULL,
`d1` TIMESTAMP NULL DEFAULT NULL
) ENGINE=innodb;
INSERT INTO t1 (i1) VALUES (1), (2), (3), (4), (5);
ALTER TABLE t1 CHANGE `d1` `d1` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
drop table t1;
set sql_mode = '';
......@@ -372,6 +372,35 @@ ha_innobase::check_if_supported_inplace_alter(
}
}
/* If we have column that has changed from NULL -> NOT NULL
and column default has changed we need to do additional
check. */
if ((ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_COLUMN_NOT_NULLABLE) &&
(ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_COLUMN_DEFAULT)) {
Alter_info *alter_info = ha_alter_info->alter_info;
List_iterator<Create_field> def_it(alter_info->create_list);
Create_field *def;
while ((def=def_it++)) {
/* If this is first column definition whose SQL type
is TIMESTAMP and it is defined as NOT NULL and
it has either constant default or function default
we must use "Copy" method. */
if (is_timestamp_type(def->sql_type)) {
if ((def->flags & NOT_NULL_FLAG) != 0 && // NOT NULL
(def->def != NULL || // constant default ?
def->unireg_check != Field::NONE)) { // function default
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_NOT_NULL);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
break;
}
}
}
/* We should be able to do the operation in-place.
See if we can do it online (LOCK=NONE). */
bool online = true;
......
......@@ -372,6 +372,35 @@ ha_innobase::check_if_supported_inplace_alter(
}
}
/* If we have column that has changed from NULL -> NOT NULL
and column default has changed we need to do additional
check. */
if ((ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_COLUMN_NOT_NULLABLE) &&
(ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_COLUMN_DEFAULT)) {
Alter_info *alter_info = ha_alter_info->alter_info;
List_iterator<Create_field> def_it(alter_info->create_list);
Create_field *def;
while ((def=def_it++)) {
/* If this is first column definition whose SQL type
is TIMESTAMP and it is defined as NOT NULL and
it has either constant default or function default
we must use "Copy" method. */
if (is_timestamp_type(def->sql_type)) {
if ((def->flags & NOT_NULL_FLAG) != 0 && // NOT NULL
(def->def != NULL || // constant default ?
def->unireg_check != Field::NONE)) { // function default
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_NOT_NULL);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
break;
}
}
}
/* We should be able to do the operation in-place.
See if we can do it online (LOCK=NONE). */
bool online = true;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment