Commit fc01b099 authored by evgen@moonbone.local's avatar evgen@moonbone.local

Bug#28427: Columns were renamed instead of moving by ALTER TABLE.

To avoid unnecessary work the mysql_alter_table function takes the
list of table fields and applies all changes to it (drops/moves/renames/etc).
Then this function compares the new list and the old one. If the changes
require only .frm to be modified then the actual data isn't copied. To detect
changes all columns attributes but names are compared. When a column has been
moved and has replaced another column with the same attributes except name
the mysql_alter_table function wrongly decides that two fields has been just
renamed. As a result the data from the moved column and from all columns
after it is not copied.

Now the mysql_alter_table function forces table data copying by setting
the need_copy_table flag when it finds a moved column. The flag is set at
the stage when the modified fields are created.
parent a24a38df
...@@ -1103,3 +1103,17 @@ Field Type Null Key Default Extra ...@@ -1103,3 +1103,17 @@ Field Type Null Key Default Extra
unsigned_int_field bigint(20) unsigned NO MUL unsigned_int_field bigint(20) unsigned NO MUL
char_field char(10) YES NULL char_field char(10) YES NULL
DROP TABLE t2; DROP TABLE t2;
CREATE TABLE t1 (f1 INT, f2 INT, f3 INT);
INSERT INTO t1 VALUES (1, 2, NULL);
SELECT * FROM t1;
f1 f2 f3
1 2 NULL
ALTER TABLE t1 MODIFY COLUMN f3 INT AFTER f1;
SELECT * FROM t1;
f1 f3 f2
1 NULL 2
ALTER TABLE t1 MODIFY COLUMN f3 INT AFTER f2;
SELECT * FROM t1;
f1 f2 f3
1 2 NULL
DROP TABLE t1;
...@@ -839,3 +839,15 @@ ALTER TABLE t2 MODIFY unsigned_int_field BIGINT UNSIGNED NOT NULL; ...@@ -839,3 +839,15 @@ ALTER TABLE t2 MODIFY unsigned_int_field BIGINT UNSIGNED NOT NULL;
DESCRIBE t2; DESCRIBE t2;
DROP TABLE t2; DROP TABLE t2;
#
# Bug#28427: Columns were renamed instead of moving by ALTER TABLE.
#
CREATE TABLE t1 (f1 INT, f2 INT, f3 INT);
INSERT INTO t1 VALUES (1, 2, NULL);
SELECT * FROM t1;
ALTER TABLE t1 MODIFY COLUMN f3 INT AFTER f1;
SELECT * FROM t1;
ALTER TABLE t1 MODIFY COLUMN f3 INT AFTER f2;
SELECT * FROM t1;
DROP TABLE t1;
...@@ -5932,6 +5932,7 @@ view_err: ...@@ -5932,6 +5932,7 @@ view_err:
goto err; goto err;
} }
find_it.after(def); // Put element after this find_it.after(def); // Put element after this
need_copy_table= ALTER_TABLE_DATA_CHANGED;
} }
} }
if (alter_info->alter_list.elements) if (alter_info->alter_list.elements)
...@@ -6170,12 +6171,14 @@ view_err: ...@@ -6170,12 +6171,14 @@ view_err:
(uint*) thd->alloc(sizeof(uint) * prepared_key_list.elements))) (uint*) thd->alloc(sizeof(uint) * prepared_key_list.elements)))
goto err; goto err;
/* Check how much the tables differ. */ /* Check how much the tables differ. */
need_copy_table= compare_tables(table, &prepared_create_list, bool res= compare_tables(table, &prepared_create_list,
key_info_buffer, key_count, key_info_buffer, key_count,
create_info, alter_info, order_num, create_info, alter_info, order_num,
index_drop_buffer, &index_drop_count, index_drop_buffer, &index_drop_count,
index_add_buffer, &index_add_count, index_add_buffer, &index_add_count,
varchar); varchar);
if (!need_copy_table)
need_copy_table= res;
} }
/* /*
......
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