|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\Model\Doctrine\DBAL\Command; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\DBAL\DriverManager; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* This file is part of the Doctrine Bundle. |
|
14
|
|
|
* |
|
15
|
|
|
* The code was originally distributed inside the Symfony framework. |
|
16
|
|
|
* |
|
17
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
18
|
|
|
* (c) Doctrine Project, Benjamin Eberlei <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
final class CreateDatabaseCommand |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Connection |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $connection; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Connection $connection |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Connection $connection) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->connection = $connection; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param InputInterface $input |
|
37
|
|
|
* @param OutputInterface $output |
|
38
|
|
|
* |
|
39
|
|
|
* @return int|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __invoke(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
|
|
$parameters = $this->getParameters(); |
|
44
|
|
|
|
|
45
|
|
|
$name = $this->getName($parameters); |
|
46
|
|
|
|
|
47
|
|
|
// Need to get rid of _every_ occurrence of dbname from connection configuration |
|
48
|
|
|
unset($parameters['dbname'], $parameters['path'], $parameters['url']); |
|
49
|
|
|
|
|
50
|
|
|
$tmpConnection = DriverManager::getConnection($parameters); |
|
51
|
|
|
$shouldNotCreateDatabase = in_array($name, $tmpConnection->getSchemaManager()->listDatabases()); |
|
52
|
|
|
|
|
53
|
|
|
// Only quote if we don't have a path |
|
54
|
|
|
if (!isset($parameters['path'])) { |
|
55
|
|
|
$name = $tmpConnection->getDatabasePlatform()->quoteSingleIdentifier($name); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $this->createDatabase($output, $tmpConnection, $name, $shouldNotCreateDatabase); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
private function getParameters(): array |
|
65
|
|
|
{ |
|
66
|
|
|
$params = $this->connection->getParams(); |
|
67
|
|
|
|
|
68
|
|
|
if (isset($params['master'])) { |
|
69
|
|
|
$params = $params['master']; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $params; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
private function getName(array $parameters): string |
|
79
|
|
|
{ |
|
80
|
|
|
if (isset($parameters['path'])) { |
|
81
|
|
|
return $parameters['path']; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (isset($parameters['dbname'])) { |
|
85
|
|
|
return $parameters['dbname']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter."); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param OutputInterface $output |
|
93
|
|
|
* @param Connection $tmpConnection |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @param bool $shouldNotCreateDatabase |
|
96
|
|
|
* |
|
97
|
|
|
* @return int |
|
98
|
|
|
*/ |
|
99
|
|
|
private function createDatabase( |
|
100
|
|
|
OutputInterface $output, |
|
101
|
|
|
Connection $tmpConnection, |
|
102
|
|
|
string $name, |
|
103
|
|
|
bool $shouldNotCreateDatabase |
|
104
|
|
|
): int { |
|
105
|
|
|
try { |
|
106
|
|
|
if ($shouldNotCreateDatabase) { |
|
107
|
|
|
$output->writeln(sprintf('<info>Database <comment>%s</comment> already exists.</info>', $name)); |
|
108
|
|
|
} else { |
|
109
|
|
|
$tmpConnection->getSchemaManager()->createDatabase($name); |
|
110
|
|
|
$output->writeln(sprintf('<info>Created database <comment>%s</comment>', $name)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return 0; |
|
114
|
|
|
} catch (\Exception $e) { |
|
115
|
|
|
$output->writeln(sprintf('<error>Could not create database <comment>%s</comment></error>', $name)); |
|
116
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
|
117
|
|
|
|
|
118
|
|
|
return 1; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|