Skip to content

Commit 2715d7c

Browse files
committed
Skip search indexes not enabled exception
If search indexes are not enabled on the server, we should simply skip any search index operation. This commit adds an error code to the search index error check already in place.
1 parent 6f4f0fb commit 2715d7c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/Doctrine/ODM/MongoDB/SchemaManager.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ final class SchemaManager
4747

4848
private const CODE_SHARDING_ALREADY_INITIALIZED = 23;
4949
private const CODE_COMMAND_NOT_SUPPORTED = 115;
50+
private const CODE_SEARCH_NOT_ENABLED = 31082;
5051

5152
private const ALLOWED_MISSING_INDEX_OPTIONS = [
5253
'background',
@@ -1079,6 +1080,11 @@ private function getWriteOptions(?int $maxTimeMs = null, ?WriteConcern $writeCon
10791080

10801081
private function isSearchIndexCommandException(CommandException $e): bool
10811082
{
1083+
// MongoDB 8.0+: "Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration."
1084+
if ($e->getCode() === self::CODE_SEARCH_NOT_ENABLED) {
1085+
return true;
1086+
}
1087+
10821088
// MongoDB 6.0.7+ and 7.0+: "Search indexes are only available on Atlas"
10831089
if ($e->getCode() === self::CODE_COMMAND_NOT_SUPPORTED && str_contains($e->getMessage(), 'Search index')) {
10841090
return true;
@@ -1090,7 +1096,7 @@ private function isSearchIndexCommandException(CommandException $e): bool
10901096
}
10911097

10921098
// Older server versions don't support $listSearchIndexes
1093-
// We don't check for an error code here as the code is not documented and we can't rely on it
1099+
// We don't check for an error code here as the code is not documented, and we can't rely on it
10941100
return str_contains($e->getMessage(), 'Unrecognized pipeline stage name: \'$listSearchIndexes\'');
10951101
}
10961102
}

tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,25 @@ public function testUpdateDocumentSearchIndexesNotSupportedForClassWithoutSearch
492492
$this->schemaManager->updateDocumentSearchIndexes(CmsProduct::class);
493493
}
494494

495+
public function testUpdateDocumentWhenSearchNotEnabled(): void
496+
{
497+
// Class has no search indexes, so if the server doesn't support them we assume everything is fine
498+
$collectionName = $this->dm->getClassMetadata(CmsProduct::class)->getCollection();
499+
$collection = $this->documentCollections[$collectionName];
500+
$collection
501+
->expects($this->once())
502+
->method('listSearchIndexes')
503+
->willThrowException($this->createSearchNotEnabledCommandException());
504+
$collection
505+
->expects($this->never())
506+
->method('dropSearchIndex');
507+
$collection
508+
->expects($this->never())
509+
->method('updateSearchIndex');
510+
511+
$this->schemaManager->updateDocumentSearchIndexes(CmsProduct::class);
512+
}
513+
495514
public function testUpdateDocumentSearchIndexesNotSupportedForClassWithoutSearchIndexesOnOlderServers(): void
496515
{
497516
// Class has no search indexes, so if the server doesn't support them we assume everything is fine
@@ -1375,6 +1394,11 @@ private function createSearchIndexCommandException(): CommandException
13751394
return new CommandException('PlanExecutor error during aggregation :: caused by :: Search index commands are only supported with Atlas.', 115);
13761395
}
13771396

1397+
private function createSearchNotEnabledCommandException(): CommandException
1398+
{
1399+
return new CommandException('Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration.', 31082);
1400+
}
1401+
13781402
private function createSearchIndexCommandExceptionForOlderServers(): CommandException
13791403
{
13801404
return new CommandException('Unrecognized pipeline stage name: \'$listSearchIndexes\'', 40234);

0 commit comments

Comments
 (0)