|
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
|
|
|
use Doctrine\DBAL\DriverManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Database tool allows you to easily drop and create your configured databases. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Fabien Potencier <[email protected]> |
|
26
|
|
|
* @author Jonathan H. Wage <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class CreateDatabaseDoctrineCommand extends DoctrineCommand |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritDoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function configure() |
|
34
|
|
|
{ |
|
35
|
|
|
$this |
|
36
|
|
|
->setName('doctrine:database:create') |
|
37
|
|
|
->setDescription('Creates the configured databases') |
|
38
|
|
|
->addOption('connection', null, InputOption::VALUE_OPTIONAL, 'The connection to use for this command') |
|
39
|
|
|
->setHelp(<<<EOT |
|
40
|
|
|
The <info>doctrine:database:create</info> command creates the default |
|
41
|
|
|
connections database: |
|
42
|
|
|
|
|
43
|
|
|
<info>php app/console doctrine:database:create</info> |
|
44
|
|
|
|
|
45
|
|
|
You can also optionally specify the name of a connection to create the |
|
46
|
|
|
database for: |
|
47
|
|
|
|
|
48
|
|
|
<info>php app/console doctrine:database:create --connection=default</info> |
|
49
|
|
|
EOT |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
57
|
|
|
{ |
|
58
|
|
|
$connection = $this->getDoctrineConnection($input->getOption('connection')); |
|
59
|
|
|
|
|
60
|
|
|
$params = $connection->getParams(); |
|
61
|
|
|
if (isset($params['master'])) { |
|
62
|
|
|
$params = $params['master']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$name = isset($params['path']) ? $params['path'] : (isset($params['dbname']) ? $params['dbname'] : false); |
|
66
|
|
|
if (!$name) { |
|
67
|
|
|
throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped."); |
|
68
|
|
|
} |
|
69
|
|
|
unset($params['dbname']); |
|
70
|
|
|
|
|
71
|
|
|
$tmpConnection = DriverManager::getConnection($params); |
|
72
|
|
|
|
|
73
|
|
|
// Only quote if we don't have a path |
|
74
|
|
|
if (!isset($params['path'])) { |
|
75
|
|
|
$name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$error = false; |
|
79
|
|
|
try { |
|
80
|
|
|
$tmpConnection->getSchemaManager()->createDatabase($name); |
|
81
|
|
|
$output->writeln(sprintf('<info>Created database for connection named <comment>%s</comment></info>', $name)); |
|
82
|
|
|
} catch (\Exception $e) { |
|
83
|
|
|
$output->writeln(sprintf('<error>Could not create database for connection named <comment>%s</comment></error>', $name)); |
|
84
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
|
85
|
|
|
$error = true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$tmpConnection->close(); |
|
89
|
|
|
|
|
90
|
|
|
return $error ? 1 : 0; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|