Skip to content

Commit

Permalink
Merge pull request #6671 from morozov/deprecate-empty-index-name
Browse files Browse the repository at this point in the history
Deprecate empty index name
  • Loading branch information
morozov authored Dec 26, 2024
2 parents beadca8 + d2b09cf commit 85765ed
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 29 deletions.
3 changes: 2 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ The `Sequence::isAutoIncrementsFor()` method has been deprecated.

## Deprecated using invalid database object names

Using the following objects with an empty name is deprecated: `Column`, `View`, `Sequence`, `Identifier`.
Using the following objects with an empty name is deprecated: `Table`, `Column`, `Index`, `View`, `Sequence`,
`Identifier`.

Using the following objects with a qualified name is deprecated: `Column`, `ForeignKeyConstraint`, `Index`, `Schema`,
`UniqueConstraint`. If the object name contains a dot, the name should be quoted.
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,21 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
{
$queryFields = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
if (! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $definition) {
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
}
}

// add all indexes
if (isset($options['indexes']) && ! empty($options['indexes'])) {
if (! empty($options['indexes'])) {
foreach ($options['indexes'] as $definition) {
$queryFields .= ', ' . $this->getIndexDeclarationSQL($definition);
}
}

// attach all primary keys
if (isset($options['primary']) && ! empty($options['primary'])) {
if (! empty($options['primary'])) {
$keyColumns = array_unique(array_values($options['primary']));
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -987,17 +987,17 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
{
$columnListSql = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
if (! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $definition) {
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
}
}

if (isset($options['primary']) && ! empty($options['primary'])) {
if (! empty($options['primary'])) {
$columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')';
}

if (isset($options['indexes']) && ! empty($options['indexes'])) {
if (! empty($options['indexes'])) {
foreach ($options['indexes'] as $definition) {
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
{
$queryFields = $this->getColumnDeclarationListSQL($columns);

if (isset($options['primary']) && ! empty($options['primary'])) {
if (! empty($options['primary'])) {
$keyColumns = array_unique(array_values($options['primary']));
$queryFields .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}
Expand All @@ -396,7 +396,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio

$sql = [$query];

if (isset($options['indexes']) && ! empty($options['indexes'])) {
if (! empty($options['indexes'])) {
foreach ($options['indexes'] as $index) {
$sql[] = $this->getCreateIndexSQL($index, $name);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio

$columnListSql = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
if (! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $definition) {
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
}
}

if (isset($options['primary']) && ! empty($options['primary'])) {
if (! empty($options['primary'])) {
$flags = '';
if (isset($options['primary_index']) && $options['primary_index']->hasFlag('nonclustered')) {
$flags = ' NONCLUSTERED';
Expand All @@ -235,7 +235,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio

$sql = [$query];

if (isset($options['indexes']) && ! empty($options['indexes'])) {
if (! empty($options['indexes'])) {
foreach ($options['indexes'] as $index) {
$sql[] = $this->getCreateIndexSQL($index, $name);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
{
$queryFields = $this->getColumnDeclarationListSQL($columns);

if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) {
if (! empty($options['uniqueConstraints'])) {
foreach ($options['uniqueConstraints'] as $definition) {
$queryFields .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition);
}
Expand All @@ -300,13 +300,13 @@ protected function _getCreateTableSQL(string $name, array $columns, array $optio
return $query;
}

if (isset($options['indexes']) && ! empty($options['indexes'])) {
if (! empty($options['indexes'])) {
foreach ($options['indexes'] as $indexDef) {
$query[] = $this->getCreateIndexSQL($indexDef, $name);
}
}

if (isset($options['unique']) && ! empty($options['unique'])) {
if (! empty($options['unique'])) {
foreach ($options['unique'] as $indexDef) {
$query[] = $this->getCreateIndexSQL($indexDef, $name);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Schema/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use function count;
use function strtolower;

/** @extends AbstractOptionallyNamedObject<UnqualifiedName> */
class Index extends AbstractOptionallyNamedObject
/** @extends AbstractNamedObject<UnqualifiedName> */
class Index extends AbstractNamedObject
{
/**
* Asset identifier instances of the column names the index is associated with.
Expand Down Expand Up @@ -51,7 +51,7 @@ public function __construct(
array $flags = [],
private readonly array $options = [],
) {
parent::__construct($name);
parent::__construct($name ?? '');

$this->_isUnique = $isUnique || $isPrimary;
$this->_isPrimary = $isPrimary;
Expand Down
27 changes: 16 additions & 11 deletions tests/Schema/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

namespace Doctrine\DBAL\Tests\Schema;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Name\Identifier;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class IndexTest extends TestCase
{
use VerifyDeprecations;

/** @param mixed[] $options */
private function createIndex(bool $unique = false, bool $primary = false, array $options = []): Index
{
Expand Down Expand Up @@ -174,21 +176,24 @@ public function testOptions(): void
self::assertSame(['where' => 'name IS NULL'], $idx2->getOptions());
}

/** @throws Exception */
public function testGetNonNullObjectName(): void
public function testEmptyName(): void
{
$index = new Index('idx_user_id', ['user_id']);
$name = $index->getObjectName();
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6646');

self::assertNotNull($name);
self::assertEquals(Identifier::unquoted('idx_user_id'), $name->getIdentifier());
new Index(null, ['user_id']);
}

/** @throws Exception */
public function testGetNullObjectName(): void
public function testQualifiedName(): void
{
$index = new Index(null, ['user_id']);
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/dbal/pull/6592');

new Index('auth.idx_user_id', ['user_id']);
}

public function testGetObjectName(): void
{
$index = new Index('idx_user_id', ['user_id']);

self::assertNull($index->getObjectName());
self::assertEquals(Identifier::unquoted('idx_user_id'), $index->getObjectName()->getIdentifier());
}
}

0 comments on commit 85765ed

Please sign in to comment.