DropDatabaseDoctrineCommand::configure()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Saxulum\DoctrineOrmCommands\Command;
16
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * Database tool allows you to easily drop and create your configured databases.
23
 *
24
 * @author Fabien Potencier <[email protected]>
25
 * @author Jonathan H. Wage <[email protected]>
26
 */
27
class DropDatabaseDoctrineCommand extends DoctrineCommand
28
{
29
    const RETURN_CODE_NOT_DROP = 1;
30
31
    const RETURN_CODE_NO_FORCE = 2;
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    protected function configure()
37
    {
38
        $this
39
            ->setName('doctrine:database:drop')
40
            ->setDescription('Drops the configured databases')
41
            ->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command')
42
            ->addOption('force', null, InputOption::VALUE_NONE, 'Set this parameter to execute this action')
43
            ->setHelp(<<<EOT
44
The <info>doctrine:database:drop</info> command drops the default connections
45
database:
46
47
<info>php app/console doctrine:database:drop</info>
48
49
The --force parameter has to be used to actually drop the database.
50
51
You can also optionally specify the name of a connection to drop the database
52
for:
53
54
<info>php app/console doctrine:database:drop --connection=default</info>
55
56
<error>Be careful: All data in a given database will be lost when executing
57
this command.</error>
58
EOT
59
            );
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    protected function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $connection = $this->getDoctrineConnection($input->getOption('connection'));
68
69
        $params = $connection->getParams();
70
        if (isset($params['master'])) {
71
            $params = $params['master'];
72
        }
73
74
        $name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false);
75
        if (!$name) {
76
            throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
77
        }
78
        unset($params['dbname']);
79
80
        if ($input->getOption('force')) {
81
            // Only quote if we don't have a path
82
            if (!isset($params['path'])) {
83
                $name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name);
84
            }
85
86
            try {
87
                $connection->getSchemaManager()->dropDatabase($name);
88
                $output->writeln(sprintf('<info>Dropped database for connection named <comment>%s</comment></info>', $name));
89
            } catch (\Exception $e) {
90
                $output->writeln(sprintf('<error>Could not drop database for connection named <comment>%s</comment></error>', $name));
91
                $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
92
93
                return self::RETURN_CODE_NOT_DROP;
94
            }
95
        } else {
96
            $output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.');
97
            $output->writeln('');
98
            $output->writeln(sprintf('<info>Would drop the database named <comment>%s</comment>.</info>', $name));
99
            $output->writeln('Please run the operation with --force to execute');
100
            $output->writeln('<error>All data will be lost!</error>');
101
102
            return self::RETURN_CODE_NO_FORCE;
103
        }
104
    }
105
}
106