File manager - Edit - /var/www/html/homologBancodetalentos/app/Console/Commands/TalentBankLegacyDryRunCommand.php
Back
<?php namespace App\Console\Commands; use App\Support\TalentBank\IntegrationLog; use App\Support\TalentBank\LegacyMigrationDryRunAnalyzer; use App\Support\TalentBank\LegacySqlDumpSession; use Illuminate\Console\Command; use Illuminate\Support\Facades\File; use Throwable; class TalentBankLegacyDryRunCommand extends Command { protected $signature = 'talent-bank:legacy-dry-run {--sql-dump= : Caminho do dump .sql (padrão: TALENT_BANK_LEGACY_SQL_DUMP)} {--legacy-connection= : Conexão legada (padrão: mysql_bt_legacy após import do dump)} {--target-connection= : Conexão alvo BT} {--keep-temp-db : Mantém o banco temporário após o dry-run} {--report= : Caminho do JSON de relatório} {--stdout : Imprime o JSON completo no stdout}'; protected $description = 'Simula migração legado→BT a partir de dump SQL ou conexão legada (somente leitura).'; public function handle(): int { $session = null; $legacyConn = (string) ($this->option('legacy-connection') ?: ''); try { [$legacyConn, $session] = $this->resolveLegacyConnection($legacyConn); } catch (Throwable $e) { $this->error($e->getMessage()); return self::FAILURE; } $targetConn = (string) ($this->option('target-connection') ?: config('talent_bank_legacy_import.target_connection')); $tables = config('talent_bank_legacy_import.tables', []); $this->info("Conexão legada: {$legacyConn}"); $this->info("Conexão alvo: {$targetConn}"); $analyzer = new LegacyMigrationDryRunAnalyzer($legacyConn, $targetConn, is_array($tables) ? $tables : []); $report = $analyzer->analyze(); IntegrationLog::info('talent_bank.legacy_migration', 'dry_run_completed', [ 'legacy_connection' => $legacyConn, 'target_connection' => $targetConn, 'error_count' => count($report['errors'] ?? []), 'used_dump' => $session !== null, ]); if (($report['errors'] ?? []) !== []) { foreach ($report['errors'] as $err) { $this->error($err); } if ($session !== null && ! $this->option('keep-temp-db')) { $session->cleanup(); } return self::FAILURE; } $reportPath = $this->resolveReportPath(); File::ensureDirectoryExists(dirname($reportPath)); File::put($reportPath, json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE)); $this->info('Relatório gravado em: '.$reportPath); $summary = $report['summary'] ?? []; $this->table(['Métrica', 'Valor'], collect($summary)->map(fn ($v, $k) => [$k, $v])->values()->all()); $totals = $report['conflicts']['totals'] ?? []; if ($totals !== []) { $this->newLine(); $this->warn('Totais de conflitos por tipo:'); $this->table(['Tipo', 'Quantidade'], collect($totals)->map(fn ($v, $k) => [$k, $v])->values()->all()); } foreach ($report['warnings'] ?? [] as $w) { $this->warn($w); } if ($this->option('stdout')) { $this->line(json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE)); } if ($session !== null && ! $this->option('keep-temp-db')) { $this->line('Removendo banco temporário do dump...'); $session->cleanup(); } elseif ($session !== null) { $this->warn('Banco temporário mantido (conexão '.LegacySqlDumpSession::LEGACY_CONNECTION.').'); } return self::SUCCESS; } /** * @return array{0: string, 1: LegacySqlDumpSession|null} */ private function resolveLegacyConnection(string $legacyConn): array { if ($legacyConn !== '') { return [$legacyConn, null]; } $dumpPath = $this->resolveDumpPathOrNull(); if ($dumpPath === null) { return [(string) config('talent_bank_legacy_import.connection', 'mysql_legacy'), null]; } $this->info('── Importando dump para dry-run ──'); $session = new LegacySqlDumpSession($dumpPath); $session->ensureImported(function (string $step, string $message): void { $this->line(" [{$step}] {$message}"); }); return [$session->legacyConnection(), $session]; } private function resolveDumpPathOrNull(): ?string { $dumpOpt = $this->option('sql-dump'); if (is_string($dumpOpt) && trim($dumpOpt) !== '') { return LegacySqlDumpSession::resolvePath(trim($dumpOpt)); } $cfg = (string) config('talent_bank_legacy_import.sql_dump', ''); if ($cfg === '') { return null; } try { $path = LegacySqlDumpSession::resolvePath($cfg); } catch (Throwable) { return null; } return file_exists($path) ? $path : null; } private function resolveReportPath(): string { $opt = $this->option('report'); if (! is_string($opt) || trim($opt) === '') { return storage_path('app/talent_bank_legacy_dry_run_'.now()->format('Y-m-d_His').'.json'); } $rp = trim($opt); $isAbsolute = str_starts_with($rp, '/') || preg_match('#^[A-Za-z]:[/\\\\]#', $rp); return $isAbsolute ? $rp : base_path(str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $rp)); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings