|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the "RocketORM" package. |
|
5
|
|
|
* |
|
6
|
|
|
* https://github.com/RocketORM/ORM |
|
7
|
|
|
* |
|
8
|
|
|
* For the full license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Rocket\ORM\Command; |
|
13
|
|
|
|
|
14
|
|
|
use Rocket\ORM\Console\Command\AbstractCommand; |
|
15
|
|
|
use Rocket\ORM\Generator\Schema\Schema; |
|
16
|
|
|
use Rocket\ORM\Rocket; |
|
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Sylvain Lorinet <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class RocketDatabaseCreateCommand extends AbstractCommand |
|
24
|
|
|
{ |
|
25
|
|
|
protected function configure() |
|
26
|
|
|
{ |
|
27
|
|
|
$this |
|
28
|
|
|
->setName('database:create') |
|
29
|
|
|
->setDescription('Create the database, but not the tables') |
|
30
|
|
|
; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param InputInterface $input |
|
35
|
|
|
* @param OutputInterface $output |
|
36
|
|
|
* |
|
37
|
|
|
* @return int |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
|
|
$schemas = $this->getSchemas($this->getSchemaPath()); |
|
44
|
|
|
$connections = []; |
|
45
|
|
|
$pendingDatabaseCount = 0; |
|
46
|
|
|
|
|
47
|
|
|
/** @var Schema $schema */ |
|
48
|
|
|
foreach ($schemas as $schema) { |
|
49
|
|
|
if (!Rocket::getConnection($schema->connection)->isDatabaseCreated($schema->database)) { |
|
|
|
|
|
|
50
|
|
|
$connections[$schema->connection][] = $schema->database; |
|
51
|
|
|
++$pendingDatabaseCount; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$schemaCount = sizeof($schemas); |
|
56
|
|
|
if ($schemaCount == $pendingDatabaseCount) { |
|
57
|
|
|
$output->write(sprintf( |
|
58
|
|
|
'%d database%s will be created... ', |
|
59
|
|
|
$pendingDatabaseCount, |
|
60
|
|
|
1 < $pendingDatabaseCount ? 's' : '' |
|
61
|
|
|
)); |
|
62
|
|
|
} elseif (0 == $pendingDatabaseCount) { |
|
63
|
|
|
$output->writeln('<info>All databases are already created.</info>'); |
|
64
|
|
|
|
|
65
|
|
|
return 0; |
|
66
|
|
|
} else { |
|
67
|
|
|
$existDatabaseCount = $schemaCount - $pendingDatabaseCount; |
|
68
|
|
|
$output->write(sprintf( |
|
69
|
|
|
'%d database%s already exist, %d will be created... ', |
|
70
|
|
|
$existDatabaseCount, |
|
71
|
|
|
1 < $existDatabaseCount ? 's' : '', |
|
72
|
|
|
$pendingDatabaseCount |
|
73
|
|
|
)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
|
|
foreach ($connections as $connectionName => $databases) { |
|
78
|
|
|
$connection = Rocket::getConnection($connectionName); |
|
79
|
|
|
foreach ($databases as $databaseName) { |
|
80
|
|
|
if (!$connection->createDatabase($databaseName)) { |
|
|
|
|
|
|
81
|
|
|
throw new \RuntimeException('Cannot create the database "' . $databaseName . '"'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} catch (\Exception $e) { |
|
86
|
|
|
$output->writeln('<fg=red;options=bold>FAIL</fg=red;options=bold>'); |
|
87
|
|
|
|
|
88
|
|
|
throw $e; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$output->writeln('<info>Success</info>'); |
|
92
|
|
|
|
|
93
|
|
|
return 0; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: