From 7aa11b436145b9b5d1cef2fb88725d81729aab20 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 17 Jun 2014 14:11:02 +0200 Subject: [PATCH 1/2] Do not rename primary key index when renaming table When the migrator renames a table, for example for upgrade simulation, it should not rename the primary key to avoid messing up with the diff because the MySQL Doctrine code expects that index to always be called "primary". --- lib/private/db/migrator.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/private/db/migrator.php b/lib/private/db/migrator.php index 517be8399e..8dbfabe443 100644 --- a/lib/private/db/migrator.php +++ b/lib/private/db/migrator.php @@ -128,7 +128,13 @@ class Migrator { $indexes = $table->getIndexes(); $newIndexes = array(); foreach ($indexes as $index) { - $indexName = 'oc_' . uniqid(); // avoid conflicts in index names + if ($index->isPrimary()) { + // do not rename primary key + $indexName = $index->getName(); + } else { + // avoid conflicts in index names + $indexName = 'oc_' . uniqid(); + } $newIndexes[] = new Index($indexName, $index->getColumns(), $index->isUnique(), $index->isPrimary()); } From ffdc1c2fcfd3fe05bd8d5b58839d3610ece0a03e Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Tue, 17 Jun 2014 15:07:36 +0200 Subject: [PATCH 2/2] Added unit test for checkMigrate with primary key + autoinc Added unit test to make sure that checkMigrate() works when adding a primary key and autoincrement column to a table schema. --- tests/lib/db/migrator.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/lib/db/migrator.php b/tests/lib/db/migrator.php index e9b986236b..e94d550f83 100644 --- a/tests/lib/db/migrator.php +++ b/tests/lib/db/migrator.php @@ -119,4 +119,25 @@ class Migrator extends \PHPUnit_Framework_TestCase { $this->assertTrue(true); } } + + public function testAddingPrimaryKeyWithAutoIncrement() { + $startSchema = new Schema(array(), array(), $this->getSchemaConfig()); + $table = $startSchema->createTable($this->tableName); + $table->addColumn('id', 'integer'); + $table->addColumn('name', 'string'); + + $endSchema = new Schema(array(), array(), $this->getSchemaConfig()); + $table = $endSchema->createTable($this->tableName); + $table->addColumn('id', 'integer', array('autoincrement' => true)); + $table->addColumn('name', 'string'); + $table->setPrimaryKey(array('id')); + + $migrator = $this->getMigrator(); + $migrator->migrate($startSchema); + + $migrator->checkMigrate($endSchema); + $migrator->migrate($endSchema); + + $this->assertTrue(true); + } }