ogcore/src/Command/Migration/MigrateClientsCommand.php

132 lines
6.3 KiB
PHP

<?php
namespace App\Command\Migration;
use App\Entity\Client;
use App\Entity\HardwareProfile;
use App\Entity\OrganizationalUnit;
use App\Model\OrganizationalUnitTypes;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'opengnsys:migration:clients', description: 'Migrate clients data')]
class MigrateClientsCommand extends Command
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly ManagerRegistry $doctrine
)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
/** @var EntityManagerInterface $oldDatabaseEntityManager */
$oldDatabaseEntityManager = $this->entityManager;
$clientRepository = $this->entityManager->getRepository(Client::class);
$organizationalUnitRepository = $this->entityManager->getRepository(OrganizationalUnit::class);
$hardwareProfileRepository = $this->entityManager->getRepository(HardwareProfile::class);
/** Obtener los ordenadores de la base de datos antigua **/
$rsmClients = new ResultSetMapping();
$rsmClients->addScalarResult('idordenador', 'idordenador');
$rsmClients->addScalarResult('nombreordenador', 'nombreordenador');
$rsmClients->addScalarResult('numserie', 'numserie');
$rsmClients->addScalarResult('mac', 'mac');
$rsmClients->addScalarResult('ip', 'ip');
$rsmClients->addScalarResult('netiface', 'netiface');
$rsmClients->addScalarResult('netdriver', 'netdriver');
$rsmClients->addScalarResult('idperfilhard', 'idperfilhard');
$rsmClients->addScalarResult('grupoid', 'grupoid');
$rsmClients->addScalarResult('idaula', 'idaula');
$clientsQuery = $oldDatabaseEntityManager->createNativeQuery('SELECT idordenador, nombreordenador, numserie, ip, mac, netiface, netdriver, idaula, grupoid, idperfilhard FROM ordenadores', $rsmClients);
$clients = $clientsQuery->getResult();
/** Ordenadores **/
$output->writeln("CLIENTES TOTAL: ". count($clients));
$count = 0;
foreach ($clients as $client){
try {
$clientEntity = $clientRepository->findOneBy(['migrationId' => $client['idordenador']]);
if(!$clientEntity){
// Proactive check to avoid EntityManager closure on duplicate key
if ($client['ip'] && $clientRepository->findOneBy(['ip' => $client['ip']])) {
$output->writeln("<error>Skipping client " . $client['nombreordenador'] . " due to duplicate IP: " . $client['ip'] . "</error>");
continue;
}
if ($client['mac'] && $clientRepository->findOneBy(['mac' => $client['mac']])) {
$output->writeln("<error>Skipping client " . $client['nombreordenador'] . " due to duplicate MAC: " . $client['mac'] . "</error>");
continue;
}
$clientEntity = new Client();
$clientEntity->setMigrationId($client['idordenador']);
$clientEntity->setName($client['nombreordenador']);
$clientEntity->setSerialNumber($client['numserie']);
$clientEntity->setNetiface($client['netiface']);
$clientEntity->setNetdriver($client['netdriver']);
// Format MAC: ensure colons and uppercase
$mac = str_replace([':', '-', '.'], '', $client['mac']);
if (strlen($mac) === 12) {
$mac = strtoupper(implode(':', str_split($mac, 2)));
}
$clientEntity->setMac($mac);
$clientEntity->setIp($client['ip']);
$clientEntity->setMaintenance(false);
$clientEntity->setPosition(['x' => 0, 'y' => 0]);
}
$parentMigrationId = (empty($client['grupoid']) || $client['grupoid'] == 0)
? OrganizationalUnitTypes::CLASSROOM . '-' . $client['idaula']
: OrganizationalUnitTypes::CLIENTS_GROUP . '-' . $client['grupoid'];
$organizationalUnit = $organizationalUnitRepository->findOneBy(['migrationId' => $parentMigrationId]);
if (!$organizationalUnit){
$output->writeln("ORGANIZATIONAL UNIT NOT FOUND FOR CLIENT: ". $client['nombreordenador'] . " (ID: ". $parentMigrationId .")");
}
if ($organizationalUnit){
$clientEntity->setOrganizationalUnit($organizationalUnit);
}
// Note: We don't link the 1.1 hardware profile entity here because the 2.0
// HardwareProfile entity is exclusive to the client (OneToOne).
// The population of hardware items will be done in the next command
// based on the client's legacy idperfilhard.
$this->entityManager->persist($clientEntity);
$count++;
if ($count % 100 === 0) {
$output->writeln("... Processed $count clients ...");
$this->entityManager->flush();
$this->entityManager->clear(Client::class);
$this->entityManager->clear(HardwareProfile::class);
}
} catch (\Exception $e) {
$output->writeln("<error>CRITICAL ERROR migrating client " . ($client['nombreordenador'] ?? 'ID:'.$client['idordenador']) . ": " . $e->getMessage() . "</error>");
// Reset EntityManager if a database error occurred
if (!$this->entityManager->isOpen()) {
$output->writeln("<error>EntityManager closed. Stopping migration.</error>");
return Command::FAILURE;
}
}
}
$this->entityManager->flush();
$output->writeln("CLIENTES MIGRADOS CORRECTAMENTE: $count");
return Command::SUCCESS;
}
}