Skip to content

Commit

Permalink
Merge pull request #2696 from drieschel/pk_add_column_mysql
Browse files Browse the repository at this point in the history
MySQL: Fix primary key alteration when adding new columns
  • Loading branch information
deeky666 authored Apr 26, 2017
2 parents 23301dc + fa2a12e commit fbed76e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ private function getPreAlterTableAlterPrimaryKeySQL(TableDiff $diff, Index $inde

// Dropping primary keys requires to unset autoincrement attribute on the particular column first.
foreach ($index->getColumns() as $columnName) {
if (! $diff->fromTable->hasColumn($columnName)) {
continue;
}

$column = $diff->fromTable->getColumn($columnName);

if ($column->getAutoincrement() === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,30 @@ public function testNamedPrimaryKey()
"ALTER TABLE mytable ADD PRIMARY KEY (foo)",
), $sql);
}

public function testAlterPrimaryKeyWithNewColumn()
{
$table = new Table("yolo");
$table->addColumn('pkc1', 'integer');
$table->addColumn('col_a', 'integer');
$table->setPrimaryKey(array('pkc1'));

$comparator = new Comparator();
$diffTable = clone $table;

$diffTable->addColumn('pkc2', 'integer');
$diffTable->dropPrimaryKey();
$diffTable->setPrimaryKey(array('pkc1', 'pkc2'));

$this->assertSame(
array(
'ALTER TABLE yolo DROP PRIMARY KEY',
'ALTER TABLE yolo ADD pkc2 INT NOT NULL',
'ALTER TABLE yolo ADD PRIMARY KEY (pkc1, pkc2)',
),
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
);
}

public function testInitializesDoctrineTypeMappings()
{
Expand Down

0 comments on commit fbed76e

Please sign in to comment.