1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
8
|
|
|
|
9
|
|
|
class DatabaseCreate extends DatabaseManagingCommand |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
protected static $defaultName = 'db3v4l:database:create'; |
12
|
|
|
|
13
|
|
|
protected function configure() |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$this |
16
|
|
|
->setDescription('Creates a database & associated user in parallel on all configured database instances') |
17
|
|
|
->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'The name of the database to create') |
18
|
|
|
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'The name of a user to create with r/w access to the new database. If omitted, no user will be created') |
19
|
|
|
->addOption('password', 'p', InputOption::VALUE_REQUIRED, 'The password. If omitted, a random one will be generated and echoed as part of results') |
20
|
|
|
->addOption('charset', 'c', InputOption::VALUE_REQUIRED, 'The collation/character-set to use for the database. If omitted, the default collation for the instance will be used') |
21
|
|
|
->addCommonOptions() |
|
|
|
|
22
|
|
|
; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @param InputInterface $input |
|
|
|
|
27
|
|
|
* @param OutputInterface $output |
|
|
|
|
28
|
|
|
* @return int |
|
|
|
|
29
|
|
|
* @throws \Exception |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
32
|
|
|
{ |
33
|
|
|
$start = microtime(true); |
34
|
|
|
|
35
|
|
|
// as per https://www.php.net/manual/en/function.ignore-user-abort.php: for cli scripts, it is probably a good idea |
36
|
|
|
// to use ignore_user_abort |
37
|
|
|
ignore_user_abort(true); |
38
|
|
|
|
39
|
|
|
$this->setOutput($output); |
40
|
|
|
$this->setVerbosity($output->getVerbosity()); |
41
|
|
|
|
42
|
|
|
$instanceList = $this->parseCommonOptions($input); |
43
|
|
|
|
44
|
|
|
$dbName = $input->getOption('database'); |
45
|
|
|
$userName = $input->getOption('user'); |
46
|
|
|
$password = $input->getOption('password'); |
47
|
|
|
$charset = $input->getOption('charset'); |
48
|
|
|
|
49
|
|
|
// BC api |
50
|
|
|
if ($dbName == null && $userName != null) { |
51
|
|
|
$dbName = $userName; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if ($dbName == null) { |
55
|
|
|
throw new \Exception("Please provide a database name"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($userName == null) { |
59
|
|
|
if ($password != null) { |
60
|
|
|
throw new \Exception("Option 'password' is only valid together with option 'user'"); |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
if ($password == null) { |
64
|
|
|
// 30 chars = max length for Oracle (at least up to 11g) |
65
|
|
|
/// @todo move to a constant |
66
|
|
|
$password = bin2hex(random_bytes(15)); |
67
|
|
|
|
68
|
|
|
// Should we warn the user always? To avoid breaking non-text-format, we can send it to stderr... |
69
|
|
|
// Otoh we give the password in the structured output that we produce |
70
|
|
|
//$this->writeErrorln("<info>Assigned password to the user: $password</info>"); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->outputFormat === 'text') { |
75
|
|
|
$this->writeln('<info>Creating databases...</info>'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$newDbSpecs = []; |
79
|
|
|
foreach($instanceList as $instanceName => $instanceSpecs) { |
|
|
|
|
80
|
|
|
$newDbSpecs[$instanceName] = [ |
81
|
|
|
'dbname' => $dbName |
82
|
|
|
]; |
83
|
|
|
if ($userName != null) { |
84
|
|
|
$newDbSpecs[$instanceName]['user'] = $userName; |
85
|
|
|
$newDbSpecs[$instanceName]['password'] = $password; |
86
|
|
|
} |
87
|
|
|
if ($charset != '') { |
88
|
|
|
$newDbSpecs[$instanceName]['charset'] = $charset; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$results = $this->createDatabases($instanceList, $newDbSpecs); |
93
|
|
|
|
94
|
|
|
$time = microtime(true) - $start; |
95
|
|
|
|
96
|
|
|
$this->writeResults($results, $time); |
97
|
|
|
|
98
|
|
|
return (int)$results['failed']; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|