Merge pull request #1375 from webimpress/fix/DDC-3671
DDC-3671 prevent duplicate unique index
This commit is contained in:
commit
ada97d55ce
@ -21,6 +21,7 @@ namespace Doctrine\ORM\Tools;
|
||||
|
||||
use Doctrine\ORM\ORMException;
|
||||
use Doctrine\DBAL\Schema\Comparator;
|
||||
use Doctrine\DBAL\Schema\Index;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Table;
|
||||
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
|
||||
@ -274,6 +275,15 @@ class SchemaTool
|
||||
|
||||
if (isset($class->table['uniqueConstraints'])) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
@ -116,6 +116,28 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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