2222use Doctrine \ODM \MongoDB \Mapping \ClassMetadata ;
2323use Doctrine \ODM \MongoDB \Mapping \ClassMetadataFactory ;
2424use Doctrine \ODM \MongoDB \Mapping \ClassMetadataInfo ;
25+ use function in_array ;
2526use function sprintf ;
2627
2728class SchemaManager
2829{
30+ /**
31+ * @internal For internal use only. This constant will be private in 2.0.
32+ */
33+ const MONGODB_ERROR_ILLEGAL_OPERATION = 20 ;
34+
35+ /**
36+ * @internal For internal use only. This constant will be private in 2.0.
37+ */
38+ const MONGODB_ERROR_ALREADY_INITIALIZED = 23 ;
39+
2940 /**
3041 * @var DocumentManager
3142 */
@@ -650,9 +661,10 @@ public function ensureDocumentSharding($documentName, array $indexOptions = arra
650661 }
651662 } while (! $ done && $ try < 2 );
652663
653- // Starting with MongoDB 3.2, this command returns code 20 when a collection is already sharded.
654- // For older MongoDB versions, check the error message
655- if ($ result ['ok ' ] == 1 || (isset ($ result ['code ' ]) && $ result ['code ' ] == 20 ) || $ result ['errmsg ' ] == 'already sharded ' ) {
664+ // For MongoDB 3.2 and newer, this command returns code 20 when a collection is already sharded.
665+ // For MongoDB 4.0 and newer, this command returns code 23 when a collection is already sharded.
666+ // For older MongoDB versions, we check the error message
667+ if ($ result ['ok ' ] == 1 || (isset ($ result ['code ' ]) && in_array ($ result ['code ' ], [self ::MONGODB_ERROR_ILLEGAL_OPERATION , self ::MONGODB_ERROR_ALREADY_INITIALIZED ])) || $ result ['errmsg ' ] == 'already sharded ' ) {
656668 return ;
657669 }
658670
@@ -674,7 +686,7 @@ public function enableShardingForDbByDocumentName($documentName)
674686
675687 // Error code is only available with MongoDB 3.2. MongoDB 3.0 only returns a message
676688 // Thus, check code if it exists and fall back on error message
677- if ($ result ['ok ' ] == 1 || (isset ($ result ['code ' ]) && $ result ['code ' ] == 23 ) || $ result ['errmsg ' ] == 'already enabled ' ) {
689+ if ($ result ['ok ' ] == 1 || (isset ($ result ['code ' ]) && $ result ['code ' ] == self :: MONGODB_ERROR_ALREADY_INITIALIZED ) || $ result ['errmsg ' ] == 'already enabled ' ) {
678690 return ;
679691 }
680692
0 commit comments