Fix #14732 Can't rename primary key with auto increment

Signed-off-by: William Desportes <williamdes@wdes.fr>
This commit is contained in:
William Desportes 2019-12-16 23:42:03 +01:00
parent ea8f8f3a86
commit 850c125684
No known key found for this signature in database
GPG Key ID: 90A0EF1B8251A889
2 changed files with 174 additions and 4 deletions

View File

@ -478,6 +478,7 @@ class Table
* @param string $expression expression for the virtual column
* @param string $move_to new position for column
* @param array $columns_with_index Fields having PRIMARY or UNIQUE KEY indexes
* @param string $oldColumnName Old column name
*
* @todo move into class PMA_Column
* @todo on the interface, some js to clear the default value when the
@ -499,7 +500,8 @@ class Table
$virtuality = '',
$expression = '',
$move_to = '',
$columns_with_index = null
$columns_with_index = null,
$oldColumnName = null
) {
/** @var DatabaseInterface $dbi */
$dbi = $GLOBALS['dbi'];
@ -641,8 +643,14 @@ class Table
$query .= ' AFTER ' . Util::backquote($move_to);
}
if (! $virtuality && ! empty($extra)) {
if (empty($columns_with_index) && ! in_array($name, $columns_with_index)) {
$query .= ', add PRIMARY KEY (' . Util::backquote($name) . ')';
if ($oldColumnName === null) {
if (is_array($columns_with_index) && ! in_array($name, $columns_with_index)) {
$query .= ', add PRIMARY KEY (' . Util::backquote($name) . ')';
}
} else {
if (is_array($columns_with_index) && ! in_array($oldColumnName, $columns_with_index)) {
$query .= ', add PRIMARY KEY (' . Util::backquote($name) . ')';
}
}
}
@ -847,7 +855,8 @@ class Table
$virtuality,
$expression,
$move_to,
$columns_with_index
$columns_with_index,
$oldcol
);
} // end function

View File

@ -709,6 +709,167 @@ class TableTest extends PmaTestCase
. "COMMENT 'PMA_comment' FIRST",
$query
);
$type = 'INT';
$default_type = 'NONE';
$move_to = '-first';
$query = Table::generateFieldSpec(
'ids',
'INT',
'11',
$attribute,
$collation,
$null,
$default_type,
$default_value,
'AUTO_INCREMENT',
$comment,
$virtuality,
$expression,
$move_to,
['id'],
'id'
);
$this->assertEquals(
"`ids` INT(11) PMA_attribute NULL AUTO_INCREMENT "
. "COMMENT 'PMA_comment' FIRST",
$query
);
$type = 'INT';
$default_type = 'NONE';
$move_to = '-first';
$query = Table::generateFieldSpec(
'ids',
'INT',
'11',
$attribute,
$collation,
$null,
$default_type,
$default_value,
'AUTO_INCREMENT',
$comment,
$virtuality,
$expression,
$move_to,
['othercol'],
'id'
);
// Add primary key for AUTO_INCREMENT if missing
$this->assertEquals(
"`ids` INT(11) PMA_attribute NULL AUTO_INCREMENT "
. "COMMENT 'PMA_comment' FIRST, add PRIMARY KEY (`ids`)",
$query
);
$type = 'INT';
$default_type = 'NONE';
$move_to = '-first';
$query = Table::generateFieldSpec(
'id',
'INT',
'11',
$attribute,
$collation,
$null,
$default_type,
$default_value,
'DEF',
$comment,
$virtuality,
$expression,
$move_to,
['id'],
'id'
);
// Do not add PK
$this->assertEquals(
"`id` INT(11) PMA_attribute NULL DEF "
. "COMMENT 'PMA_comment' FIRST",
$query
);
$type = 'INT';
$default_type = 'NONE';
$move_to = '-first';
$query = Table::generateFieldSpec(
'ids',
'INT',
'11',
$attribute,
$collation,
$null,
$default_type,
$default_value,
'DEF',
$comment,
$virtuality,
$expression,
$move_to,
['id'],
'id'
);
// Do not add PK
$this->assertEquals(
"`ids` INT(11) PMA_attribute NULL DEF "
. "COMMENT 'PMA_comment' FIRST",
$query
);
$type = 'INT';
$default_type = 'NONE';
$move_to = '-first';
$query = Table::generateFieldSpec(
'ids',
'INT',
'11',
$attribute,
$collation,
$null,
$default_type,
$default_value,
'DEF',
$comment,
$virtuality,
$expression,
$move_to,
['ids'],
'id'
);
// Add it beaucause it is missing
$this->assertEquals(
"`ids` INT(11) PMA_attribute NULL DEF "
. "COMMENT 'PMA_comment' FIRST, add PRIMARY KEY (`ids`)",
$query
);
$type = 'INT';
$default_type = 'NONE';
$move_to = '-first';
$query = Table::generateFieldSpec(
'ids',
'INT',
'11',
$attribute,
$collation,
$null,
$default_type,
$default_value,
'USER_DEFINED',
$comment,
'VIRTUAL',
'1',
$move_to,
['othercol'],
'id'
);
// Do not add PK since it is not a AUTO_INCREMENT
$this->assertEquals(
"`ids` INT(11) PMA_attribute AS (1) VIRTUAL NULL "
. "USER_DEFINED COMMENT 'PMA_comment' FIRST",
$query
);
}