1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; |
6
|
|
|
|
7
|
|
|
use BadMethodCallException; |
8
|
|
|
use Doctrine\ODM\MongoDB\SchemaManager; |
9
|
|
|
use MongoDB\Driver\WriteConcern; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Throwable; |
14
|
|
|
use function is_string; |
15
|
|
|
use function sprintf; |
16
|
|
|
|
17
|
|
|
class ShardCommand extends AbstractCommand |
18
|
|
|
{ |
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
$this |
22
|
|
|
->setName('odm:schema:shard') |
23
|
|
|
->addOption('class', 'c', InputOption::VALUE_OPTIONAL, 'Document class to process (default: all classes)') |
24
|
|
|
->setDescription('Enable sharding for selected documents'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return int |
29
|
|
|
*/ |
30
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
31
|
|
|
{ |
32
|
|
|
$class = $input->getOption('class'); |
33
|
|
|
|
34
|
|
|
$sm = $this->getSchemaManager(); |
35
|
|
|
$isErrored = false; |
36
|
|
|
|
37
|
|
|
try { |
38
|
|
|
if (is_string($class)) { |
39
|
|
|
$this->processDocumentIndex($sm, $class, null, $this->getWriteConcernFromInput($input)); |
40
|
|
|
$output->writeln(sprintf('Enabled sharding for <info>%s</info>', $class)); |
41
|
|
|
} else { |
42
|
|
|
$this->processIndex($sm, null, $this->getWriteConcernFromInput($input)); |
43
|
|
|
$output->writeln('Enabled sharding for <info>all classes</info>'); |
44
|
|
|
} |
45
|
|
|
} catch (Throwable $e) { |
46
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
47
|
|
|
$isErrored = true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $isErrored ? 255 : 0; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function processDocumentIndex(SchemaManager $sm, string $document, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) |
54
|
|
|
{ |
55
|
|
|
$sm->ensureDocumentSharding($document, $writeConcern); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function processIndex(SchemaManager $sm, ?int $maxTimeMs = null, ?WriteConcern $writeConcern = null) |
59
|
|
|
{ |
60
|
|
|
$sm->ensureSharding($writeConcern); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws BadMethodCallException |
65
|
|
|
*/ |
66
|
|
|
protected function processDocumentCollection(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
67
|
|
|
{ |
68
|
|
|
throw new BadMethodCallException('Cannot update a document collection'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws BadMethodCallException |
73
|
|
|
*/ |
74
|
|
|
protected function processCollection(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
75
|
|
|
{ |
76
|
|
|
throw new BadMethodCallException('Cannot update a collection'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @throws BadMethodCallException |
81
|
|
|
*/ |
82
|
|
|
protected function processDocumentDb(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
83
|
|
|
{ |
84
|
|
|
throw new BadMethodCallException('Cannot update a document database'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws BadMethodCallException |
89
|
|
|
*/ |
90
|
|
|
protected function processDb(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
91
|
|
|
{ |
92
|
|
|
throw new BadMethodCallException('Cannot update a database'); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|