diff --git a/UPGRADE.md b/UPGRADE.md index a144a26949..b44a3a0c77 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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. diff --git a/src/Platforms/AbstractMySQLPlatform.php b/src/Platforms/AbstractMySQLPlatform.php index eb2aa6299b..7c9faecb48 100644 --- a/src/Platforms/AbstractMySQLPlatform.php +++ b/src/Platforms/AbstractMySQLPlatform.php @@ -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) . ')'; } diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index abd8947496..7144b56b85 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -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); } diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index ad182f8f43..b5858acf2d 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -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) . ')'; } @@ -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); } diff --git a/src/Platforms/SQLServerPlatform.php b/src/Platforms/SQLServerPlatform.php index 5c0024a0a9..3cd06f973b 100644 --- a/src/Platforms/SQLServerPlatform.php +++ b/src/Platforms/SQLServerPlatform.php @@ -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'; @@ -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); } diff --git a/src/Platforms/SQLitePlatform.php b/src/Platforms/SQLitePlatform.php index ccb7c46229..c9d4e9b66b 100644 --- a/src/Platforms/SQLitePlatform.php +++ b/src/Platforms/SQLitePlatform.php @@ -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); } @@ -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); } diff --git a/src/Schema/Index.php b/src/Schema/Index.php index 28a7469bec..591b1ac0f8 100644 --- a/src/Schema/Index.php +++ b/src/Schema/Index.php @@ -17,8 +17,8 @@ use function count; use function strtolower; -/** @extends AbstractOptionallyNamedObject */ -class Index extends AbstractOptionallyNamedObject +/** @extends AbstractNamedObject */ +class Index extends AbstractNamedObject { /** * Asset identifier instances of the column names the index is associated with. @@ -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; diff --git a/tests/Schema/IndexTest.php b/tests/Schema/IndexTest.php index 0eb13502be..1f53bd2c5f 100644 --- a/tests/Schema/IndexTest.php +++ b/tests/Schema/IndexTest.php @@ -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 { @@ -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()); } }