Commit 5cba1ea7 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-126 - Fix bug during table diffs when creating new primary keys, unique...

DBAL-126 - Fix bug during table diffs when creating new primary keys, unique constraints were created instead.
parent fea10ddb
......@@ -959,18 +959,33 @@ abstract class AbstractPlatform
if (count($columns) == 0) {
throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
}
if ($index->isPrimary()) {
return $this->getCreatePrimaryKeySQL($index, $table);
} else {
$type = '';
if ($index->isUnique()) {
$type = 'UNIQUE ';
}
$type = '';
if ($index->isUnique()) {
$type = 'UNIQUE ';
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
}
$query = 'CREATE ' . $type . 'INDEX ' . $name . ' ON ' . $table;
$query .= ' (' . $this->getIndexFieldDeclarationListSQL($columns) . ')';
return $query;
}
/**
* Get SQL to create an unnamed primary key constraint.
*
* @param Index $index
* @param string|Table $table
* @return string
*/
public function getCreatePrimaryKeySQL(Index $index, $table)
{
return 'ALTER TABLE ' . $table . ' ADD PRIMARY KEY (' . $this->getIndexFieldDeclarationListSQL($index->getColumns()) . ')';
}
/**
* Quotes a string so that it can be safely used as a table or column name,
......
......@@ -4,6 +4,8 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
require_once __DIR__ . '/../../TestInit.php';
......@@ -16,7 +18,7 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
public function testGenerateMixedCaseTableCreate()
{
$table = new \Doctrine\DBAL\Schema\Table("Foo");
$table = new Table("Foo");
$table->addColumn("Bar", "integer");
$sql = $this->_platform->getCreateTableSQL($table);
......@@ -144,6 +146,32 @@ class MySqlPlatformTest extends AbstractPlatformTestCase
{
return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table(id)';
}
/**
* @group DBAL-126
*/
public function testUniquePrimaryKey()
{
$keyTable = new Table("foo");
$keyTable->addColumn("bar", "integer");
$keyTable->addColumn("baz", "string");
$keyTable->setPrimaryKey(array("bar"));
$keyTable->addUniqueIndex(array("baz"));
$oldTable = new Table("foo");
$oldTable->addColumn("bar", "integer");
$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 PRIMARY KEY (bar)",
"CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
), $sql);
}
public function testModifyLimitQuery()
{
......
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