|
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 UpdateCommand extends AbstractCommand |
|
18
|
|
|
{ |
|
19
|
|
|
protected function configure() |
|
20
|
|
|
{ |
|
21
|
|
|
parent::configure(); |
|
22
|
|
|
|
|
23
|
|
|
$this |
|
24
|
|
|
->setName('odm:schema:update') |
|
25
|
|
|
->addOption('class', 'c', InputOption::VALUE_OPTIONAL, 'Document class to process (default: all classes)') |
|
26
|
|
|
->setDescription('Update indexes for your documents'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
33
|
|
|
{ |
|
34
|
|
|
$class = $input->getOption('class'); |
|
35
|
|
|
|
|
36
|
|
|
$sm = $this->getSchemaManager(); |
|
37
|
|
|
$isErrored = false; |
|
38
|
|
|
|
|
39
|
|
|
try { |
|
40
|
|
|
if (is_string($class)) { |
|
41
|
|
|
$this->processDocumentIndex($sm, $class, $this->getMaxTimeMsFromInput($input), $this->getWriteConcernFromInput($input)); |
|
42
|
|
|
$output->writeln(sprintf('Updated <comment>index(es)</comment> for <info>%s</info>', $class)); |
|
43
|
|
|
} else { |
|
44
|
|
|
$this->processIndex($sm, $this->getMaxTimeMsFromInput($input), $this->getWriteConcernFromInput($input)); |
|
45
|
|
|
$output->writeln('Updated <comment>indexes</comment> for <info>all classes</info>'); |
|
46
|
|
|
} |
|
47
|
|
|
} catch (Throwable $e) { |
|
48
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>'); |
|
49
|
|
|
$isErrored = true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $isErrored ? 255 : 0; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function processDocumentIndex(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
|
56
|
|
|
{ |
|
57
|
|
|
$sm->updateDocumentIndexes($document, $maxTimeMs, $writeConcern); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
protected function processIndex(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
|
61
|
|
|
{ |
|
62
|
|
|
$sm->updateIndexes($maxTimeMs, $writeConcern); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @throws BadMethodCallException |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function processDocumentCollection(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
|
69
|
|
|
{ |
|
70
|
|
|
throw new BadMethodCallException('Cannot update a document collection'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @throws BadMethodCallException |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function processCollection(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
|
77
|
|
|
{ |
|
78
|
|
|
throw new BadMethodCallException('Cannot update a collection'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @throws BadMethodCallException |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function processDocumentDb(SchemaManager $sm, string $document, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
|
85
|
|
|
{ |
|
86
|
|
|
throw new BadMethodCallException('Cannot update a document database'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @throws BadMethodCallException |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function processDb(SchemaManager $sm, ?int $maxTimeMs, ?WriteConcern $writeConcern) |
|
93
|
|
|
{ |
|
94
|
|
|
throw new BadMethodCallException('Cannot update a database'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|