1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Distilleries\Contentful\Commands\Sync\Traits; |
4
|
|
|
|
5
|
|
|
trait SyncTrait |
6
|
|
|
{ |
7
|
|
|
abstract public function warn($string, $verbosity = null); |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Switch to `sync` database. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
protected function switchToSyncDb() |
15
|
|
|
{ |
16
|
|
|
$this->warn('Switch to sync database'); |
17
|
|
|
|
18
|
|
|
config([ |
19
|
|
|
'database.default' => 'mysql_sync', |
20
|
|
|
]); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Dump `sync` database into a SQL file and return its path. |
25
|
|
|
* |
26
|
|
|
* @param boolean $isPreview |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
protected function dumpSync(bool $isPreview, string $connector = 'mysql'): string |
30
|
|
|
{ |
31
|
|
|
$path = storage_path('dumps/' . date('YmdHis') . '_sync' . ($isPreview ? '_preview' : '') . '.sql'); |
32
|
|
|
$this->warn('Dump "' . basename($path) . '"...'); |
33
|
|
|
|
34
|
|
|
$dirName = dirname($path); |
35
|
|
|
if (!is_dir($dirName)) { |
36
|
|
|
mkdir($dirName, 0777, true); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->dumpSql($path, $this->getConnector($isPreview, $connector)); |
40
|
|
|
|
41
|
|
|
return realpath($path); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Dump SQL database in given file path. |
46
|
|
|
* |
47
|
|
|
* @param string $path |
48
|
|
|
* @param string $connector |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
protected function dumpSql(string $path, string $connector) |
52
|
|
|
{ |
53
|
|
|
$exec = 'export MYSQL_PWD=\'%s\'; mysqldump --add-drop-table --default-character-set=%s %s -u %s -h %s --port %s > %s'; |
54
|
|
|
|
55
|
|
|
$command = sprintf($exec, |
56
|
|
|
addcslashes(config('database.connections.' . $connector . '.password'), "'"), |
57
|
|
|
config('database.connections.' . $connector . '.charset'), |
58
|
|
|
config('database.connections.' . $connector . '.database'), |
59
|
|
|
config('database.connections.' . $connector . '.username'), |
60
|
|
|
config('database.connections.' . $connector . '.host'), |
61
|
|
|
config('database.connections.' . $connector . '.port'), |
62
|
|
|
$path |
63
|
|
|
); |
64
|
|
|
exec($command); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getConnector(bool $isPreview, string $connector = 'mysql'): string |
68
|
|
|
{ |
69
|
|
|
$compiledConnector = $connector . ($isPreview ? '_preview' : ''); |
70
|
|
|
if (empty(config('database.connections.' . $compiledConnector . '.username'))) { |
71
|
|
|
$compiledConnector = $connector; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $compiledConnector; |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Put previous dump in live-preview database. |
81
|
|
|
* |
82
|
|
|
* @param string $path |
83
|
|
|
* @param boolean $isPreview |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
protected function putSync(string $path, bool $isPreview, string $connector = 'mysql') |
87
|
|
|
{ |
88
|
|
|
$compiledConnector = $this->getConnector($isPreview, $connector); |
89
|
|
|
|
90
|
|
|
config([ |
91
|
|
|
'database.default' => $compiledConnector, |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$this->warn('Put into "' . $compiledConnector . '" database...'); |
95
|
|
|
$this->putSql($path, $compiledConnector); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Put SQL file into given database. |
100
|
|
|
* |
101
|
|
|
* @param string $path |
102
|
|
|
* @param string $connector |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
protected function putSql(string $path, string $connector) |
106
|
|
|
{ |
107
|
|
|
$exec = 'export MYSQL_PWD=\'%s\'; mysql -u %s -h %s --port %s %s < %s'; |
108
|
|
|
|
109
|
|
|
$command = sprintf($exec, |
110
|
|
|
addcslashes(config('database.connections.' . $connector . '.password'), "'"), |
111
|
|
|
config('database.connections.' . $connector . '.username'), |
112
|
|
|
config('database.connections.' . $connector . '.host'), |
113
|
|
|
config('database.connections.' . $connector . '.port'), |
114
|
|
|
config('database.connections.' . $connector . '.database'), |
115
|
|
|
$path |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
exec($command); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|