Skip to content

Commit

Permalink
Merge branch 'hotfix/#769-allow-implicit-indexes-overrides' into 2.5
Browse files Browse the repository at this point in the history
Backport #769 into 2.5
  • Loading branch information
Ocramius committed Jan 9, 2015
2 parents f7acb85 + cd20345 commit 4067c72
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/Doctrine/DBAL/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class Table extends AbstractAsset
*/
protected $_columns = array();

/**
* @var Index[]
*/
private $implicitIndexes = array();

/**
* @var Index[]
*/
Expand Down Expand Up @@ -491,11 +496,24 @@ protected function _addIndex(Index $indexCandidate)
{
$indexName = $indexCandidate->getName();
$indexName = $this->normalizeIdentifier($indexName);
$replacedImplicitIndexes = array();

if (isset($this->_indexes[$indexName]) || ($this->_primaryKeyName != false && $indexCandidate->isPrimary())) {
foreach ($this->implicitIndexes as $name => $implicitIndex) {
if ($implicitIndex->isFullfilledBy($indexCandidate) && isset($this->_indexes[$name])) {
$replacedImplicitIndexes[] = $name;
}
}

if ((isset($this->_indexes[$indexName]) && ! in_array($indexName, $replacedImplicitIndexes, true)) ||
($this->_primaryKeyName != false && $indexCandidate->isPrimary())
) {
throw SchemaException::indexAlreadyExists($indexName, $this->_name);
}

foreach ($replacedImplicitIndexes as $name) {
unset($this->_indexes[$name], $this->implicitIndexes[$name]);
}

if ($indexCandidate->isPrimary()) {
$this->_primaryKeyName = $indexName;
}
Expand Down Expand Up @@ -541,7 +559,8 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
}
}

$this->addIndex($constraint->getColumns());
$this->_addIndex($indexCandidate);
$this->implicitIndexes[$this->normalizeIdentifier($indexName)] = $indexCandidate;
}

/**
Expand Down
72 changes: 72 additions & 0 deletions tests/Doctrine/Tests/DBAL/Schema/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,78 @@ public function testPrimaryKeyOverrulingUniqueIndexDoesNotDropUniqueIndex()
$this->assertTrue($table->hasIndex('idx_unique'));
}

public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConstraintIndex()
{
$foreignTable = new Table('foreign');
$foreignTable->addColumn('id', 'integer');

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

$this->assertCount(1, $localTable->getIndexes());

$localTable->addIndex(array('id'), 'explicit_idx');

$this->assertCount(1, $localTable->getIndexes());
$this->assertTrue($localTable->hasIndex('explicit_idx'));
}

public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstraintIndex()
{
$foreignTable = new Table('foreign');
$foreignTable->addColumn('id', 'integer');

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

$this->assertCount(1, $localTable->getIndexes());

$localTable->addUniqueIndex(array('id'), 'explicit_idx');

$this->assertCount(1, $localTable->getIndexes());
$this->assertTrue($localTable->hasIndex('explicit_idx'));
}

public function testAddingFulfillingPrimaryKeyOverridesImplicitForeignKeyConstraintIndex()
{
$foreignTable = new Table('foreign');
$foreignTable->addColumn('id', 'integer');

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

$this->assertCount(1, $localTable->getIndexes());

$localTable->setPrimaryKey(array('id'), 'explicit_idx');

$this->assertCount(1, $localTable->getIndexes());
$this->assertTrue($localTable->hasIndex('explicit_idx'));
}

public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyConstraintIndexWithSameNameDoesNotThrowException()
{
$foreignTable = new Table('foreign');
$foreignTable->addColumn('id', 'integer');

$localTable = new Table('local');
$localTable->addColumn('id', 'integer');
$localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

$this->assertCount(1, $localTable->getIndexes());
$this->assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));

$implicitIndex = $localTable->getIndex('IDX_8BD688E8BF396750');

$localTable->addIndex(array('id'), 'IDX_8BD688E8BF396750');

$this->assertCount(1, $localTable->getIndexes());
$this->assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));
$this->assertNotSame($implicitIndex, $localTable->getIndex('IDX_8BD688E8BF396750'));
}

/**
* @group DBAL-64
*/
Expand Down

0 comments on commit 4067c72

Please sign in to comment.