Issues (1426)

app/src/Command/SqlExecute.php (77 issues)

1
<?php
2
0 ignored issues
show
Missing file doc comment
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Db3v4l\Core\SqlAction\Command;
7
use Db3v4l\Core\SqlAction\File;
8
use Db3v4l\Core\SqlExecutor\Forked\NativeClient;
9
use Db3v4l\Core\SqlExecutor\Forked\TimedExecutor;
10
use Symfony\Component\Console\Input\InputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputInterface 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\Console\Output\OutputInterface;
0 ignored issues
show
The type Symfony\Component\Console\Output\OutputInterface 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\Console\Input\InputOption;
0 ignored issues
show
The type Symfony\Component\Console\Input\InputOption 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Db3v4l\Util\Process;
14
15
class SqlExecute extends DatabaseManagingCommand
0 ignored issues
show
Missing doc comment for class SqlExecute
Loading history...
16
{
17
    protected static $defaultName = 'db3v4l:sql:execute';
18
19
    protected function configure()
0 ignored issues
show
Missing doc comment for function configure()
Loading history...
20
    {
21
        $this
22
            ->setDescription('Executes an SQL command in parallel on all configured database instances, by default in a dedicated temporary database/user')
23
            ->addOption('sql', 's', InputOption::VALUE_REQUIRED, 'The sql command(s) string to execute')
24
            ->addOption('file', null, InputOption::VALUE_REQUIRED, "A file with sql commands to execute. The tokens '{dbtype}' and '{instancename}' will be replaced with actual values")
25
26
            ->addOption('database', 'd', InputOption::VALUE_REQUIRED, 'The name of an existing database to use. If omitted, a dedicated temporary database will be created on the fly and disposed after use')
27
            ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'The name of the user to use for connecting to the existing database. Temporary databases get created with a temp user and random password')
28
            ->addOption('password', 'p', InputOption::VALUE_REQUIRED, 'The user password')
29
            ->addOption('charset', 'c', InputOption::VALUE_REQUIRED, 'The collation/character-set to use, _only_ for the dedicated temporary database. If omitted, the default collation for the instance will be used')
30
31
            ->addCommonOptions()
0 ignored issues
show
Space after closing parenthesis of function call prohibited
Loading history...
32
        ;
33
    }
34
35
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
36
     * @param InputInterface $input
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
37
     * @param OutputInterface $output
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
38
     * @return int
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
39
     * @throws \Exception
0 ignored issues
show
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $start = microtime(true);
44
45
        // as per https://www.php.net/manual/en/function.ignore-user-abort.php: for cli scripts, it is probably a good idea
46
        // to use ignore_user_abort
47
        ignore_user_abort(true);
48
49
        $this->setOutput($output);
50
        $this->setVerbosity($output->getVerbosity());
51
52
        $instanceList = $this->parseCommonOptions($input);
53
54
        $sql = $input->getOption('sql');
55
        $file = $input->getOption('file');
56
        $dbName = $input->getOption('database');
57
        $userName = $input->getOption('user');
58
        $password = $input->getOption('password');
59
60
        if ($sql == null && $file == null) {
61
            throw new \Exception("Please provide an sql command or file to be executed");
62
        }
63
        if ($sql != null && $file != null) {
64
            throw new \Exception("Please provide either an sql command or file to be executed, not both");
65
        }
66
67
        if (($dbName == null) xor ($userName == null)) {
68
69
            /// @todo is it possible to let the user log in to the 'root' db, regardless of db type ?
70
71
            throw new \Exception("Please provide both a custom database name and associated user account");
72
        }
73
74
        $createDB = ($dbName == null);
75
76
        if ($createDB) {
77
            // create temp databases
78
79
            if ($this->outputFormat === 'text') {
80
                $this->writeln('<info>Creating temporary databases...</info>', OutputInterface::VERBOSITY_VERBOSE);
81
            }
82
83
            /// @todo inject more randomness in the username, by allowing more chars than bin2hex produces
84
85
            $userName = 'db3v4l_' . substr(bin2hex(random_bytes(5)), 0, 9); // some mysql versions have a limitation of 16 chars for usernames
86
            $password = bin2hex(random_bytes(15)); // some oracle versions have a limit of 30 chars on passwords
87
            //$dbName = bin2hex(random_bytes(31));
88
            $dbName = $userName; // $userName will be used as db name
89
90
            $tempDbSpecs = [];
91
            foreach($instanceList as $instanceName => $instanceSpecs) {
0 ignored issues
show
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
92
                $tempDbSpecs[$instanceName] = [
93
                    'dbname' => $dbName,
94
                    'user' => $userName,
95
                    'password' => $password
96
                ];
97
            }
98
99
            if (($charset = $input->getOption('charset')) != '') {
100
                foreach($instanceList as $instanceName) {
0 ignored issues
show
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
101
                    $tempDbSpecs[$instanceName]['charset'] = $charset;
102
                }
103
            }
104
105
            // for now, we always use NativeClient for creating/dropping temp dbs
106
            $previousStrategy = $this->executionStrategy;
107
            $this->executionStrategy = NativeClient::EXECUTION_STRATEGY;
108
            $creationResults = $this->createDatabases($instanceList, $tempDbSpecs);
109
            $this->executionStrategy = $previousStrategy;
110
111
            $dbConnectionSpecs = $creationResults['data'];
112
113
        } else {
114
            // use existing databases
115
116
            if ($this->outputFormat === 'text') {
117
                $this->writeln('<info>Using existing databases...</info>', OutputInterface::VERBOSITY_VERBOSE);
118
            }
119
120
            $dbConfig = [
121
                'user' => $userName,
122
                'password' => $password,
123
                'dbname' => $dbName,
124
            ];
125
126
            $dbConnectionSpecs = [];
127
            foreach($instanceList as $instanceName => $instanceSpecs) {
0 ignored issues
show
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
128
                $dbConnectionSpecs[$instanceName] = array_merge($instanceSpecs, $dbConfig);
129
            }
130
        }
131
132
        if (count($dbConnectionSpecs)) {
133
134
            if ($this->outputFormat === 'text') {
135
                $this->writeln('<info>Executing sql command/file...</info>', OutputInterface::VERBOSITY_VERBOSE);
136
            }
137
138
            $results = $this->executeSQL($dbConnectionSpecs, $sql, $file);
139
140
            if ($this->outputFormat === 'text') {
141
                $this->writeln('<info>Dropping temporary databases...</info>', OutputInterface::VERBOSITY_VERBOSE);
142
            }
143
144
            if ($createDB) {
145
                $results['failed'] += $creationResults['failed'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $creationResults does not seem to be defined for all execution paths leading up to this point.
Loading history...
146
147
                foreach($instanceList as $instanceName  => $instanceSpecs) {
0 ignored issues
show
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
148
                    if (!isset($dbConnectionSpecs[$instanceName])) {
149
                        /// @todo retrieve the actual temp db creation error instead of doing this...
150
                        $results['data'][$instanceName] = [
151
                            'exitcode' => '1',
152
                            'stderr' => 'Error in creation of temporary database'
153
                        ];
154
                        unset($instanceList[$instanceName]);
155
                    }
156
                }
157
                // for now, we always use NativeClient for creating/dropping temp dbs
158
                $previousStrategy = $this->executionStrategy;
159
                $this->executionStrategy = NativeClient::EXECUTION_STRATEGY;
160
                $this->dropDatabases($instanceList, $dbConnectionSpecs, true);
161
                $this->executionStrategy = $previousStrategy;
162
            }
163
        } else {
164
            $results = ['succeeded' => 0,  'failed' => 0, 'data' => null];
165
        }
166
167
        $time = microtime(true) - $start;
168
169
        $this->writeResults($results, $time);
170
171
        return (int)$results['failed'];
172
    }
173
174
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
175
     * @param array $dbConnectionSpecs
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
176
     * @param string $sql
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
177
     * @param string $filename
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
178
     * @param string $format
0 ignored issues
show
Missing parameter comment
Loading history...
Superfluous parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
179
     * @param int $maxParallel
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 4 spaces after parameter type; 1 found
Loading history...
Superfluous parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
180
     * @param int $timeout
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 4 spaces after parameter type; 1 found
Loading history...
Superfluous parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
181
     * @return array
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
182
     * @throws \Exception
0 ignored issues
show
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
183
     */
184
    protected function executeSQL(array $dbConnectionSpecs, $sql, $filename = null)
185
    {
186
        return $this->executeSqlAction(
187
            $dbConnectionSpecs,
188
            'Execution of SQL',
189
            function ($schemaManager, $instanceName) use ($sql, $filename) {
190
                if ($sql != null) {
191
                    return new Command(
192
                        $sql,
193
                        function ($output, $executor) {
194
                            /** @var TimedExecutor $executor */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
195
                            return array_merge(['stdout' => $output], $executor->getTimingData());
196
                        }
197
                    );
198
                } else {
199
                    /** @var DatabaseSchemaManager $schemaManager */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
200
                    $realFileName = $this->replaceDBSpecTokens($filename, $instanceName, $schemaManager->getDatabaseConfiguration());
201
                    if (!is_file($realFileName)) {
202
                        throw new \RuntimeException("Can not find sql file for execution: '$realFileName'");
203
                    }
204
                    return new File(
205
                        $realFileName,
206
                        function ($output, $executor) {
207
                            /** @var TimedExecutor $executor */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
208
                            return array_merge(['stdout' => $output], $executor->getTimingData());
209
                        }
210
                    );
211
                }
212
            },
213
            true,
214
            array($this, 'onSubProcessOutput')
215
        );
216
    }
217
218
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
Parameter $processIndex should have a doc-comment as per coding-style.
Loading history...
219
     * @param string $type
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
220
     * @param string $buffer
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
221
     * @param string $processIndex;
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 2 spaces after parameter type; 1 found
Loading history...
Doc comment for parameter $processIndex; does not match actual variable name $processIndex
Loading history...
222
     * @param Process $process
0 ignored issues
show
Missing parameter comment
Loading history...
223
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
224
    public function onSubProcessOutput($type, $buffer, $processIndex, $process = null)
225
    {
226
        if (is_object($processIndex) && $process === null) {
227
            /// @todo php bug ? investigate deeper...
228
            $process = $processIndex;
229
            $processIndex = '?';
230
        }
231
232
        $pid = is_object($process) ? $process->getPid() : '';
233
234
        $lines = explode("\n", trim($buffer));
235
236
        foreach ($lines as $line) {
237
            // we tag the output from the different processes
238
            if (trim($line) !== '') {
239
                if ($type === 'err') {
240
                    $this->writeErrorln(
241
                        '[' . $processIndex . '][' . $pid . '] ' . trim($line),
242
                        OutputInterface::VERBOSITY_VERBOSE,
243
                        OutputInterface::OUTPUT_RAW
244
                    );
245
                } else {
246
                    $this->writeln(
247
                        '[' . $processIndex . '][' . $pid . '] ' . trim($line),
248
                        OutputInterface::VERBOSITY_VERBOSE,
249
                        OutputInterface::OUTPUT_RAW
250
                    );
251
                }
252
            }
253
        }
254
    }
255
256
    /**
257
     * Replaces tokens
258
     * @param string $string the original string
0 ignored issues
show
There must be exactly one blank line before the tags in a doc comment
Loading history...
Expected 3 spaces after parameter type; 1 found
Loading history...
Expected 11 spaces after parameter name; 1 found
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
259
     * @param string $instanceName
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 3 spaces after parameter type; 1 found
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
260
     * @param string[] $dbConnectionSpec
0 ignored issues
show
Missing parameter comment
Loading history...
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
261
     * @return string
0 ignored issues
show
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
262
     * @todo add support for more tokens, eg '{version}'
0 ignored issues
show
Tag @todo cannot be grouped with parameter tags in a doc comment
Loading history...
Tag value for @todo tag indented incorrectly; expected 3 spaces but found 1
Loading history...
263
     */
264
    protected function replaceDBSpecTokens($string, $instanceName, $dbConnectionSpec)
265
    {
266
        $dbType = $dbConnectionSpec['vendor'];
267
        return str_replace(
268
            array('{dbtype}', '{instancename}', '{vendor}'),
269
            array($dbType, $instanceName, $dbConnectionSpec['vendor']),
270
            $string
271
        );
272
    }
273
274
    protected function hasDBSpecTokens($string)
0 ignored issues
show
Missing doc comment for function hasDBSpecTokens()
Loading history...
275
    {
276
        return $string != str_replace(
277
            array('{dbtype}', '{instancename}', '{vendor}'),
278
            '',
279
            $string
280
        );
281
    }
282
283
    protected function writeResultsToFile(array $results)
0 ignored issues
show
Missing doc comment for function writeResultsToFile()
Loading history...
284
    {
285
        if (!$this->hasDBSpecTokens($this->outputFile)) {
286
            parent::writeResultsToFile($results);
287
            return;
288
        }
289
290
        foreach($results['data'] as $instanceName => $data) {
0 ignored issues
show
Expected "foreach (...) {\n"; found "foreach(...) {\n"
Loading history...
291
            $formattedData = $this->formatResults(array('data' => $data));
292
            $outputFile = $this->replaceDBSpecTokens($this->outputFile, $instanceName, $this->dbConfigurationManager->getInstanceConfiguration($instanceName));
293
            file_put_contents($outputFile, $formattedData);
294
        }
295
    }
296
}
297