Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema manager should handle shard key on reference #1693

Merged
merged 1 commit into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/Doctrine/ODM/MongoDB/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,11 +593,27 @@ private function runShardCollectionCommand(string $documentName, ?WriteConcern $
$shardKey = $class->getShardKey();
$adminDb = $this->dm->getClient()->selectDatabase('admin');

$shardKeyPart = [];
foreach ($shardKey['keys'] as $key => $order) {
if ($class->hasField($key)) {
$mapping = $class->getFieldMapping($key);
$fieldName = $mapping['name'];

if ($class->isSingleValuedReference($key)) {
$fieldName = ClassMetadata::getReferenceFieldName($mapping['storeAs'], $fieldName);
}
} else {
$fieldName = $key;
}

$shardKeyPart[$fieldName] = $order;
}

return $adminDb->command(
array_merge(
[
'shardCollection' => $dbName . '.' . $class->getCollection(),
'key' => $shardKey['keys'],
'key' => $shardKeyPart,
],
$this->getWriteOptions(null, $writeConcern)
)
Expand Down
18 changes: 18 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/EnsureShardingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\ODM\MongoDB\MongoDBException;
use Doctrine\ODM\MongoDB\Tests\BaseTest;
use Documents\Sharded\ShardedByUser;
use Documents\Sharded\ShardedOne;
use Documents\Sharded\ShardedOneWithDifferentKey;
use function iterator_to_array;
Expand Down Expand Up @@ -97,4 +98,21 @@ public function testEnsureShardingForCollectionWithShardingEnabled()

$this->assertTrue($stats['sharded']);
}

public function testEnsureDocumentShardingWithShardByReference()
{
$class = ShardedByUser::class;

$this->dm->getSchemaManager()->ensureDocumentIndexes($class);
$this->dm->getSchemaManager()->ensureDocumentSharding($class);

$collection = $this->dm->getDocumentCollection($class);
$stats = $this->dm->getDocumentDatabase($class)->command(['collstats' => $collection->getCollectionName()])->toArray()[0];
$indexes = iterator_to_array($collection->listIndexes());

$this->assertTrue($stats['sharded']);

$this->assertCount(2, $indexes);
$this->assertSame(['db_user.$id' => 1], $indexes[1]->getKey());
}
}
20 changes: 20 additions & 0 deletions tests/Documents/Sharded/ShardedByUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Documents\Sharded;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
* @ODM\Document(collection="sharded.users")
* @ODM\ShardKey(keys={"user"="asc"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI @notrix, I had to change this from desc to asc as the test failed with the error that the shard key would could only be ascending or hashed.

*/
class ShardedByUser
{
/** @ODM\Id */
public $id;

/** @ODM\ReferenceOne(targetDocument="Documents\Sharded\ShardedUser", name="db_user") */
public $user;
}