Skip to content

Commit

Permalink
Fix #14732 Can't rename primary key with auto increment
Browse files Browse the repository at this point in the history
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed Dec 16, 2019
1 parent ea8f8f3 commit 850c125
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 4 deletions.
17 changes: 13 additions & 4 deletions libraries/classes/Table.php
Expand Up @@ -478,6 +478,7 @@ public function getCreateOptions()
* @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
Expand All @@ -499,7 +500,8 @@ public static function generateFieldSpec(
$virtuality = '',
$expression = '',
$move_to = '',
$columns_with_index = null
$columns_with_index = null,
$oldColumnName = null
) {
/** @var DatabaseInterface $dbi */
$dbi = $GLOBALS['dbi'];
Expand Down Expand Up @@ -641,8 +643,14 @@ public static function generateFieldSpec(
$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) . ')';
}
}
}

Expand Down Expand Up @@ -847,7 +855,8 @@ public static function generateAlter(
$virtuality,
$expression,
$move_to,
$columns_with_index
$columns_with_index,
$oldcol
);
} // end function

Expand Down
161 changes: 161 additions & 0 deletions test/classes/TableTest.php
Expand Up @@ -709,6 +709,167 @@ public function testGenerateFieldSpec()
. "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
);
}


Expand Down

0 comments on commit 850c125

Please sign in to comment.