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
|
|
|
protected $useInserts = false; |
11
|
|
|
protected $createTables = true; |
12
|
|
|
|
13
|
|
|
public function useInserts() |
14
|
|
|
{ |
15
|
|
|
$this->useInserts = true; |
16
|
|
|
return $this; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function doNotUseCreateTables() |
20
|
|
|
{ |
21
|
|
|
$this->createTables = false; |
22
|
|
|
|
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function dump(string $destinationPath = "") |
27
|
|
|
{ |
28
|
|
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
29
|
|
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
30
|
|
|
$this->command = $this->removeExtraSpaces($dumpCommand); |
31
|
|
|
$this->runCommand(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function restore(string $restorePath = "") |
35
|
|
|
{ |
36
|
|
|
$restorePath = !empty($restorePath) ? $restorePath : $this->restorePath; |
37
|
|
|
$restoreCommand = $this->prepareRestoreCommand($restorePath); |
38
|
|
|
$this->command = $this->removeExtraSpaces($restoreCommand); |
39
|
|
|
$this->runCommand(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function prepareDumpCommand(string $destinationPath): string |
43
|
|
|
{ |
44
|
|
|
$dumpCommand = sprintf( |
45
|
|
|
'%spg_dump -U %s -h %s %s %s %s %s %s %s', |
46
|
|
|
$this->dumpCommandPath, |
47
|
|
|
$this->prepareUsername(), |
48
|
|
|
$this->prepareHost(), |
49
|
|
|
$this->preparePort(), |
50
|
|
|
$this->prepareUseInserts(), |
51
|
|
|
$this->prepareCreateTables(), |
52
|
|
|
$this->prepareIncludeTables(), |
53
|
|
|
$this->prepareIgnoreTables(), |
54
|
|
|
$this->prepareDatabase() |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
if ($this->isCompress) { |
58
|
|
|
return "{$dumpCommand} | {$this->compressBinaryPath}{$this->compressCommand} > {$destinationPath}{$this->compressExtension}"; |
59
|
|
|
} |
60
|
|
|
return "{$dumpCommand} > {$destinationPath}"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function prepareRestoreCommand(string $filePath): string |
64
|
|
|
{ |
65
|
|
|
$restoreCommand = sprintf("%spsql -U %s -h %s %s %s", |
66
|
|
|
$this->dumpCommandPath, |
67
|
|
|
$this->prepareUsername(), |
68
|
|
|
$this->prepareHost(), |
69
|
|
|
$this->preparePort(), |
70
|
|
|
$this->prepareDatabase() |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
if ($this->isCompress) { |
74
|
|
|
return "{$this->compressBinaryPath}{$this->compressCommand} < {$filePath} | {$restoreCommand}"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return "{$restoreCommand} < {$filePath}"; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function runCommand() |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
|
84
|
|
|
$credentials = $this->host . ':' . $this->port . ':' . $this->dbName . ':' . $this->username . ':' . $this->password; |
85
|
|
|
$this->tempFile = tempnam(sys_get_temp_dir(), 'pgsqlpass'); |
86
|
|
|
$handler = fopen($this->tempFile, 'r+'); |
87
|
|
|
fwrite($handler, $credentials); |
|
|
|
|
88
|
|
|
$env = ['PGPASSFILE' => $this->tempFile]; |
89
|
|
|
$process = $this->prepareProcessCommand($this->command); |
|
|
|
|
90
|
|
|
if ($this->debug) { |
91
|
|
|
$process->mustRun(null, $env); |
92
|
|
|
} else { |
93
|
|
|
$process->run(null, $env); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
fclose($handler); |
|
|
|
|
97
|
|
|
unlink($this->tempFile); |
98
|
|
|
|
99
|
|
|
} catch (ProcessFailedException $e) { |
100
|
|
|
throw new \Exception($e->getMessage()); |
101
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function prepareDatabase() |
106
|
|
|
{ |
107
|
|
|
return !empty($this->dbName) ? $this->dbName : ""; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function prepareUsername() |
111
|
|
|
{ |
112
|
|
|
return !empty($this->username) ? $this->username : ""; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function prepareHost() |
116
|
|
|
{ |
117
|
|
|
return ($this->socket !== '') ? $this->socket : $this->host; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function preparePort() |
121
|
|
|
{ |
122
|
|
|
return !empty($this->port) ? '-p ' . $this->port : ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function prepareIncludeTables() |
126
|
|
|
{ |
127
|
|
|
return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : ""; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function prepareIgnoreTables() |
131
|
|
|
{ |
132
|
|
|
return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : ''; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function prepareCreateTables() |
136
|
|
|
{ |
137
|
|
|
return (!$this->createTables) ? '--data-only' : ''; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function prepareUseInserts() |
141
|
|
|
{ |
142
|
|
|
return (!$this->useInserts) ? '--inserts' : ''; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|