1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Command; |
4
|
|
|
|
5
|
|
|
use Db3v4l\Core\DatabaseSchemaManager; |
|
|
|
|
6
|
|
|
|
7
|
|
|
abstract class DatabaseManagingCommand extends SQLExecutingCommand |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
|
|
|
|
10
|
|
|
* @param string[][] $instanceList |
|
|
|
|
11
|
|
|
* @param array[] $dbSpecList key: db name (as used to identify configured databases), value: array('user': mandatory, 'dbname': mandatory, 'password': mandatory) |
|
|
|
|
12
|
|
|
* @return array 'succeeded': int, 'failed': int, 'results': same format as dbConfigurationManager::getInstanceConfiguration |
|
|
|
|
13
|
|
|
* @throws \Exception |
|
|
|
|
14
|
|
|
* |
15
|
|
|
* @todo make it easier to report back to the caller the errors that prevented creation of any DB |
16
|
|
|
*/ |
17
|
|
|
protected function createDatabases($instanceList, $dbSpecList) |
18
|
|
|
{ |
19
|
|
|
// Sadly, psql does not allow to create a db and a user using a multiple-sql-commands string, |
20
|
|
|
// and we have to resort to using temp files |
21
|
|
|
/// @todo can we make this safer? Ideally the new user name and pwd should neither hit disk nor the process list... |
22
|
|
|
$results = $this->executeSqlAction( |
23
|
|
|
$instanceList, |
24
|
|
|
'Creating new database & user', |
25
|
|
|
function ($schemaManager, $instanceName) use ($dbSpecList) { |
26
|
|
|
$dbConnectionSpec = $dbSpecList[$instanceName]; |
27
|
|
|
/** @var DatabaseSchemaManager $schemaManager */ |
|
|
|
|
28
|
|
|
return $schemaManager->getCreateDatabaseSqlAction( |
29
|
|
|
$dbConnectionSpec['dbname'], |
30
|
|
|
isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null, |
31
|
|
|
isset($dbConnectionSpec['password']) ? $dbConnectionSpec['password'] : null, |
32
|
|
|
(isset($dbConnectionSpec['charset']) && $dbConnectionSpec['charset'] != '') ? $dbConnectionSpec['charset'] : null |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
/// @todo the new connection spec should be generated by the schemaManager for each instance. This would allow |
38
|
|
|
/// f.e. connection specs for Oracle to be different when using PDBs vs when using schemas |
39
|
|
|
/// (this could be achieved by copying the 'dbname' member over the 'servicename' one, or unsetting 'servicename'...) |
40
|
|
|
$finalData = []; |
41
|
|
|
foreach($results['data'] as $instanceName => $data) { |
|
|
|
|
42
|
|
|
// check for failure in creation of temp db |
43
|
|
|
// @todo how can we tell apart correctly errors that actually prevented the db from being created from other errors? |
44
|
|
|
if (is_array($data) && isset($data['exitcode']) && $data['exitcode'] != 0) { |
45
|
|
|
continue; |
46
|
|
|
} |
47
|
|
|
$dbConnectionSpec = $dbSpecList[$instanceName]; |
48
|
|
|
$finalData[$instanceName] = $instanceList[$instanceName]; |
49
|
|
|
$finalData[$instanceName]['dbname'] = $dbConnectionSpec['dbname']; |
50
|
|
|
if (isset($dbConnectionSpec['user'])) { |
51
|
|
|
$finalData[$instanceName]['user'] = $dbConnectionSpec['user']; |
52
|
|
|
} |
53
|
|
|
if (isset($dbConnectionSpec['password'])) { |
54
|
|
|
$finalData[$instanceName]['password'] = $dbConnectionSpec['password']; |
55
|
|
|
} |
56
|
|
|
if (isset($dbConnectionSpec['charset'])) { |
57
|
|
|
$finalData[$instanceName]['charset'] = $dbConnectionSpec['charset']; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$results['data'] = $finalData; |
62
|
|
|
return $results; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @param string[][] $instanceList |
|
|
|
|
67
|
|
|
* @param array[] $dbSpecList key: db name (as used to identify configured databases), value: array('user': mandatory, 'dbname': mandatory, if unspecified assumed same as user) |
|
|
|
|
68
|
|
|
* @param bool $ifExists |
|
|
|
|
69
|
|
|
* @param string $executionStrategy |
|
|
|
|
70
|
|
|
* @return array 'succeeded': int, 'failed': int, 'results': string[] |
|
|
|
|
71
|
|
|
* @throws \Exception |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
protected function dropDatabases($instanceList, $dbSpecList, $ifExists = false) |
74
|
|
|
{ |
75
|
|
|
return $this->executeSqlAction( |
76
|
|
|
$instanceList, |
77
|
|
|
'Dropping of database & user', |
78
|
|
|
function ($schemaManager, $instanceName) use ($dbSpecList, $ifExists) { |
79
|
|
|
$dbConnectionSpec = $dbSpecList[$instanceName]; |
80
|
|
|
/** @var DatabaseSchemaManager $schemaManager */ |
|
|
|
|
81
|
|
|
return $schemaManager->getDropDatabaseSqlAction( |
82
|
|
|
$dbConnectionSpec['dbname'], |
83
|
|
|
isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null, |
84
|
|
|
$ifExists |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|