1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\Dumper\Drivers; |
4
|
|
|
|
5
|
|
|
use CodexShaper\Dumper\Dumper; |
6
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
7
|
|
|
|
8
|
|
|
class PgsqlDumper extends Dumper |
9
|
|
|
{ |
10
|
|
|
/*@var int*/ |
11
|
|
|
protected $port = 5432; |
12
|
|
|
/*@var bool*/ |
13
|
|
|
protected $useInserts = false; |
14
|
|
|
/*@var bool*/ |
15
|
|
|
protected $createTables = true; |
16
|
|
|
|
17
|
2 |
|
public function useInserts() |
18
|
|
|
{ |
19
|
2 |
|
$this->useInserts = true; |
20
|
2 |
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
2 |
|
public function doNotCreateTables() |
24
|
|
|
{ |
25
|
2 |
|
$this->createTables = false; |
26
|
|
|
|
27
|
2 |
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function dump(string $destinationPath = "") |
31
|
|
|
{ |
32
|
|
|
$destinationPath = !empty($destinationPath) ? '"' . $destinationPath . '"' : '"' . $this->destinationPath . '"'; |
33
|
|
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
34
|
|
|
$this->command = $this->removeExtraSpaces($dumpCommand); |
35
|
|
|
$this->runCommand(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function restore(string $restorePath = "") |
39
|
|
|
{ |
40
|
|
|
$restorePath = !empty($restorePath) ? '"' . $restorePath . '"' : '"' . $this->restorePath . '"'; |
41
|
|
|
$restoreCommand = $this->prepareRestoreCommand($restorePath); |
42
|
|
|
$this->command = $this->removeExtraSpaces($restoreCommand); |
43
|
|
|
$this->runCommand(); |
44
|
|
|
} |
45
|
|
|
|
46
|
24 |
|
protected function prepareDumpCommand(string $destinationPath): string |
47
|
|
|
{ |
48
|
24 |
|
$dumpCommand = sprintf( |
49
|
24 |
|
'%s -U %s -h %s %s %s %s %s %s %s', |
50
|
24 |
|
$this->quoteCommand($this->commandBinaryPath . 'pg_dump'), |
51
|
24 |
|
$this->prepareUserName(), |
52
|
24 |
|
$this->prepareHost(), |
53
|
24 |
|
$this->preparePort(), |
54
|
24 |
|
$this->prepareUseInserts(), |
55
|
24 |
|
$this->prepareCreateTables(), |
56
|
24 |
|
$this->prepareIncludeTables(), |
57
|
24 |
|
$this->prepareIgnoreTables(), |
58
|
24 |
|
$this->prepareDatabase() |
59
|
|
|
); |
60
|
|
|
|
61
|
24 |
|
if ($this->isCompress) { |
62
|
2 |
|
$compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}"); |
63
|
2 |
|
return "{$dumpCommand} | {$compressCommand} > \"{$destinationPath}{$this->compressExtension}\""; |
64
|
|
|
} |
65
|
22 |
|
return "{$dumpCommand} > \"{$destinationPath}\""; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
protected function prepareRestoreCommand(string $filePath): string |
69
|
|
|
{ |
70
|
6 |
|
$restoreCommand = sprintf("%s -U %s -h %s %s %s", |
71
|
6 |
|
$this->quoteCommand($this->commandBinaryPath . 'psql'), |
72
|
6 |
|
$this->prepareUserName(), |
73
|
6 |
|
$this->prepareHost(), |
74
|
6 |
|
$this->preparePort(), |
75
|
6 |
|
$this->prepareDatabase() |
76
|
|
|
); |
77
|
|
|
|
78
|
6 |
|
if ($this->isCompress) { |
79
|
2 |
|
$compressCommand = $this->quoteCommand("{$this->compressBinaryPath}{$this->compressCommand}"); |
80
|
2 |
|
return "{$compressCommand} < \"{$filePath}\" | {$restoreCommand}"; |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
return "{$restoreCommand} < \"{$filePath}\""; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function runCommand() |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
|
90
|
|
|
$credentials = $this->host . ':' . $this->port . ':' . $this->dbName . ':' . $this->username . ':' . $this->password; |
91
|
|
|
$this->tempFile = tempnam(sys_get_temp_dir(), 'pgsqlpass'); |
|
|
|
|
92
|
|
|
$handler = fopen($this->tempFile, 'r+'); |
93
|
|
|
fwrite($handler, $credentials); |
|
|
|
|
94
|
|
|
$env = ['PGPASSFILE' => $this->tempFile]; |
95
|
|
|
$process = $this->prepareProcessCommand($this->command); |
|
|
|
|
96
|
|
|
if ($this->debug) { |
97
|
|
|
$process->mustRun(null, $env); |
98
|
|
|
} else { |
99
|
|
|
$process->run(null, $env); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
fclose($handler); |
|
|
|
|
103
|
|
|
unlink($this->tempFile); |
104
|
|
|
|
105
|
|
|
} catch (ProcessFailedException $e) { |
106
|
|
|
throw new \Exception($e->getMessage()); |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
24 |
|
public function prepareCreateTables() |
112
|
|
|
{ |
113
|
24 |
|
return (!$this->createTables) ? '--data-only' : ''; |
114
|
|
|
} |
115
|
|
|
|
116
|
24 |
|
public function prepareUseInserts() |
117
|
|
|
{ |
118
|
24 |
|
return ($this->useInserts) ? '--inserts' : ''; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|