-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL.php
More file actions
70 lines (57 loc) · 1.93 KB
/
MySQL.php
File metadata and controls
70 lines (57 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
namespace CakeDumpSql\Sql;
use Cake\Database\Driver\Mysql as MysqlDriver;
use CakeDumpSql\Error\BinaryNotFoundException;
use Symfony\Component\Process\Process;
class MySQL extends SqlBase
{
protected string $command = 'mysqldump';
/**
* @param array<string, mixed> $config The config array from the connection object
* @param \Cake\Database\Driver\Mysql $driver The current mysql driver instance
*/
public function __construct(array $config, protected MysqlDriver $driver)
{
parent::__construct($config);
if ($driver->isMariadb()) {
$this->command = 'mariadb-dump';
}
}
/**
* @return string
* @throws \CakeDumpSql\Error\BinaryNotFoundException
*/
public function dump(): string
{
if (!$this->checkBinary($this->command)) {
throw new BinaryNotFoundException($this->command . ' was not found');
}
$command = [
$this->command,
'--user="' . ($this->config['username'] ?? '') . '"',
'--password="' . ($this->config['password'] ?? '') . '"',
'--default-character-set=' . ($this->config['encoding'] ?? 'utf8mb4'),
'--host=' . ($this->config['host'] ?? 'localhost'),
'--port=' . ($this->config['port'] ?? 3306),
'--databases',
$this->config['database'],
];
if ($this->driver->isMariadb()) {
$command[] = '--skip-create-options';
} else {
$command[] = '--no-create-db';
}
if ($this->isDataOnly()) {
$command[] = '--no-create-info';
}
$process = Process::fromShellCommandline(implode(' ', $command));
$process->run();
$output = $process->getOutput();
$error = $process->getErrorOutput();
if (!empty($error)) {
$this->io->warning($error);
}
return $output;
}
}