Commit bfa1e77b authored by Bart Visscher's avatar Bart Visscher Committed by Benjamin Eberlei

Add primary key to 'ALTER TABLE' in MySql

This is at least needed when adding a autoincrement column
parent 01760a4a
......@@ -556,6 +556,12 @@ class MySqlPlatform extends AbstractPlatform
. $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
}
if (isset($diff->addedIndexes['primary'])) {
$keyColumns = array_unique(array_values($diff->addedIndexes['primary']->getColumns()));
$queryParts[] = 'ADD PRIMARY KEY (' . implode(', ', $keyColumns) . ')';
unset($diff->addedIndexes['primary']);
}
$sql = array();
$tableSql = array();
......
......@@ -320,4 +320,27 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
$this->_platform->getAlterTableSQL($comparator->diffTable($table, $diffTable))
);
}
/**
* @group DBAL-586
*/
public function testAddAutoIncrementPrimaryKey()
{
$keyTable = new Table("foo");
$keyTable->addColumn("id", "integer", array('autoincrement' => true));
$keyTable->addColumn("baz", "string");
$keyTable->setPrimaryKey(array("id"));
$oldTable = new Table("foo");
$oldTable->addColumn("baz", "string");
$c = new \Doctrine\DBAL\Schema\Comparator;
$diff = $c->diffTable($oldTable, $keyTable);
$sql = $this->_platform->getAlterTableSQL($diff);
$this->assertEquals(array(
"ALTER TABLE foo ADD id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)",
), $sql);
}
}
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