1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Algatux\InfluxDbBundle\Command; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Drops database from command line. |
13
|
|
|
*/ |
14
|
|
|
final class DropDatabaseCommand extends AbstractConnectionCommand |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
protected function configure() |
20
|
|
|
{ |
21
|
|
|
parent::configure(); |
22
|
|
|
|
23
|
|
|
$this |
24
|
|
|
->setName('algatux:influx:database:drop') |
25
|
|
|
->setDescription('Drops the configured database') |
26
|
|
|
->addOption('if-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist') |
27
|
|
|
->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action') |
28
|
|
|
; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
if (!$input->getOption('force')) { |
37
|
|
|
$this->io->error(sprintf(<<<'EOF' |
38
|
|
|
ATTENTION: This operation should not be executed in a production environment. |
39
|
|
|
Would drop the database named "%s". |
40
|
|
|
Please run the operation with --force to execute. |
41
|
|
|
All data will be lost! |
42
|
|
|
EOF |
43
|
|
|
, $this->connection->getName())); |
44
|
|
|
|
45
|
|
|
return 1; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
if (!$this->connection->exists() && !$input->getOption('if-exists')) { |
|
|
|
|
49
|
|
|
$this->io->error('Database "'.$this->connection->getName().'" does not exist.'); |
50
|
|
|
|
51
|
|
|
return 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
if (!$this->connection->exists()) { |
|
|
|
|
55
|
|
|
$this->io->comment('Database <comment>'.$this->connection->getName().'</comment> does not exist. Skipped.'); |
56
|
|
|
|
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->connection->drop(); |
61
|
|
|
$this->io->success('Dropped database "'.$this->connection->getName().'".'); |
62
|
|
|
|
63
|
|
|
return 0; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.