Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MySQL when adding primary key #363

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
Expand Up @@ -572,6 +572,12 @@ public function getAlterTableSQL(TableDiff $diff)
. $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();

Expand Down Expand Up @@ -620,6 +626,15 @@ protected function getPreAlterTableIndexForeignKeySQL(TableDiff $diff)
}
}
}
foreach ($diff->changedIndexes as $changedKey => $changedIndex) {
if ($changedIndex->isPrimary() && $changedKey != 'PRIMARY') {
$index = $diff->changedIndexes[$changedKey];
$index = new Index($changedKey, $index->getColumns(), $index->isUnique(), false);
$diff->removedIndexes[$changedKey] = $index;
$diff->addedIndexes['PRIMARY'] = $diff->changedIndexes[$changedKey];
unset($diff->changedIndexes[$changedKey]);
}
}

$sql = array_merge($sql, parent::getPreAlterTableIndexForeignKeySQL($diff));

Expand Down
33 changes: 33 additions & 0 deletions tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php
Expand Up @@ -295,4 +295,37 @@ public function testBlobTypeDeclarationSQL()
$this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array('length' => 16777216)));
$this->assertEquals('LONGBLOB', $this->_platform->getBlobTypeDeclarationSQL(array()));
}

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);
}

public function testNamedPrimaryKey()
{
$diff = new TableDiff('mytable');
$diff->changedIndexes['foo_index'] = new Index('foo_index', array('foo'), true, true);

$sql = $this->_platform->getAlterTableSQL($diff);

$this->assertEquals(array(
"DROP INDEX foo_index ON mytable",
"ALTER TABLE mytable ADD PRIMARY KEY (foo)",
), $sql);
}
}