Skip to content

Commit

Permalink
Merge branch 'bpo/2.9/#3415' into 2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Dec 27, 2018
2 parents d16c92b + d807849 commit c7450ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/Doctrine/DBAL/Schema/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use InvalidArgumentException;
use function array_filter;
use function array_keys;
use function array_map;
use function array_search;
Expand Down Expand Up @@ -211,6 +212,10 @@ public function isFullfilledBy(Index $other)
return false;
}

if (! $this->hasSameColumnLengths($other)) {
return false;
}

if (! $this->isUnique() && ! $this->isPrimary()) {
// this is a special case: If the current key is neither primary or unique, any unique or
// primary key will always have the same effect for the index and there cannot be any constraint
Expand Down Expand Up @@ -336,4 +341,17 @@ private function samePartialIndex(Index $other)

return ! $this->hasOption('where') && ! $other->hasOption('where');
}

/**
* Returns whether the index has the same column lengths as the other
*/
private function hasSameColumnLengths(self $other) : bool
{
$filter = static function (?int $length) : bool {
return $length !== null;
};

return array_filter($this->options['lengths'] ?? [], $filter)
=== array_filter($other->options['lengths'] ?? [], $filter);
}
}
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/DBAL/Schema/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,36 @@ public function testOverrulesWithPartial()
self::assertTrue($another->overrules($partial));
}

/**
* @param string[] $columns
* @param int[]|null[] $lengths1
* @param int[]|null[] $lengths2
*
* @dataProvider indexLengthProvider
*/
public function testFulfilledWithLength(array $columns, array $lengths1, array $lengths2, bool $expected) : void
{
$index1 = new Index('index1', $columns, false, false, [], ['lengths' => $lengths1]);
$index2 = new Index('index2', $columns, false, false, [], ['lengths' => $lengths2]);

self::assertSame($expected, $index1->isFullfilledBy($index2));
self::assertSame($expected, $index2->isFullfilledBy($index1));
}

/**
* @return mixed[][]
*/
public static function indexLengthProvider() : iterable
{
return [
'empty' => [['column'], [], [], true],
'same' => [['column'], [64], [64], true],
'different' => [['column'], [32], [64], false],
'sparse-different-positions' => [['column1', 'column2'], [0 => 32], [1 => 32], false],
'sparse-same-positions' => [['column1', 'column2'], [null, 32], [1 => 32], true],
];
}

/**
* @group DBAL-220
*/
Expand Down

0 comments on commit c7450ab

Please sign in to comment.