|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/sprout. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/sprout/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/sprout |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\Sprout\Db\Mysql; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\ParallelProcess\Pool; |
|
17
|
|
|
use Graze\Sprout\Chop\TableChopperInterface; |
|
18
|
|
|
use Graze\Sprout\Config\ConnectionConfigInterface; |
|
19
|
|
|
use Symfony\Component\Process\Process; |
|
20
|
|
|
|
|
21
|
|
|
class MysqlTableChopper implements TableChopperInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var ConnectionConfigInterface */ |
|
24
|
|
|
private $connection; |
|
25
|
|
|
/** @var Pool */ |
|
26
|
|
|
private $pool; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* MysqlTableDumper constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Pool $pool |
|
32
|
|
|
* @param ConnectionConfigInterface $connection |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Pool $pool, ConnectionConfigInterface $connection) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->connection = $connection; |
|
37
|
|
|
$this->pool = $pool; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $schema |
|
42
|
|
|
* @param string $table |
|
43
|
|
|
*/ |
|
44
|
|
|
public function chop(string $schema, string $table) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$process = new Process(''); |
|
47
|
|
|
$process->setCommandLine( |
|
48
|
|
|
sprintf( |
|
49
|
|
|
'mysql -h%1$s -u%2$s -p%3$s --default-character-set=utf8 --execute=%5$s %4$s', |
|
50
|
|
|
escapeshellarg($this->connection->getHost()), |
|
51
|
|
|
escapeshellarg($this->connection->getUser()), |
|
52
|
|
|
escapeshellarg($this->connection->getPassword()), |
|
53
|
|
|
escapeshellarg($schema), |
|
54
|
|
|
escapeshellarg(sprintf('TRUNCATE `%s`', $table)) |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$this->pool->add($process, ['chop', 'schema' => $schema, 'table' => $table]); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|