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

Deprecate renaming index to an auto-generated name #6879

Closed
Closed
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
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ awareness about deprecated code.

# Upgrade to 4.3

## Deprecated renaming an index to an auto-generated name

Renaming an index to an auto-generated name by passing NULL as the `$newName` argument to `Table::renameIndex()` is
deprecated. Pass the new index name instead.

## Deprecated features related to primary key constraints

1. The `AbstractPlatform::getCreatePrimaryKeySQL()` method has been deprecated. Use the schema manager to create and
Expand Down
22 changes: 16 additions & 6 deletions src/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use function in_array;
use function preg_match;
use function sprintf;
use function strlen;
use function strtolower;

/**
Expand Down Expand Up @@ -298,22 +299,31 @@ public function addUniqueIndex(array $columnNames, ?string $indexName = null, ar
*/
public function renameIndex(string $oldName, ?string $newName = null): self
{
$oldName = $this->normalizeIdentifier($oldName);
if ($newName === null || strlen($newName) === 0) {
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/6879',
'Passing NULL as the $newName argument to %s() is deprecated. Pass the new index name instead',
__METHOD__,
);
}

$normalizedOldName = $this->normalizeIdentifier($oldName);
$normalizedNewName = $this->normalizeIdentifier($newName);

if ($oldName === $normalizedNewName) {
if ($normalizedOldName === $normalizedNewName) {
return $this;
}

if (! $this->hasIndex($oldName)) {
throw IndexDoesNotExist::new($oldName, $this->_name);
if (! $this->hasIndex($normalizedOldName)) {
throw IndexDoesNotExist::new($normalizedOldName, $this->_name);
}

if ($this->hasIndex($normalizedNewName)) {
throw IndexAlreadyExists::new($normalizedNewName, $this->_name);
}

$oldIndex = $this->_indexes[$oldName];
$oldIndex = $this->_indexes[$normalizedOldName];

if ($oldIndex->isPrimary()) {
Deprecation::triggerIfCalledFromOutside(
Expand All @@ -329,7 +339,7 @@ public function renameIndex(string $oldName, ?string $newName = null): self
return $this->setPrimaryKey($oldIndex->getColumns(), $newName ?? null);
}

unset($this->_indexes[$oldName]);
unset($this->_indexes[$normalizedOldName]);

if ($oldIndex->isUnique()) {
return $this->addUniqueIndex($oldIndex->getColumns(), $newName, $oldIndex->getOptions());
Expand Down
2 changes: 2 additions & 0 deletions tests/Schema/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ public function testRenameIndex(): void
);
self::assertEquals(new Index('uniq_new', ['bar', 'baz'], true), $table->getIndex('uniq_new'));

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6879');

// Rename to auto-generated name.
self::assertSame($table, $table->renameIndex('pk_new', null));
self::assertSame($table, $table->renameIndex('idx_new', null));
Expand Down