Merge pull request #1558 from doctrine/DDC-3922
[RFC] remove indexes overruled by primary key
This commit is contained in:
commit
5ec9a62e2b
@ -247,12 +247,14 @@ class SchemaTool
|
|||||||
}
|
}
|
||||||
|
|
||||||
$pkColumns = array();
|
$pkColumns = array();
|
||||||
|
|
||||||
foreach ($class->identifier as $identifierField) {
|
foreach ($class->identifier as $identifierField) {
|
||||||
if (isset($class->fieldMappings[$identifierField])) {
|
if (isset($class->fieldMappings[$identifierField])) {
|
||||||
$pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
|
$pkColumns[] = $this->quoteStrategy->getColumnName($identifierField, $class, $this->platform);
|
||||||
} elseif (isset($class->associationMappings[$identifierField])) {
|
} elseif (isset($class->associationMappings[$identifierField])) {
|
||||||
/* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
|
/* @var $assoc \Doctrine\ORM\Mapping\OneToOne */
|
||||||
$assoc = $class->associationMappings[$identifierField];
|
$assoc = $class->associationMappings[$identifierField];
|
||||||
|
|
||||||
foreach ($assoc['joinColumns'] as $joinColumn) {
|
foreach ($assoc['joinColumns'] as $joinColumn) {
|
||||||
$pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
$pkColumns[] = $this->quoteStrategy->getJoinColumnName($joinColumn, $class, $this->platform);
|
||||||
}
|
}
|
||||||
@ -263,6 +265,17 @@ class SchemaTool
|
|||||||
$table->setPrimaryKey($pkColumns);
|
$table->setPrimaryKey($pkColumns);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// there can be unique indexes automatically created for join column
|
||||||
|
// if join column is also primary key we should keep only primary key on this column
|
||||||
|
// so, remove indexes overruled by primary key
|
||||||
|
$primaryKey = $table->getIndex('primary');
|
||||||
|
|
||||||
|
foreach ($table->getIndexes() as $idxKey => $existingIndex) {
|
||||||
|
if ($primaryKey->overrules($existingIndex)) {
|
||||||
|
$table->dropIndex($idxKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($class->table['indexes'])) {
|
if (isset($class->table['indexes'])) {
|
||||||
foreach ($class->table['indexes'] as $indexName => $indexData) {
|
foreach ($class->table['indexes'] as $indexName => $indexData) {
|
||||||
if( ! isset($indexData['flags'])) {
|
if( ! isset($indexData['flags'])) {
|
||||||
|
@ -122,10 +122,9 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
*/
|
*/
|
||||||
public function testSchemaHasProperIndexesFromUniqueConstraintAnnotation()
|
public function testSchemaHasProperIndexesFromUniqueConstraintAnnotation()
|
||||||
{
|
{
|
||||||
$em = $this->_getTestEntityManager();
|
$em = $this->_getTestEntityManager();
|
||||||
$schemaTool = new SchemaTool($em);
|
$schemaTool = new SchemaTool($em);
|
||||||
|
$classes = [
|
||||||
$classes = [
|
|
||||||
$em->getClassMetadata(__NAMESPACE__ . '\\UniqueConstraintAnnotationModel'),
|
$em->getClassMetadata(__NAMESPACE__ . '\\UniqueConstraintAnnotationModel'),
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -138,6 +137,25 @@ class SchemaToolTest extends \Doctrine\Tests\OrmTestCase
|
|||||||
$this->assertTrue($table->hasIndex('primary'));
|
$this->assertTrue($table->hasIndex('primary'));
|
||||||
$this->assertTrue($table->hasIndex('uniq_hash'));
|
$this->assertTrue($table->hasIndex('uniq_hash'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testRemoveUniqueIndexOverruledByPrimaryKey()
|
||||||
|
{
|
||||||
|
$em = $this->_getTestEntityManager();
|
||||||
|
$schemaTool = new SchemaTool($em);
|
||||||
|
$classes = [
|
||||||
|
$em->getClassMetadata(__NAMESPACE__ . '\\FirstEntity'),
|
||||||
|
$em->getClassMetadata(__NAMESPACE__ . '\\SecondEntity')
|
||||||
|
];
|
||||||
|
|
||||||
|
$schema = $schemaTool->getSchemaFromMetadata($classes);
|
||||||
|
|
||||||
|
$this->assertTrue($schema->hasTable('first_entity'), "Table first_entity should exist.");
|
||||||
|
|
||||||
|
$indexes = $schema->getTable('first_entity')->getIndexes();
|
||||||
|
|
||||||
|
$this->assertCount(1, $indexes, "there should be only one index");
|
||||||
|
$this->assertTrue(current($indexes)->isPrimary(), "index should be primary");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -187,3 +205,45 @@ class UniqueConstraintAnnotationModel
|
|||||||
*/
|
*/
|
||||||
private $hash;
|
private $hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Entity
|
||||||
|
* @Table(name="first_entity")
|
||||||
|
*/
|
||||||
|
class FirstEntity
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(name="id")
|
||||||
|
*/
|
||||||
|
public $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OneToOne(targetEntity="SecondEntity")
|
||||||
|
* @JoinColumn(name="id", referencedColumnName="fist_entity_id")
|
||||||
|
*/
|
||||||
|
public $secondEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Column(name="name")
|
||||||
|
*/
|
||||||
|
public $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Entity
|
||||||
|
* @Table(name="second_entity")
|
||||||
|
*/
|
||||||
|
class SecondEntity
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(name="fist_entity_id")
|
||||||
|
*/
|
||||||
|
public $fist_entity_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Column(name="name")
|
||||||
|
*/
|
||||||
|
public $name;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user