Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider column lengths when comparing indices #3415

Merged
merged 1 commit into from Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/Doctrine/DBAL/Schema/Index.php
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)
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
=== array_filter($other->options['lengths'] ?? [], $filter);
}
}
30 changes: 30 additions & 0 deletions tests/Doctrine/Tests/DBAL/Schema/IndexTest.php
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],
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
];
}

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