Merge branch 'hotfix/#1375-prevent-duplicate-unique-index' into 2.5
Backported #1375 to 2.5
This commit is contained in:
commit
3a058f8522
@ -21,6 +21,7 @@ namespace Doctrine\ORM\Tools;
|
|||||||
|
|
||||||
use Doctrine\ORM\ORMException;
|
use Doctrine\ORM\ORMException;
|
||||||
use Doctrine\DBAL\Schema\Comparator;
|
use Doctrine\DBAL\Schema\Comparator;
|
||||||
|
use Doctrine\DBAL\Schema\Index;
|
||||||
use Doctrine\DBAL\Schema\Schema;
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
use Doctrine\DBAL\Schema\Table;
|
use Doctrine\DBAL\Schema\Table;
|
||||||
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
|
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
|
||||||
@ -274,6 +275,15 @@ class SchemaTool
|
|||||||
|
|
||||||
if (isset($class->table['uniqueConstraints'])) {
|
if (isset($class->table['uniqueConstraints'])) {
|
||||||
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
|
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
|
||||||
|
$uniqIndex = new Index($indexName, $indexData['columns'], true, false, [], isset($indexData['options']) ? $indexData['options'] : []);
|
||||||
|
|
||||||
|
foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
|
||||||
|
if ($tableIndex->isFullfilledBy($uniqIndex)) {
|
||||||
|
$table->dropIndex($tableIndexName);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['options']) ? $indexData['options'] : array());
|
$table->addUniqueIndex($indexData['columns'], is_numeric($indexName) ? null : $indexName, isset($indexData['options']) ? $indexData['options'] : array());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,6 +116,28 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
|
|
||||||
$this->assertSame(array(), $customSchemaOptions);
|
$this->assertSame(array(), $customSchemaOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-3671
|
||||||
|
*/
|
||||||
|
public function testSchemaHasProperIndexesFromUniqueConstraintAnnotation()
|
||||||
|
{
|
||||||
|
$em = $this->_getTestEntityManager();
|
||||||
|
$schemaTool = new SchemaTool($em);
|
||||||
|
|
||||||
|
$classes = [
|
||||||
|
$em->getClassMetadata(__NAMESPACE__ . '\\UniqueConstraintAnnotationModel'),
|
||||||
|
];
|
||||||
|
|
||||||
|
$schema = $schemaTool->getSchemaFromMetadata($classes);
|
||||||
|
|
||||||
|
$this->assertTrue($schema->hasTable('unique_constraint_annotation_table'));
|
||||||
|
$table = $schema->getTable('unique_constraint_annotation_table');
|
||||||
|
|
||||||
|
$this->assertEquals(2, count($table->getIndexes()));
|
||||||
|
$this->assertTrue($table->hasIndex('primary'));
|
||||||
|
$this->assertTrue($table->hasIndex('uniq_hash'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -148,3 +170,20 @@ class GenerateSchemaEventListener
|
|||||||
$this->schemaCalled = true;
|
$this->schemaCalled = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Entity
|
||||||
|
* @Table(name="unique_constraint_annotation_table", uniqueConstraints={
|
||||||
|
* @UniqueConstraint(name="uniq_hash", columns={"hash"})
|
||||||
|
* })
|
||||||
|
*/
|
||||||
|
class UniqueConstraintAnnotationModel
|
||||||
|
{
|
||||||
|
/** @Id @Column */
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Column(name="hash", type="string", length=8, nullable=false, unique=true)
|
||||||
|
*/
|
||||||
|
private $hash;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user