Skip to content

Commit 4acb369

Browse files
authored
Merge pull request #1977 from alcaeus/fix-sharding-mongodb-4.0
[1.2] Fix sharding commands for MongoDB 4.0
2 parents e669957 + eeef654 commit 4acb369

6 files changed

Lines changed: 30 additions & 5 deletions

File tree

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ env:
1313
- DOCTRINE_MONGODB_SERVER="mongodb://localhost:27017"
1414
- KEY_SERVER="hkp://keyserver.ubuntu.com:80"
1515
- MONGO_REPO_URI="https://repo.mongodb.org/apt/ubuntu"
16-
- MONGO_REPO_TYPE="precise/mongodb-org/"
16+
- MONGO_REPO_TYPE="trusty/mongodb-org/"
1717
- SOURCES_LOC="/etc/apt/sources.list.d/mongodb.list"
1818
- DRIVER_VERSION="stable"
1919
- ADAPTER_VERSION="^1.0.0"
@@ -26,6 +26,8 @@ matrix:
2626
env: DRIVER_VERSION="1.6.7" COMPOSER_FLAGS="--prefer-lowest" SERVER_VERSION="3.2" KEY_ID="EA312927"
2727
- php: 7.2
2828
env: SERVER_VERSION="3.6" KEY_ID="2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5"
29+
- php: 7.3
30+
env: SERVER_VERSION="4.0" KEY_ID="9DA31620334BD75D9DCB49F368818C72E52529D4"
2931

3032
before_install:
3133
- sudo apt-key adv --keyserver ${KEY_SERVER} --recv ${KEY_ID}

lib/Doctrine/ODM/MongoDB/SchemaManager.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,21 @@
2222
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
2323
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
2424
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
25+
use function in_array;
2526
use function sprintf;
2627

2728
class 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

tests/Doctrine/ODM/MongoDB/Tests/Functional/EnsureShardingTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
66
use Doctrine\ODM\MongoDB\Tests\BaseTest;
77

8+
/**
9+
* @group sharding
10+
*/
811
class EnsureShardingTest extends BaseTest
912
{
1013
public function setUp()
@@ -35,6 +38,7 @@ public function testEnsureShardingForCollectionWithDocuments()
3538
$doc = array('title' => 'hey', 'k' => 'hi');
3639
$collection->insert($doc);
3740

41+
$this->dm->getSchemaManager()->ensureDocumentIndexes($class);
3842
$this->dm->getSchemaManager()->ensureDocumentSharding($class);
3943

4044
$indexes = $collection->getIndexInfo();
@@ -68,6 +72,7 @@ public function testEnsureShardingForCollectionWithData()
6872
$this->dm->flush();
6973

7074
$class = \Documents\Sharded\ShardedOne::class;
75+
$this->dm->getSchemaManager()->ensureDocumentIndexes($class);
7176
$this->dm->getSchemaManager()->ensureDocumentSharding($class);
7277

7378
$collection = $this->dm->getDocumentCollection($class);

tests/Doctrine/ODM/MongoDB/Tests/Functional/ShardKeyTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Doctrine\ODM\MongoDB\Tests\QueryLogger;
88
use Documents\Sharded\ShardedOne;
99

10+
/**
11+
* @group sharding
12+
*/
1013
class ShardKeyTest extends BaseTest
1114
{
1215
/**

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
88
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
99
use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock;
10+
use Documents\Sharded\ShardedOne;
1011
use PHPUnit\Framework\TestCase;
1112

1213
class SchemaManagerTest extends TestCase
@@ -18,6 +19,7 @@ class SchemaManagerTest extends TestCase
1819
\Documents\CmsProduct::class,
1920
\Documents\Comment::class,
2021
\Documents\SimpleReferenceUser::class,
22+
ShardedOne::class,
2123
);
2224

2325
private $someNonIndexedClasses = array(

tests/Documents/Sharded/ShardedOne.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @ODM\Document(collection="sharded.one")
9+
* @ODM\Index(keys={"k"="asc"})
910
* @ODM\ShardKey(keys={"k"="asc"})
1011
*/
1112
class ShardedOne

0 commit comments

Comments
 (0)