Skip to content

Commit

Permalink
Merge branch '2.1.x'
Browse files Browse the repository at this point in the history
* 2.1.x:
  Added missing check for order of compound keys
  • Loading branch information
alcaeus committed Dec 12, 2020
2 parents db79000 + d8be1b9 commit 02406c5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
36 changes: 32 additions & 4 deletions lib/Doctrine/ODM/MongoDB/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
use MongoDB\Model\IndexInfo;
use function array_diff_key;
use function array_filter;
use function array_keys;
use function array_merge;
use function array_search;
use function array_unique;
use function array_values;
use function assert;
use function in_array;
use function is_string;
use function iterator_count;
use function iterator_to_array;
Expand Down Expand Up @@ -460,11 +464,35 @@ private function isEquivalentIndexKeys(IndexInfo $mongoIndex, array $documentInd
});
}

/* Avoid a strict equality check here. The numeric type returned by
* MongoDB may differ from the document index without implying that the
* indexes themselves are inequivalent. */
/* Avoid a strict equality check of the arrays here. The numeric type returned
* by MongoDB may differ from the document index without implying that the
* indexes themselves are inequivalent. The strict check of the keys asserts
* that the order of the keys remained the same. */
// phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator
return $mongoIndexKeys == $documentIndexKeys;
return $this->hasTextIndexesAtSamePosition($mongoIndex, $documentIndex) &&
array_keys($mongoIndexKeys) === array_keys($documentIndexKeys) &&
$mongoIndexKeys == $documentIndexKeys;
}

private function hasTextIndexesAtSamePosition(IndexInfo $mongoIndex, array $documentIndex) : bool
{
$mongoIndexKeys = $mongoIndex['key'];
$documentIndexKeys = $documentIndex['keys'];

if (! isset($mongoIndexKeys['_fts']) && ! in_array('text', $documentIndexKeys, true)) {
return true;
}

/*
* We unset _ftsx to avoid the uncertainty whether _fts really comes first and
* therefore denotes the position of the text index.
*/
unset($mongoIndexKeys['_ftsx']);

$mongoIndexTextPosition = array_search('_fts', array_keys($mongoIndexKeys), true);
$documentIndexTextPosition = array_search('text', array_values($documentIndexKeys), true);

return $mongoIndexTextPosition === $documentIndexTextPosition;
}

/**
Expand Down
31 changes: 26 additions & 5 deletions tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,16 @@ public function dataIsMongoIndexEquivalentToDocumentIndex()
'mongoIndex' => ['key' => ['foo' => 1]],
'documentIndex' => ['keys' => ['foo' => -1]],
],
'compoundIndexKeysSame' => [
'expected' => true,
'mongoIndex' => ['key' => ['foo' => 1, 'baz' => 1]],
'documentIndex' => ['keys' => ['foo' => 1, 'baz' => 1]],
],
'compoundIndexKeysSameDifferentOrder' => [
'expected' => false,
'mongoIndex' => ['key' => ['foo' => 1, 'baz' => 1]],
'documentIndex' => ['keys' => ['baz' => 1, 'foo' => 1]],
],
// Sparse option
'sparseOnlyInMongoIndex' => [
'expected' => false,
Expand Down Expand Up @@ -823,34 +833,45 @@ public function dataIsMongoTextIndexEquivalentToDocumentIndex()
],
'compoundIndexKeysSameAndWeightsSame' => [
'expected' => true,
'mongoIndex' => [
'key' => ['a' => -1, '_fts' => 'text', '_ftsx' => 1, 'd' => 1],
'weights' => ['b' => 1, 'c' => 2],
],
'documentIndex' => [
'keys' => ['a' => -1, 'b' => 'text', 'c' => 'text', 'd' => 1],
'options' => ['weights' => ['b' => 1, 'c' => 2]],
],
],
'compoundIndexKeysDifferentOrder' => [
'expected' => false,
'mongoIndex' => [
'key' => ['_fts' => 'text', '_ftsx' => 1, 'a' => -1, 'd' => 1],
'weights' => ['b' => 1, 'c' => 2],
],
'documentIndex' => [
'keys' => ['a' => -1, 'b' => 'text', 'c' => 'text', 'd' => 1],
'keys' => ['a' => -1, 'b' => 'text', 'c' => 'text', 'd' => 1],
'options' => ['weights' => ['b' => 1, 'c' => 2]],
],
],
'compoundIndexKeysSameAndWeightsDiffer' => [
'expected' => false,
'mongoIndex' => [
'key' => ['_fts' => 'text', '_ftsx' => 1, 'a' => -1, 'd' => 1],
'key' => ['a' => -1, '_fts' => 'text', '_ftsx' => 1, 'd' => 1],
'weights' => ['b' => 1, 'c' => 2],
],
'documentIndex' => [
'keys' => ['a' => -1, 'b' => 'text', 'c' => 'text', 'd' => 1],
'keys' => ['a' => -1, 'b' => 'text', 'c' => 'text', 'd' => 1],
'options' => ['weights' => ['b' => 3, 'c' => 2]],
],
],
'compoundIndexKeysDifferAndWeightsSame' => [
'expected' => false,
'mongoIndex' => [
'key' => ['_fts' => 'text', '_ftsx' => 1, 'a' => 1, 'd' => 1],
'key' => ['a' => 1, '_fts' => 'text', '_ftsx' => 1, 'd' => 1],
'weights' => ['b' => 1, 'c' => 2],
],
'documentIndex' => [
'keys' => ['a' => -1, 'b' => 'text', 'c' => 'text', 'd' => 1],
'keys' => ['a' => -1, 'b' => 'text', 'c' => 'text', 'd' => 1],
'options' => ['weights' => ['b' => 1, 'c' => 2]],
],
],
Expand Down

0 comments on commit 02406c5

Please sign in to comment.