First Commit
This commit is contained in:
+130
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class DbSyncSisTablesCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'db:sync-sis-tables';
|
||||
protected static $defaultDescription = 'Substitui as tabelas sis_*** no evoluterSys2.sql pelas tabelas de syscms.sql';
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$targetFile = 'evoluterSys2.sql';
|
||||
$sourceFile = 'syscms.sql';
|
||||
|
||||
if (!file_exists($targetFile)) {
|
||||
$output->writeln("<error>Arquivo de destino não encontrado: $targetFile</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
if (!file_exists($sourceFile)) {
|
||||
$output->writeln("<error>Arquivo de origem não encontrado: $sourceFile</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$output->writeln("<info>Iniciando a extração das tabelas sis_*** de $sourceFile...</info>");
|
||||
$sisTablesDefinitions = $this->extractSisTables($sourceFile);
|
||||
|
||||
if (empty($sisTablesDefinitions)) {
|
||||
$output->writeln("<error>Nenhuma tabela sis_*** encontrada em $sourceFile.</error>");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$output->writeln("<info>Encontradas " . count($sisTablesDefinitions) . " tabelas sis_***. Substituindo em $targetFile...</info>");
|
||||
$this->replaceSisTables($targetFile, $sisTablesDefinitions, $output);
|
||||
|
||||
$output->writeln("<info>Operação concluída com sucesso!</info>");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function extractSisTables(string $filePath): array
|
||||
{
|
||||
$handle = fopen($filePath, 'r');
|
||||
if (!$handle) return [];
|
||||
|
||||
$tables = [];
|
||||
$currentTable = null;
|
||||
$capture = false;
|
||||
$buffer = "";
|
||||
|
||||
while (($line = fgets($handle)) !== false) {
|
||||
// Detecta início de um CREATE TABLE sis_***
|
||||
if (preg_match("/CREATE TABLE `?(sis_[^`\s(]+)`?/i", $line, $matches)) {
|
||||
$currentTable = $matches[1];
|
||||
$capture = true;
|
||||
$buffer = $line;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($capture) {
|
||||
$buffer .= $line;
|
||||
// Detecta fim do CREATE TABLE (assume-se que termina com ); ou ); seguido de comentários/inserts se for dump compactado,
|
||||
// mas para simplificar, buscamos o padrão );
|
||||
if (preg_match("/\);/", $line)) {
|
||||
$capture = false;
|
||||
$tables[$currentTable] = $buffer;
|
||||
$buffer = "";
|
||||
$currentTable = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
return $tables;
|
||||
}
|
||||
|
||||
private function replaceSisTables(string $filePath, array $definitions, OutputInterface $output): void
|
||||
{
|
||||
$tempFile = $filePath . '.tmp';
|
||||
$inHandle = fopen($filePath, 'r');
|
||||
$outHandle = fopen($tempFile, 'w');
|
||||
|
||||
$skip = false;
|
||||
$replacedCount = 0;
|
||||
|
||||
while (($line = fgets($inHandle)) !== false) {
|
||||
// Verifica se a linha inicia uma tabela que queremos substituir
|
||||
if (preg_match("/CREATE TABLE `?(sis_[^`\s(]+)`?/i", $line, $matches)) {
|
||||
$tableName = $matches[1];
|
||||
if (isset($definitions[$tableName])) {
|
||||
$output->writeln("Substituindo tabela: <comment>$tableName</comment>");
|
||||
fwrite($outHandle, $definitions[$tableName]);
|
||||
$skip = true;
|
||||
$replacedCount++;
|
||||
unset($definitions[$tableName]); // Marca como usada
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Se estiver pulando, ignora as linhas até o fim da definição da tabela
|
||||
if ($skip) {
|
||||
if (preg_match("/\);/", $line)) {
|
||||
$skip = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
fwrite($outHandle, $line);
|
||||
}
|
||||
|
||||
// Se sobraram tabelas no source que não existiam no target, podemos adicioná-las ao final ou apenas informar
|
||||
if (!empty($definitions)) {
|
||||
$output->writeln("<info>Adicionando novas tabelas sis_*** que não existiam no destino...</info>");
|
||||
foreach ($definitions as $tableName => $def) {
|
||||
$output->writeln("Adicionando: <comment>$tableName</comment>");
|
||||
fwrite($outHandle, "\n" . $def . "\n");
|
||||
$replacedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($inHandle);
|
||||
fclose($outHandle);
|
||||
|
||||
rename($tempFile, $filePath);
|
||||
$output->writeln("<info>Total de tabelas processadas: $replacedCount</info>");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user