|
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
|
4 |
|
public function __construct(Pool $pool, ConnectionConfigInterface $connection) |
|
35
|
|
|
{ |
|
36
|
4 |
|
$this->connection = $connection; |
|
37
|
4 |
|
$this->pool = $pool; |
|
38
|
4 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $schema |
|
42
|
|
|
* @param string ...$tables |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function chop(string $schema, string ...$tables) |
|
45
|
|
|
{ |
|
46
|
2 |
|
$query = sprintf( |
|
47
|
2 |
|
'SET FOREIGN_KEY_CHECKS=0; %s; SET FOREIGN_KEY_CHECKS=1;', |
|
48
|
2 |
|
implode( |
|
49
|
2 |
|
'; ', |
|
50
|
2 |
|
array_map( |
|
51
|
2 |
|
function (string $table) { |
|
52
|
2 |
|
return "TRUNCATE `{$table}`"; |
|
53
|
2 |
|
}, |
|
54
|
2 |
|
$tables |
|
55
|
|
|
) |
|
56
|
|
|
) |
|
57
|
|
|
); |
|
58
|
2 |
|
$process = new Process(''); |
|
59
|
2 |
|
$process->setCommandLine( |
|
60
|
2 |
|
sprintf( |
|
61
|
2 |
|
'mysql -h%1$s -u%2$s -p%3$s --default-character-set=utf8 --execute=%5$s %4$s', |
|
62
|
2 |
|
escapeshellarg($this->connection->getHost()), |
|
63
|
2 |
|
escapeshellarg($this->connection->getUser()), |
|
64
|
2 |
|
escapeshellarg($this->connection->getPassword()), |
|
65
|
2 |
|
escapeshellarg($schema), |
|
66
|
2 |
|
escapeshellarg($query) |
|
67
|
|
|
) |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
2 |
|
$displayTables = (count($tables) < 3 ? implode(', ', $tables) : count($tables)); |
|
71
|
2 |
|
$this->pool->add($process, ['chop', 'schema' => $schema, 'tables' => $displayTables]); |
|
72
|
2 |
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|