From ab5e9e393b94bfcf851d4f4188298f7e1c47479e Mon Sep 17 00:00:00 2001 From: Rok Motaln Date: Sun, 3 Mar 2024 16:02:48 +0100 Subject: [PATCH] Fix SchemaTool::getSchemaFromMetadata() uniqueConstraint without a predefined name (#11314) * Fix loading SchemaTool::getSchemaFromMetadata() uniqueConstraint without a name Fixes a type miss-match exception when reading a UniqueConstraint defined on an Entity which doesn't have a predefined name. * Fix deprecation on DBAL 3 --------- Co-authored-by: Alexander M. Turek --- src/Tools/SchemaTool.php | 2 +- tests/Tests/ORM/Tools/SchemaToolTest.php | 47 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Tools/SchemaTool.php b/src/Tools/SchemaTool.php index 2e02279cc0c..532d40d82a9 100644 --- a/src/Tools/SchemaTool.php +++ b/src/Tools/SchemaTool.php @@ -365,7 +365,7 @@ static function (ClassMetadata $class) use ($idMapping): bool { if (isset($class->table['uniqueConstraints'])) { foreach ($class->table['uniqueConstraints'] as $indexName => $indexData) { - $uniqIndex = new Index($indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []); + $uniqIndex = new Index('tmp__' . $indexName, $this->getIndexColumns($class, $indexData), true, false, [], $indexData['options'] ?? []); foreach ($table->getIndexes() as $tableIndexName => $tableIndex) { $method = method_exists($tableIndex, 'isFulfilledBy') ? 'isFulfilledBy' : 'isFullfilledBy'; diff --git a/tests/Tests/ORM/Tools/SchemaToolTest.php b/tests/Tests/ORM/Tools/SchemaToolTest.php index 83f70e85965..bc0e811f68f 100644 --- a/tests/Tests/ORM/Tools/SchemaToolTest.php +++ b/tests/Tests/ORM/Tools/SchemaToolTest.php @@ -374,6 +374,27 @@ public function testConfigurationSchemaIgnoredEntity(): void self::assertTrue($schema->hasTable('first_entity'), 'Table first_entity should exist.'); self::assertFalse($schema->hasTable('second_entity'), 'Table second_entity should not exist.'); } + + #[Group('11314')] + public function testLoadUniqueConstraintWithoutName(): void + { + $em = $this->getTestEntityManager(); + $entity = $em->getClassMetadata(GH11314Entity::class); + + $schemaTool = new SchemaTool($em); + $schema = $schemaTool->getSchemaFromMetadata([$entity]); + + self::assertTrue($schema->hasTable('GH11314Entity')); + + $tableEntity = $schema->getTable('GH11314Entity'); + + self::assertTrue($tableEntity->hasIndex('uniq_2d81a3ed5bf54558875f7fd5')); + + $tableIndex = $tableEntity->getIndex('uniq_2d81a3ed5bf54558875f7fd5'); + + self::assertTrue($tableIndex->isUnique()); + self::assertSame(['field', 'anotherField'], $tableIndex->getColumns()); + } } /** @@ -559,6 +580,32 @@ class IndexByFieldEntity public $fieldName; } +/** + * @Entity + * @Table(uniqueConstraints={@UniqueConstraint(columns={"field", "anotherField"})}) + */ +class GH11314Entity +{ + /** + * @Column(type="integer") + * @Id + * @var int + */ + private $id; + + /** + * @Column(name="field", type="string") + * @var string + */ + private $field; + + /** + * @Column(name="anotherField", type="string") + * @var string + */ + private $anotherField; +} + class IncorrectIndexByFieldEntity { /** @var int */