Skip to content

Commit 45549ae

Browse files
committed
Use UnqualifiedNameSet for implicit index names
1 parent 6cf0b8e commit 45549ae

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/Schema/Table.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Schema\Collections\Exception\ObjectDoesNotExist;
99
use Doctrine\DBAL\Schema\Collections\OptionallyUnqualifiedNamedObjectSet;
1010
use Doctrine\DBAL\Schema\Collections\UnqualifiedNamedObjectSet;
11+
use Doctrine\DBAL\Schema\Collections\UnqualifiedNameSet;
1112
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist;
1213
use Doctrine\DBAL\Schema\Exception\ForeignKeyDoesNotExist;
1314
use Doctrine\DBAL\Schema\Exception\IndexAlreadyExists;
@@ -32,7 +33,6 @@
3233
use Doctrine\Deprecations\Deprecation;
3334
use LogicException;
3435

35-
use function array_diff_key;
3636
use function array_keys;
3737
use function array_map;
3838
use function array_merge;
@@ -66,13 +66,9 @@ final class Table extends AbstractNamedObject
6666
private array $indexes = [];
6767

6868
/**
69-
* The keys of this array are the keys of the {@see $indexes} array that correspond to the indexes that were
70-
* implicitly created as backing for foreign key constraints. The values are not used but must be non-null for
71-
* {@link isset()} to work correctly.
72-
*
73-
* @var array<string,true>
69+
* The names of the indexes that were implicitly created as backing for foreign key constraints.
7470
*/
75-
private array $implicitIndexKeys = [];
71+
private readonly UnqualifiedNameSet $implicitIndexNames;
7672

7773
/** @var OptionallyUnqualifiedNamedObjectSet<UniqueConstraint> */
7874
private OptionallyUnqualifiedNamedObjectSet $uniqueConstraints;
@@ -133,6 +129,8 @@ public function __construct(
133129
$foreignKeyConstraints = new OptionallyUnqualifiedNamedObjectSet();
134130
$this->foreignKeyConstraints = $foreignKeyConstraints;
135131

132+
$this->implicitIndexNames = new UnqualifiedNameSet();
133+
136134
foreach ($indexes as $idx) {
137135
$this->_addIndex($idx);
138136
}
@@ -775,9 +773,11 @@ private function _addIndex(Index $index): self
775773
$indexName = $index->getObjectName();
776774
$indexKey = $this->getObjectKey($indexName);
777775

778-
$replacedImplicitIndexKeys = [];
776+
$replacedImplicitIndexNames = new UnqualifiedNameSet();
777+
778+
foreach ($this->implicitIndexNames as $implicitIndexName) {
779+
$implicitIndexKey = $this->getObjectKey($implicitIndexName);
779780

780-
foreach ($this->implicitIndexKeys as $implicitIndexKey => $_) {
781781
if (! isset($this->indexes[$implicitIndexKey])) {
782782
continue;
783783
}
@@ -786,15 +786,16 @@ private function _addIndex(Index $index): self
786786
continue;
787787
}
788788

789-
$replacedImplicitIndexKeys[$implicitIndexKey] = true;
789+
$replacedImplicitIndexNames->add($implicitIndexName);
790790
}
791791

792-
if (isset($this->indexes[$indexKey]) && ! isset($replacedImplicitIndexKeys[$indexKey])) {
793-
throw IndexAlreadyExists::new($this->name, $index->getObjectName());
792+
if (isset($this->indexes[$indexKey]) && ! $replacedImplicitIndexNames->contains($indexName)) {
793+
throw IndexAlreadyExists::new($this->name, $indexName);
794794
}
795795

796-
foreach ($replacedImplicitIndexKeys as $key => $_) {
797-
unset($this->indexes[$key], $this->implicitIndexKeys[$key]);
796+
foreach ($replacedImplicitIndexNames as $replacedImplicitIndexName) {
797+
unset($this->indexes[$this->getObjectKey($replacedImplicitIndexName)]);
798+
$this->implicitIndexNames->remove($replacedImplicitIndexName);
798799
}
799800

800801
$this->indexes[$indexKey] = $index;
@@ -831,7 +832,7 @@ private function _addUniqueConstraint(UniqueConstraint $constraint): self
831832
}
832833
}
833834

834-
$this->implicitIndexKeys[$this->getObjectKey($indexName)] = true;
835+
$this->implicitIndexNames->add($indexName);
835836

836837
return $this;
837838
}
@@ -866,7 +867,7 @@ private function _addForeignKeyConstraint(ForeignKeyConstraint $constraint): sel
866867
}
867868

868869
$this->_addIndex($indexCandidate);
869-
$this->implicitIndexKeys[$this->getObjectKey($indexName)] = true;
870+
$this->implicitIndexNames->add($indexName);
870871

871872
return $this;
872873
}
@@ -907,10 +908,20 @@ public static function editor(): TableEditor
907908
*/
908909
public function edit(): TableEditor
909910
{
911+
$explicitIndexes = [];
912+
913+
foreach ($this->indexes as $index) {
914+
if ($this->implicitIndexNames->contains($index->getObjectName())) {
915+
continue;
916+
}
917+
918+
$explicitIndexes[] = $index;
919+
}
920+
910921
$editor = self::editor()
911922
->setName($this->getObjectName())
912923
->setColumns(...$this->columns->toList())
913-
->setIndexes(...array_values(array_diff_key($this->indexes, $this->implicitIndexKeys)))
924+
->setIndexes(...$explicitIndexes)
914925
->setPrimaryKeyConstraint($this->primaryKeyConstraint)
915926
->setUniqueConstraints(...$this->uniqueConstraints->toList())
916927
->setForeignKeyConstraints(...$this->foreignKeyConstraints->toList());

0 commit comments

Comments
 (0)