Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DatabaseService | |
0.00% |
0 / 38 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| index | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| dropOrCreate | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| sync | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 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 string $pgHost = '', |
| 13 | private string $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 | $total = preg_split('/\n/', $result->output()); |
| 29 | $total = array_filter($total); |
| 30 | |
| 31 | logs()->debug('Total de databases encontrados em prod: ' . count($total)); |
| 32 | |
| 33 | return $total; |
| 34 | } |
| 35 | |
| 36 | public function execute(string $database): string |
| 37 | { |
| 38 | logs()->debug("Inciando sincronização da base: {$database}"); |
| 39 | |
| 40 | if ($this->dropOrCreate($database)) { |
| 41 | $this->sync($database); |
| 42 | } |
| 43 | |
| 44 | return redirect()->back() |
| 45 | ->with([ |
| 46 | 'message' => "Datadase {$database} sincronizao com {$database}_dev com sucesso!", |
| 47 | ]); |
| 48 | } |
| 49 | |
| 50 | private function dropOrCreate(string $database): bool |
| 51 | { |
| 52 | Log::debug("Procurando se banco {$database}_dev já existe..."); |
| 53 | |
| 54 | $process = Process::pipe(function (Pipe $pipe) use ($database) { |
| 55 | $pipe->command("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} -l"); |
| 56 | $pipe->command("grep {$database}_dev"); |
| 57 | }); |
| 58 | |
| 59 | if ($process->successful()) { |
| 60 | logs()->debug('Database encontrado, removendo!: ' . $process->output()); |
| 61 | Process::run("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} -c \"DROP DATABASE {$database}_dev with (force)\""); |
| 62 | } |
| 63 | |
| 64 | logs()->debug('Criando Database: ' . $process->output()); |
| 65 | $result = Process::run("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} -c \"CREATE DATABASE {$database}_dev\""); |
| 66 | |
| 67 | return $result->successful(); |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | private function sync(string $database): void |
| 73 | { |
| 74 | $process = Process::pipe(function (Pipe $pipe) use ($database) { |
| 75 | $pipe->command("/usr/bin/pg_dump -h {$this->pgHost} -U {$this->pgUser} -v {$database}"); |
| 76 | $pipe->command("/usr/bin/psql -h {$this->pgHost} -U {$this->pgUser} {$database}_dev"); |
| 77 | }); |
| 78 | |
| 79 | $process->successful() |
| 80 | ? logs()->debug('Sincronização executada com sucesso') |
| 81 | : logs()->debug('Erro ao tentar sincronizar!'); |
| 82 | } |
| 83 | } |