From fa2a12e7e817e8abd8db7dc5380c5dc176127f70 Mon Sep 17 00:00:00 2001 From: Immanuel Klinkenberg Date: Sun, 2 Apr 2017 15:53:04 +0200 Subject: [PATCH] Fix primary key alteration when adding new columns to a table and it's primary key on MySQL platform. --- lib/Doctrine/DBAL/Platforms/MySqlPlatform.php | 4 ++++ .../AbstractMySQLPlatformTestCase.php | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php index 830ac099cef..a1c5f4c039b 100644 --- a/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/MySqlPlatform.php @@ -708,6 +708,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) { diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php index 4f326a8f125..e6b77999efc 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractMySQLPlatformTestCase.php @@ -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() {