Skip to content

Commit feed861

Browse files
authored
Merge pull request #6882 from morozov/cleanup-rename-index
Clean up Table::renameIndex() code
2 parents f29f027 + 2180588 commit feed861

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/Schema/Table.php

+15-16
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,25 @@ public function addUniqueIndex(array $columnNames, ?string $indexName = null, ar
298298
*/
299299
public function renameIndex(string $oldName, ?string $newName = null): self
300300
{
301-
$oldName = $this->normalizeIdentifier($oldName);
302-
$normalizedNewName = $this->normalizeIdentifier($newName);
303-
304-
if ($oldName === $normalizedNewName) {
305-
return $this;
306-
}
307-
308301
if (! $this->hasIndex($oldName)) {
309302
throw IndexDoesNotExist::new($oldName, $this->_name);
310303
}
311304

312-
if ($this->hasIndex($normalizedNewName)) {
313-
throw IndexAlreadyExists::new($normalizedNewName, $this->_name);
305+
$normalizedOldName = $this->normalizeIdentifier($oldName);
306+
307+
if ($newName !== null) {
308+
$normalizedNewName = $this->normalizeIdentifier($newName);
309+
310+
if ($normalizedOldName === $normalizedNewName) {
311+
return $this;
312+
}
313+
314+
if ($this->hasIndex($newName)) {
315+
throw IndexAlreadyExists::new($newName, $this->_name);
316+
}
314317
}
315318

316-
$oldIndex = $this->_indexes[$oldName];
319+
$oldIndex = $this->_indexes[$normalizedOldName];
317320

318321
if ($oldIndex->isPrimary()) {
319322
Deprecation::triggerIfCalledFromOutside(
@@ -329,7 +332,7 @@ public function renameIndex(string $oldName, ?string $newName = null): self
329332
return $this->setPrimaryKey($oldIndex->getColumns(), $newName ?? null);
330333
}
331334

332-
unset($this->_indexes[$oldName]);
335+
unset($this->_indexes[$normalizedOldName]);
333336

334337
if ($oldIndex->isUnique()) {
335338
return $this->addUniqueIndex($oldIndex->getColumns(), $newName, $oldIndex->getOptions());
@@ -944,12 +947,8 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint): s
944947
*
945948
* Trims quotes and lowercases the given identifier.
946949
*/
947-
private function normalizeIdentifier(?string $identifier): string
950+
private function normalizeIdentifier(string $identifier): string
948951
{
949-
if ($identifier === null) {
950-
return '';
951-
}
952-
953952
return $this->trimQuotes(strtolower($identifier));
954953
}
955954

tests/Schema/TableTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\DBAL\Platforms\MySQLPlatform;
99
use Doctrine\DBAL\Platforms\SQLitePlatform;
1010
use Doctrine\DBAL\Schema\Column;
11+
use Doctrine\DBAL\Schema\Exception\IndexDoesNotExist;
1112
use Doctrine\DBAL\Schema\Exception\InvalidState;
1213
use Doctrine\DBAL\Schema\Exception\PrimaryKeyAlreadyExists;
1314
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
@@ -776,6 +777,14 @@ public function testRenameIndex(): void
776777
self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));
777778
}
778779

780+
public function testRenameNonExistingIndexToTheSameName(): void
781+
{
782+
$table = new Table('test');
783+
784+
$this->expectException(IndexDoesNotExist::class);
785+
$table->renameIndex('test', 'test');
786+
}
787+
779788
public function testKeepsIndexOptionsOnRenamingRegularIndex(): void
780789
{
781790
$table = new Table('foo');

0 commit comments

Comments
 (0)