gggeek /
db-3v4l
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | namespace Db3v4l\Command; |
||
| 4 | |||
| 5 | use Db3v4l\Core\DatabaseSchemaManager; |
||
|
0 ignored issues
–
show
The type
Db3v4l\Core\DatabaseSchemaManager was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 6 | |||
| 7 | abstract class DatabaseManagingCommand extends SQLExecutingCommand |
||
|
0 ignored issues
–
show
|
|||
| 8 | { |
||
| 9 | /** |
||
|
0 ignored issues
–
show
|
|||
| 10 | * @param string[][] $instanceList |
||
|
0 ignored issues
–
show
|
|||
| 11 | * @param array[] $dbSpecList key: db name (as used to identify configured databases), value: array('user': mandatory, 'dbname': mandatory, 'password': mandatory) |
||
|
0 ignored issues
–
show
|
|||
| 12 | * @return array 'succeeded': int, 'failed': int, 'results': same format as dbConfigurationManager::getInstanceConfiguration |
||
|
0 ignored issues
–
show
|
|||
| 13 | * @throws \Exception |
||
|
0 ignored issues
–
show
|
|||
| 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 */ |
||
|
0 ignored issues
–
show
|
|||
| 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) { |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 66 | * @param string[][] $instanceList |
||
|
0 ignored issues
–
show
|
|||
| 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) |
||
|
0 ignored issues
–
show
|
|||
| 68 | * @param bool $ifExists |
||
|
0 ignored issues
–
show
|
|||
| 69 | * @param string $executionStrategy |
||
|
0 ignored issues
–
show
|
|||
| 70 | * @return array 'succeeded': int, 'failed': int, 'results': string[] |
||
|
0 ignored issues
–
show
|
|||
| 71 | * @throws \Exception |
||
|
0 ignored issues
–
show
|
|||
| 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 */ |
||
|
0 ignored issues
–
show
|
|||
| 81 | return $schemaManager->getDropDatabaseSqlAction( |
||
| 82 | $dbConnectionSpec['dbname'], |
||
| 83 | isset($dbConnectionSpec['user']) ? $dbConnectionSpec['user'] : null, |
||
| 84 | $ifExists |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | } |
||
| 90 |