Mejorado comando ImportLegacy y creada ruta api para invocarlo desde la consola web, controla errores e informa de cambios a través de mercure
testing/ogcore-api/pipeline/head There was a failure building this commit Details
testing/ogcore-api/pipeline/pr-main Build queued... Details

pull/111/head
Juan Manuel Bardallo 2026-05-08 15:36:57 +02:00
parent 7baacb820f
commit a0c871add5
3 changed files with 312 additions and 15 deletions

View File

@ -1,5 +1,9 @@
# Changelog
## [1.7.7] - 2026-05-08
### Added
- Mejorado comando ImportLegacy y creada ruta api para invocarlo desde la consola web, controla errores e informa de cambios a través de mercure
## [1.7.6] - 2026-05-08
### Added
- Añadido comando ImportLegacy para importar una base de datos de OG 1.1 a OG 3.0, hace llamadas al resto de comandos Migrate ya creados. Acepta un fichero dump.sql o los parametros de conexión a la base de datos OG 1.1

View File

@ -15,6 +15,8 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Console\Input\ArrayInput;
@ -26,7 +28,8 @@ class ImportLegacyDbCommand extends Command
{
public function __construct(
private EntityManagerInterface $entityManager,
private UserPasswordHasherInterface $passwordHasher
private UserPasswordHasherInterface $passwordHasher,
private ?HubInterface $hub = null
) {
parent::__construct();
}
@ -39,7 +42,8 @@ class ImportLegacyDbCommand extends Command
->addOption('legacy-user', null, InputOption::VALUE_OPTIONAL, 'Legacy database user')
->addOption('legacy-password', null, InputOption::VALUE_OPTIONAL, 'Legacy database password')
->addOption('legacy-db', null, InputOption::VALUE_OPTIONAL, 'Legacy database name')
;
->addOption('cleanup-file', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Files to clean up after execution')
->addOption('log-file', null, InputOption::VALUE_OPTIONAL, 'Log file to read and send via Mercure');
}
protected function execute(InputInterface $input, OutputInterface $output): int
@ -77,10 +81,16 @@ class ImportLegacyDbCommand extends Command
$passwordArg = $dbpass ? "-p$dbpass" : "";
if ($file) {
$command = "mysql -h $host -u $dbuser $passwordArg $dbname < " . escapeshellarg($file);
$command = "mysql -h " . escapeshellarg($host) . " -u " . escapeshellarg($dbuser) . " $passwordArg " . escapeshellarg($dbname) . " < " . escapeshellarg($file);
exec($command . ' 2>&1', $outputLines, $resultCode);
if ($resultCode !== 0) {
$errorMsg = "Failed to import SQL dump.\n" . implode("\n", $outputLines);
$io->error($errorMsg);
throw new \Exception($errorMsg);
}
} else {
$legacyPassArg = $legacyPass ? "-p$legacyPass" : "";
// We only dump the strictly necessary tables to avoid giant dumps and potential collisions
$legacyPassArg = $legacyPass ? "-p" . escapeshellarg($legacyPass) : "";
$tablesToDump = [
'centros', 'aulas', 'grupos', 'gruposordenadores',
'nombresos',
@ -93,14 +103,39 @@ class ImportLegacyDbCommand extends Command
$tablesArg = implode(' ', $tablesToDump);
$io->text("Fetching tables from remote database $legacyHost...");
$command = "mysqldump -h $legacyHost -u $legacyUser $legacyPassArg $legacyDb $tablesArg | mysql -h $host -u $dbuser $passwordArg $dbname";
}
exec($command . ' 2>&1', $outputLines, $resultCode);
if ($resultCode !== 0) {
$io->error("Failed to import SQL dump.\n" . implode("\n", $outputLines));
return Command::FAILURE;
$tempDump = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('legacy_dump_', true) . '.sql';
// Execute dump
// Using --no-tablespaces if supported, but if it fails we will see the error now
$dumpCmd = "mysqldump --no-tablespaces -h " . escapeshellarg($legacyHost) . " -u " . escapeshellarg($legacyUser) . " $legacyPassArg " . escapeshellarg($legacyDb) . " $tablesArg > " . escapeshellarg($tempDump);
exec($dumpCmd . ' 2>&1', $dumpOutput, $dumpResult);
// If mysqldump fails due to --no-tablespaces, fallback without it
if ($dumpResult !== 0 && stripos(implode(" ", $dumpOutput), 'unknown option') !== false) {
$dumpCmd = "mysqldump -h " . escapeshellarg($legacyHost) . " -u " . escapeshellarg($legacyUser) . " $legacyPassArg " . escapeshellarg($legacyDb) . " $tablesArg > " . escapeshellarg($tempDump);
$dumpOutput = [];
exec($dumpCmd . ' 2>&1', $dumpOutput, $dumpResult);
}
if (!file_exists($tempDump) || filesize($tempDump) < 100) {
$errorMsg = "Failed to dump remote database. Dump is empty or failed.\n" . implode("\n", $dumpOutput);
$io->error($errorMsg);
if (file_exists($tempDump)) unlink($tempDump);
throw new \Exception($errorMsg);
}
// Execute import
$importCmd = "mysql -h " . escapeshellarg($host) . " -u " . escapeshellarg($dbuser) . " $passwordArg " . escapeshellarg($dbname) . " < " . escapeshellarg($tempDump);
exec($importCmd . ' 2>&1', $importOutput, $importResult);
if (file_exists($tempDump)) unlink($tempDump);
if ($importResult !== 0) {
$errorMsg = "Failed to import remote database dump.\n" . implode("\n", $importOutput);
$io->error($errorMsg);
throw new \Exception($errorMsg);
}
}
// Get new tables
@ -112,6 +147,7 @@ class ImportLegacyDbCommand extends Command
} else {
$io->text("Detected " . count($injectedTables) . " injected tables.");
}
$this->dispatchProgress($input);
// Define commands and their mandatory table dependencies
$commands = [
@ -157,19 +193,28 @@ class ImportLegacyDbCommand extends Command
}
try {
$commandLine = "php bin/console $cmdName 2>&1";
passthru($commandLine, $returnCode);
$consolePath = dirname(__DIR__, 3) . '/bin/console';
$commandLine = "php " . escapeshellarg($consolePath) . " $cmdName 2>&1";
$cmdOutput = [];
exec($commandLine, $cmdOutput, $returnCode);
foreach ($cmdOutput as $line) {
$io->writeln(' ' . $line);
}
if ($returnCode !== 0) {
$io->error("Command $cmdName failed with code $returnCode.");
}
$this->dispatchProgress($input);
} catch (\Exception $e) {
$io->error("Exception during $cmdName: " . $e->getMessage());
$this->dispatchProgress($input);
}
}
// Migrate Users manually since there is no official command for it
$this->importUsers($connection, $io);
$this->dispatchProgress($input);
} finally {
// Cleanup ALWAYS runs
@ -185,14 +230,64 @@ class ImportLegacyDbCommand extends Command
}
$io->success('Migration orchestrator finished successfully!');
$this->finalize(true, "Migration completed successfully.", $input);
return Command::SUCCESS;
} catch (\Exception $e) {
$io->error("Error: " . $e->getMessage());
$this->finalize(false, "Error: " . $e->getMessage(), $input);
return Command::FAILURE;
}
}
private function dispatchProgress(InputInterface $input): void
{
if ($this->hub) {
$logFile = $input->getOption('log-file');
if ($logFile && file_exists($logFile)) {
$outputStr = file_get_contents($logFile);
$update = new Update(
'/migration',
json_encode([
'type' => 'migration_progress',
'output' => $outputStr
])
);
$this->hub->publish($update);
}
}
}
private function finalize(bool $success, string $message, InputInterface $input): void
{
if ($this->hub) {
$outputStr = $message;
$logFile = $input->getOption('log-file');
if ($logFile && file_exists($logFile)) {
$outputStr = file_get_contents($logFile);
}
$update = new Update(
'/migration',
json_encode([
'type' => 'migration_result',
'status' => $success ? 'success' : 'error',
'output' => $outputStr
])
);
$this->hub->publish($update);
}
$cleanupFiles = $input->getOption('cleanup-file');
if (!empty($cleanupFiles)) {
foreach ($cleanupFiles as $f) {
if (file_exists($f)) {
@unlink($f);
}
}
}
}
private function importUsers(\Doctrine\DBAL\Connection $connection, SymfonyStyle $io): void
{
$io->text("-> Migrating Users and Permissions...");

View File

@ -0,0 +1,198 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use OpenApi\Attributes as OA;
#[Route('/migration/import', name: 'api_migration_import')]
#[IsGranted('ROLE_SUPER_ADMIN')]
class ImportLegacyDbController extends AbstractController
{
#[Route('', methods: ['POST'])]
#[OA\Post(
summary: "Import legacy OpenGnsys 1.1 database",
description: "Imports data from an OG 1.1 database. It accepts either a .sql file upload via multipart/form-data or database credentials via JSON.",
responses: [
new OA\Response(response: 200, description: "Import completed successfully"),
new OA\Response(response: 400, description: "Invalid input or import failure"),
new OA\Response(response: 500, description: "Server error during import")
]
)]
public function __invoke(Request $request, KernelInterface $kernel): JsonResponse
{
// Set no time limit for this specific long-running controller
set_time_limit(0);
$inputArgs = ['command' => 'opengnsys:import-legacy'];
$tempFiles = [];
// Check if we received a file upload
$file = $request->files->get('sql_file');
if ($file) {
$extension = strtolower($file->getClientOriginalExtension());
if (!in_array($extension, ['sql', 'zip'])) {
return new JsonResponse(['error' => 'Uploaded file must be a .sql or .zip file'], 400);
}
$uploadDir = sys_get_temp_dir();
if ($extension === 'zip') {
$zipPath = $uploadDir . DIRECTORY_SEPARATOR . uniqid('legacy_import_', true) . '.zip';
$file->move($uploadDir, basename($zipPath));
$tempFiles[] = $zipPath;
$zip = new \ZipArchive();
if ($zip->open($zipPath) === true) {
$sqlFileName = null;
for ($i = 0; $i < $zip->numFiles; $i++) {
$stat = $zip->statIndex($i);
if (pathinfo($stat['name'], PATHINFO_EXTENSION) === 'sql') {
$sqlFileName = $stat['name'];
break;
}
}
if ($sqlFileName) {
$zip->extractTo($uploadDir, $sqlFileName);
$filePath = $uploadDir . DIRECTORY_SEPARATOR . $sqlFileName;
$tempFiles[] = $filePath;
$zip->close();
} else {
$zip->close();
foreach ($tempFiles as $f) { if (file_exists($f)) unlink($f); }
return new JsonResponse(['error' => 'No .sql file found inside the uploaded ZIP archive'], 400);
}
} else {
foreach ($tempFiles as $f) { if (file_exists($f)) unlink($f); }
return new JsonResponse(['error' => 'Failed to open the uploaded ZIP archive'], 500);
}
} else {
$fileName = uniqid('legacy_import_', true) . '.sql';
$file->move($uploadDir, $fileName);
$filePath = $uploadDir . DIRECTORY_SEPARATOR . $fileName;
$tempFiles[] = $filePath;
}
$inputArgs['--file'] = $filePath;
} else {
// Check for JSON payload with DB credentials
$data = json_decode($request->getContent(), true);
if (!$data || !isset($data['legacyHost']) || !isset($data['legacyUser']) || !isset($data['legacyDb'])) {
return new JsonResponse([
'error' => 'You must provide either an "sql_file" or a JSON payload with legacyHost, legacyUser, and legacyDb'
], 400);
}
$inputArgs['--legacy-host'] = $data['legacyHost'];
$inputArgs['--legacy-user'] = $data['legacyUser'];
if (!empty($data['legacyPassword'])) {
$inputArgs['--legacy-password'] = $data['legacyPassword'];
}
$inputArgs['--legacy-db'] = $data['legacyDb'];
}
try {
$logFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'opengnsys_migration_latest.log';
$pidFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'opengnsys_migration.pid';
// Check if already running
if (file_exists($pidFile)) {
$existingPid = trim(file_get_contents($pidFile));
if (file_exists('/proc/' . $existingPid)) {
return new JsonResponse(['error' => 'Ya hay una migración en curso. Por favor, espera a que termine.'], 409);
} else {
@unlink($pidFile); // Cleanup orphaned PID
}
}
$consolePath = $kernel->getProjectDir() . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'console';
if (!file_exists($consolePath)) {
return new JsonResponse(['error' => 'System error: bin/console not found.'], 500);
}
$commandArray = ['php', escapeshellarg($consolePath), 'opengnsys:import-legacy'];
foreach ($inputArgs as $key => $value) {
if ($key !== 'command') {
$commandArray[] = $key . '=' . escapeshellarg($value);
}
}
// Add cleanup files
foreach ($tempFiles as $f) {
$commandArray[] = '--cleanup-file=' . escapeshellarg($f);
}
// Log file for Mercure
$commandArray[] = '--log-file=' . escapeshellarg($logFile);
$commandArray[] = '--cleanup-file=' . escapeshellarg($logFile);
$commandArray[] = '--cleanup-file=' . escapeshellarg($pidFile); // Let command delete PID when done
$commandStr = implode(' ', $commandArray);
// Run detached OS process and get PID
exec("$commandStr > " . escapeshellarg($logFile) . " 2>&1 & echo $!", $output);
if (isset($output[0]) && is_numeric($output[0])) {
file_put_contents($pidFile, $output[0]);
}
return new JsonResponse([
'status' => 'started',
'message' => 'Migration started in background'
], 200);
} catch (\Exception $e) {
// Clean up the uploaded files in case of exception
foreach ($tempFiles as $f) {
if (file_exists($f)) {
unlink($f);
}
}
return new JsonResponse([
'error' => 'An error occurred while executing the migration process',
'details' => $e->getMessage()
], 500);
}
}
#[Route('/status', name: 'api_migration_status', methods: ['GET'])]
public function status(): JsonResponse
{
$pidFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'opengnsys_migration.pid';
$logFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'opengnsys_migration_latest.log';
$isRunning = false;
if (file_exists($pidFile)) {
$pid = trim(file_get_contents($pidFile));
if (file_exists('/proc/' . $pid)) {
$isRunning = true;
} else {
@unlink($pidFile);
}
}
$log = '';
if (file_exists($logFile)) {
$log = file_get_contents($logFile);
}
return new JsonResponse([
'isRunning' => $isRunning,
'log' => $log
]);
}
}