Skip to content

Commit a240864

Browse files
jwagemorozov
authored andcommitted
Merge pull request #3519 from doctrine/fix-unique-constraint-with-empty-name
Fix UniqueConstraint with empty name.
2 parents 14ea980 + 76bc3de commit a240864

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

lib/Doctrine/DBAL/Schema/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ protected function _addUniqueConstraint(UniqueConstraint $constraint)
863863
$name = strlen($constraint->getName())
864864
? $constraint->getName()
865865
: $this->_generateIdentifierName(
866-
array_merge((array) $this->getName(), $constraint->getLocalColumns()),
866+
array_merge((array) $this->getName(), $constraint->getColumns()),
867867
'fk',
868868
$this->_getMaxIdentifierLength()
869869
);

phpstan.neon.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ parameters:
5656
# weird class name, represented in stubs as OCI_(Lob|Collection)
5757
- '~unknown class OCI-(Lob|Collection)~'
5858

59-
# https://github.com/doctrine/dbal/issues/3236
60-
- '~^Call to an undefined method Doctrine\\DBAL\\Schema\\UniqueConstraint::getLocalColumns\(\)~'
61-
6259
# https://github.com/doctrine/dbal/issues/3237
6360
- '~^Call to an undefined method Doctrine\\DBAL\\Driver\\PDOStatement::nextRowset\(\)~'
6461

tests/Doctrine/Tests/DBAL/Schema/TableTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
use Doctrine\DBAL\Schema\Index;
1313
use Doctrine\DBAL\Schema\SchemaException;
1414
use Doctrine\DBAL\Schema\Table;
15+
use Doctrine\DBAL\Schema\UniqueConstraint;
1516
use Doctrine\DBAL\Types\Type;
1617
use Doctrine\Tests\DbalTestCase;
17-
use function array_shift;
18+
use function array_keys;
1819
use function current;
1920

2021
class TableTest extends DbalTestCase
@@ -206,7 +207,11 @@ public function testConstraints() : void
206207
$constraints = $tableA->getForeignKeys();
207208

208209
self::assertCount(1, $constraints);
209-
self::assertSame($constraint, array_shift($constraints));
210+
211+
$constraintNames = array_keys($constraints);
212+
213+
self::assertSame('fk_8c736521', $constraintNames[0]);
214+
self::assertSame($constraint, $constraints['fk_8c736521']);
210215
}
211216

212217
public function testOptions() : void
@@ -887,4 +892,33 @@ public static function getNormalizesAssetNames() : iterable
887892
['"FOO"'],
888893
];
889894
}
895+
896+
public function testUniqueConstraintWithEmptyName() : void
897+
{
898+
$columns = [
899+
new Column('column1', Type::getType(Type::STRING)),
900+
new Column('column2', Type::getType(Type::STRING)),
901+
new Column('column3', Type::getType(Type::STRING)),
902+
new Column('column4', Type::getType(Type::STRING)),
903+
];
904+
905+
$uniqueConstraints = [
906+
new UniqueConstraint('', ['column1', 'column2']),
907+
new UniqueConstraint('', ['column3', 'column4']),
908+
];
909+
910+
$table = new Table('test', $columns, [], $uniqueConstraints);
911+
912+
$constraints = $table->getUniqueConstraints();
913+
914+
self::assertCount(2, $constraints);
915+
916+
$constraintNames = array_keys($constraints);
917+
918+
self::assertSame('fk_d87f7e0c341ce00bad15b1b1', $constraintNames[0]);
919+
self::assertSame('fk_d87f7e0cda12812744761484', $constraintNames[1]);
920+
921+
self::assertSame($uniqueConstraints[0], $constraints['fk_d87f7e0c341ce00bad15b1b1']);
922+
self::assertSame($uniqueConstraints[1], $constraints['fk_d87f7e0cda12812744761484']);
923+
}
890924
}

0 commit comments

Comments
 (0)