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
|
|
|
protected function createDatabases($instanceList, $dbSpecList) |
16
|
|
|
{ |
17
|
|
|
// Sadly, psql does not allow to create a db and a user using a multiple-sql-commands string, |
18
|
|
|
// and we have to resort to using temp files |
19
|
|
|
/// @todo can we make this safer? Ideally the new user name and pwd should neither hit disk nor the process list... |
20
|
|
|
$results = $this->executeSqlAction( |
21
|
|
|
$instanceList, |
22
|
|
|
'Creating new database & user', |
23
|
|
|
function ($schemaManager, $instanceName) use ($dbSpecList) { |
24
|
|
|
$dbConnectionSpec = $dbSpecList[$instanceName]; |
25
|
|
|
/** @var DatabaseSchemaManager $schemaManager */ |
|
|
|
|
26
|
|
|
return $schemaManager->getCreateDatabaseSqlAction( |
27
|
|
|
$dbConnectionSpec['dbname'], |
28
|
|
|
isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null, |
29
|
|
|
isset($dbConnectionSpec['password']) ? $dbConnectionSpec['password'] : null, |
30
|
|
|
(isset($dbConnectionSpec['charset']) && $dbConnectionSpec['charset'] != '') ? $dbConnectionSpec['charset'] : null |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$finalData = []; |
36
|
|
|
foreach($results['data'] as $instanceName => $data) { |
|
|
|
|
37
|
|
|
$dbConnectionSpec = $dbSpecList[$instanceName]; |
38
|
|
|
$finalData[$instanceName] = $instanceList[$instanceName]; |
39
|
|
|
$finalData[$instanceName]['dbname'] = $dbConnectionSpec['dbname']; |
40
|
|
|
if (isset($dbConnectionSpec['user'])) { |
41
|
|
|
$finalData[$instanceName]['user'] = $dbConnectionSpec['user']; |
42
|
|
|
} |
43
|
|
|
if (isset($dbConnectionSpec['password'])) { |
44
|
|
|
$finalData[$instanceName]['password'] = $dbConnectionSpec['password']; |
45
|
|
|
} |
46
|
|
|
if (isset($dbConnectionSpec['charset'])) { |
47
|
|
|
$finalData[$instanceName]['charset'] = $dbConnectionSpec['charset']; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$results['data'] = $finalData; |
52
|
|
|
return $results; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* @param string[][] $instanceList |
|
|
|
|
57
|
|
|
* @param array[] $dbSpecList key: db name (as used to identify configured databases), value: array('user': mandatory, 'dbname': mandatory, if unspecified assumed same as user) |
|
|
|
|
58
|
|
|
* @param bool $ifExists |
|
|
|
|
59
|
|
|
* @return array 'succeeded': int, 'failed': int, 'results': string[] |
|
|
|
|
60
|
|
|
* @throws \Exception |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
protected function dropDatabases($instanceList, $dbSpecList, $ifExists = false) |
63
|
|
|
{ |
64
|
|
|
return $this->executeSqlAction( |
65
|
|
|
$instanceList, |
66
|
|
|
'Dropping of database & user', |
67
|
|
|
function ($schemaManager, $instanceName) use ($dbSpecList, $ifExists) { |
68
|
|
|
$dbConnectionSpec = $dbSpecList[$instanceName]; |
69
|
|
|
/** @var DatabaseSchemaManager $schemaManager */ |
|
|
|
|
70
|
|
|
return $schemaManager->getDropDatabaseSqlAction( |
71
|
|
|
$dbConnectionSpec['dbname'], |
72
|
|
|
isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null, |
73
|
|
|
$ifExists |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|