From 0bf1fd5cb5a31f0dfeeb1ecffb613a15c953bdc2 Mon Sep 17 00:00:00 2001 From: Ksaveras Sakys Date: Sat, 26 Sep 2020 11:24:49 +0100 Subject: [PATCH] Ignore index prefix lengths for spatial indexes --- lib/Doctrine/DBAL/Schema/Index.php | 10 ++++- .../Doctrine/Tests/DBAL/Schema/IndexTest.php | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/DBAL/Schema/Index.php b/lib/Doctrine/DBAL/Schema/Index.php index 4880da717e2..06d8496a13d 100644 --- a/lib/Doctrine/DBAL/Schema/Index.php +++ b/lib/Doctrine/DBAL/Schema/Index.php @@ -223,7 +223,7 @@ public function isFullfilledBy(Index $other) return false; } - if (! $this->hasSameColumnLengths($other)) { + if (! $this->sameFlag('spatial', $other) && ! $this->hasSameColumnLengths($other)) { return false; } @@ -373,4 +373,12 @@ private function hasSameColumnLengths(self $other): bool return array_filter($this->options['lengths'] ?? [], $filter) === array_filter($other->options['lengths'] ?? [], $filter); } + + /** + * Returns whether the index has the same flag as the other + */ + private function sameFlag(string $flag, Index $other): bool + { + return $this->hasFlag($flag) && $other->hasFlag($flag); + } } diff --git a/tests/Doctrine/Tests/DBAL/Schema/IndexTest.php b/tests/Doctrine/Tests/DBAL/Schema/IndexTest.php index d5d2b8761b8..47d896d56e5 100644 --- a/tests/Doctrine/Tests/DBAL/Schema/IndexTest.php +++ b/tests/Doctrine/Tests/DBAL/Schema/IndexTest.php @@ -173,4 +173,44 @@ public function testOptions(): void self::assertSame('name IS NULL', $idx2->getOption('WHERE')); self::assertSame(['where' => 'name IS NULL'], $idx2->getOptions()); } + + /** + * @param string[] $flags1 + * @param string[] $flags2 + * @param int[] $lengths1 + * @param int[] $lengths2 + * + * @dataProvider spatialIndexProvider + */ + public function testFulfilledSpatialIndex( + array $flags1, + array $flags2, + array $lengths1, + array $lengths2, + bool $expected + ): void { + $index1 = new Index('index1', ['geo_location'], false, false, $flags1, ['lengths' => $lengths1]); + $index2 = new Index('index2', ['geo_location'], false, false, $flags2, ['lengths' => $lengths2]); + + self::assertSame($expected, $index1->isFullfilledBy($index2)); + self::assertSame($expected, $index2->isFullfilledBy($index1)); + } + + /** + * @return mixed[][] + */ + public static function spatialIndexProvider(): iterable + { + return [ + 'spatial-indexes-without-length' => [['spatial'], ['spatial'], [], [], true], + 'spatial-indexes-with-2nd-length' => [['spatial'], ['spatial'], [], [32], true], + 'spatial-indexes-with-length' => [['spatial'], ['spatial'], [32], [32], true], + 'spatial-indexes-with-different-lengths' => [['spatial'], ['spatial'], [32], [24], true], + 'spatial-indexes-with-1st-length' => [['spatial'], ['spatial'], [24], [], true], + 'index-with-length-compare-to-spatial-index' => [[], ['spatial'], [24], [], false], + 'spatial-with-length-index-compare-to-index' => [['spatial'], [], [24], [], false], + 'index-compare-to-spatial-with-length-index' => [[], ['spatial'], [], [24], false], + 'spatial-index-compare-to-index-with-length' => [['spatial'], [], [], [24], false], + ]; + } }