Skip to content

Commit

Permalink
Use the same unique index name
Browse files Browse the repository at this point in the history
  • Loading branch information
deasraze committed Feb 19, 2024
1 parent d5ba106 commit 54a6dc9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Tools/SchemaTool.php
Expand Up @@ -337,7 +337,8 @@ public function getSchemaFromMetadata(array $classes): Schema

if (isset($class->table['uniqueConstraints'])) {
foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) {
$uniqIndex = new Index($indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []);
$uniqIndexName = is_numeric($indexName) ? null : $indexName;
$uniqIndex = new Index($uniqIndexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []);

Check failure on line 341 in src/Tools/SchemaTool.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (3.7)

PossiblyNullArgument

src/Tools/SchemaTool.php:341:48: PossiblyNullArgument: Argument 1 of Doctrine\DBAL\Schema\Index::__construct cannot be null, possibly null value provided (see https://psalm.dev/078)

foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
if ($tableIndex->isFulfilledBy($uniqIndex)) {
Expand All @@ -346,7 +347,7 @@ public function getSchemaFromMetadata(array $classes): Schema
}
}

$table->addUniqueIndex($uniqIndex->getColumns(), is_numeric($indexName) ? null : $indexName, $indexData['options'] ?? []);
$table->addUniqueIndex($uniqIndex->getColumns(), $uniqIndexName, $indexData['options'] ?? []);
}
}

Expand Down
84 changes: 84 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/DDC11246Test.php
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\UniqueConstraint;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmFunctionalTestCase;
use Generator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;

#[Group('DDC-11246')]
class DDC11246Test extends OrmFunctionalTestCase
{
/** @param class-string $className */
#[DataProvider('classesWithoutUniqueConstraintName')]
public function testUniqueConstraintWithoutName(string $className): void
{
$em = $this->getTestEntityManager();
$schemaTool = new SchemaTool($em);

$classes = [
$em->getClassMetadata($className),
];

$schema = $schemaTool->getSchemaFromMetadata($classes);

self::assertTrue($schema->hasTable('unique-constraint-without-name'));
$table = $schema->getTable('unique-constraint-without-name');

self::assertCount(2, $table->getIndexes());
self::assertTrue($table->columnsAreIndexed(['name', 'phone']));
}

public static function classesWithoutUniqueConstraintName(): Generator
{
yield 'Entity class that passes fields for the UniqueConstraint attribute.' => [DDC11246FieldsEntity::class];
yield 'Entity class that passes columns for the UniqueConstraint attribute.' => [DDC11246ColumnsEntity::class];
}
}

#[Entity]
#[Table('unique-constraint-without-name')]
#[UniqueConstraint(fields: ['name', 'phone'])]
class DDC11246FieldsEntity
{
#[Id]
#[Column(type: 'integer')]
public int $id;

#[Column(type: 'string')]
public string $name;

#[Column(type: 'string')]
public string $phone;

#[Column(type: 'string')]
public string $email;
}

#[Entity]
#[Table('unique-constraint-without-name')]
#[UniqueConstraint(columns: ['name', 'phone'])]
class DDC11246ColumnsEntity
{
#[Id]
#[Column(type: 'integer')]
public int $id;

#[Column(type: 'string')]
public string $name;

#[Column(type: 'string')]
public string $phone;

#[Column(type: 'string')]
public string $email;
}

0 comments on commit 54a6dc9

Please sign in to comment.