1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use Illuminate\Process\Pipe; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Illuminate\Support\Facades\Process; |
8
|
|
|
|
9
|
|
|
class DatabaseService |
10
|
|
|
{ |
11
|
|
|
public function __construct( |
12
|
|
|
private $pgHost = '', |
13
|
|
|
private $pgUser = '' |
14
|
|
|
) { |
15
|
|
|
$this->pgHost = env('PGHOST'); |
16
|
|
|
$this->pgUser = env('PGUSERNAME'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function index(): array |
20
|
|
|
{ |
21
|
|
|
logs()->debug('Iniciando busca por databases...'); |
22
|
|
|
$result = Process::pipe(function (Pipe $pipe) { |
23
|
|
|
$pipe->command("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} -l"); |
24
|
|
|
$pipe->command("awk '{print $1}'"); |
25
|
|
|
$pipe->command("egrep -v 'List|Name|--|\||\(|dev|hml|__|_2'"); |
26
|
|
|
}); |
27
|
|
|
|
28
|
|
|
if ($result->successful()) { |
29
|
|
|
$total = preg_split('/\n/', $result->output()); |
30
|
|
|
$total = array_filter($total); |
31
|
|
|
|
32
|
|
|
logs()->debug('Total de databases encontrados em ' . $this->pgHost . ': ' . count($total) . ' com o usuário: ' . $this->pgUser); |
33
|
|
|
|
34
|
|
|
return $total; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
logs()->debug($result->errorOutput()); |
38
|
|
|
return []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function execute(string $database): string |
42
|
|
|
{ |
43
|
|
|
logs()->debug("Inciando sincronização da base: {$database}"); |
44
|
|
|
|
45
|
|
|
if ($this->dropOrCreate($database)) { |
46
|
|
|
$this->sync($database); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return redirect()->back() |
50
|
|
|
->with([ |
51
|
|
|
'message' => "Datadase {$database} sincronizao com {$database}_dev com sucesso!", |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function dropOrCreate(string $database): bool |
56
|
|
|
{ |
57
|
|
|
Log::debug("Procurando se banco {$database}_dev já existe..."); |
58
|
|
|
|
59
|
|
|
$process = Process::pipe(function (Pipe $pipe) use ($database) { |
60
|
|
|
$pipe->command("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} -l"); |
61
|
|
|
$pipe->command("grep {$database}_dev"); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
if ($process->successful()) { |
65
|
|
|
logs()->debug('Database encontrado, removendo!: ' . $process->output()); |
66
|
|
|
Process::run("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} -c \"DROP DATABASE {$database}_dev with (force)\""); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
logs()->debug('Criando Database: ' . $process->output()); |
70
|
|
|
$result = Process::run("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} -c \"CREATE DATABASE {$database}_dev\""); |
71
|
|
|
|
72
|
|
|
return $result->successful(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function sync(string $database): void |
76
|
|
|
{ |
77
|
|
|
$process = Process::pipe(function (Pipe $pipe) use ($database) { |
78
|
|
|
$pipe->command("/usr/bin/pg_dump -h {$this->pgHost} -U {$this->pgUser} -v {$database}"); |
79
|
|
|
$pipe->command("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} {$database}_dev"); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
$process->successful() |
83
|
|
|
? logs()->debug('Sincronização executada com sucesso') |
84
|
|
|
: logs()->debug('Erro ao tentar sincronizar!'); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|