1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\DriverManager; |
6
|
|
|
use Exception; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Database tool allows you to easily drop your configured databases. |
14
|
|
|
* |
15
|
|
|
* @final |
16
|
|
|
*/ |
17
|
|
|
class DropDatabaseDoctrineCommand extends DoctrineCommand |
18
|
|
|
{ |
19
|
|
|
const RETURN_CODE_NOT_DROP = 1; |
20
|
|
|
|
21
|
|
|
const RETURN_CODE_NO_FORCE = 2; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
|
|
protected function configure() |
27
|
|
|
{ |
28
|
|
|
$this |
29
|
|
|
->setName('doctrine:database:drop') |
30
|
|
|
->setDescription('Drops the configured database') |
31
|
|
|
->addOption('shard', 's', InputOption::VALUE_REQUIRED, 'The shard connection to use for this command') |
32
|
|
|
->addOption('connection', 'c', InputOption::VALUE_OPTIONAL, 'The connection to use for this command') |
33
|
|
|
->addOption('if-exists', null, InputOption::VALUE_NONE, 'Don\'t trigger an error, when the database doesn\'t exist') |
34
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Set this parameter to execute this action') |
35
|
|
|
->setHelp(<<<EOT |
36
|
|
|
The <info>%command.name%</info> command drops the default connections database: |
37
|
|
|
|
38
|
|
|
<info>php %command.full_name%</info> |
39
|
|
|
|
40
|
|
|
The <info>--force</info> parameter has to be used to actually drop the database. |
41
|
|
|
|
42
|
|
|
You can also optionally specify the name of a connection to drop the database for: |
43
|
|
|
|
44
|
|
|
<info>php %command.full_name% --connection=default</info> |
45
|
|
|
|
46
|
|
|
<error>Be careful: All data in a given database will be lost when executing this command.</error> |
47
|
|
|
EOT |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
55
|
|
|
{ |
56
|
|
|
$connectionName = $input->getOption('connection'); |
57
|
|
|
if (empty($connectionName)) { |
58
|
|
|
$connectionName = $this->getDoctrine()->getDefaultConnectionName(); |
59
|
|
|
} |
60
|
|
|
$connection = $this->getDoctrineConnection($connectionName); |
61
|
|
|
|
62
|
|
|
$ifExists = $input->getOption('if-exists'); |
63
|
|
|
|
64
|
|
|
$params = $connection->getParams(); |
65
|
|
|
if (isset($params['master'])) { |
66
|
|
|
$params = $params['master']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (isset($params['shards'])) { |
70
|
|
|
$shards = $params['shards']; |
71
|
|
|
// Default select global |
72
|
|
|
$params = array_merge($params, $params['global']); |
73
|
|
|
if ($input->getOption('shard')) { |
74
|
|
|
foreach ($shards as $shard) { |
75
|
|
|
if ($shard['id'] === (int) $input->getOption('shard')) { |
76
|
|
|
// Select sharded database |
77
|
|
|
$params = $shard; |
78
|
|
|
unset($params['id']); |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false); |
86
|
|
|
if (! $name) { |
87
|
|
|
throw new InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped."); |
88
|
|
|
} |
89
|
|
|
unset($params['dbname'], $params['url']); |
90
|
|
|
|
91
|
|
|
if (! $input->getOption('force')) { |
92
|
|
|
$output->writeln('<error>ATTENTION:</error> This operation should not be executed in a production environment.'); |
93
|
|
|
$output->writeln(''); |
94
|
|
|
$output->writeln(sprintf('<info>Would drop the database <comment>%s</comment> for connection named <comment>%s</comment>.</info>', $name, $connectionName)); |
95
|
|
|
$output->writeln('Please run the operation with --force to execute'); |
96
|
|
|
$output->writeln('<error>All data will be lost!</error>'); |
97
|
|
|
|
98
|
|
|
return self::RETURN_CODE_NO_FORCE; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
// Reopen connection without database name set |
102
|
|
|
// as some vendors do not allow dropping the database connected to. |
103
|
|
|
$connection->close(); |
104
|
|
|
$connection = DriverManager::getConnection($params); |
105
|
|
|
$shouldDropDatabase = ! $ifExists || in_array($name, $connection->getSchemaManager()->listDatabases()); |
106
|
|
|
|
107
|
|
|
// Only quote if we don't have a path |
108
|
|
|
if (! isset($params['path'])) { |
109
|
|
|
$name = $connection->getDatabasePlatform()->quoteSingleIdentifier($name); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
if ($shouldDropDatabase) { |
114
|
|
|
$connection->getSchemaManager()->dropDatabase($name); |
115
|
|
|
$output->writeln(sprintf('<info>Dropped database <comment>%s</comment> for connection named <comment>%s</comment></info>', $name, $connectionName)); |
116
|
|
|
} else { |
117
|
|
|
$output->writeln(sprintf('<info>Database <comment>%s</comment> for connection named <comment>%s</comment> doesn\'t exist. Skipped.</info>', $name, $connectionName)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return 0; |
121
|
|
|
} catch (Exception $e) { |
122
|
|
|
$output->writeln(sprintf('<error>Could not drop database <comment>%s</comment> for connection named <comment>%s</comment></error>', $name, $connectionName)); |
123
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
124
|
|
|
|
125
|
|
|
return self::RETURN_CODE_NOT_DROP; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|