Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding implicit indexes #769

Merged
merged 1 commit into from
Jan 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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