|
@@ -4,17 +4,12 @@ declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Commands\Doctrine;
|
|
namespace App\Commands\Doctrine;
|
|
|
|
|
|
|
|
-use Doctrine\DBAL\Connection;
|
|
|
|
|
-use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
-use Doctrine\ORM\Tools\SchemaValidator;
|
|
|
|
|
-use Doctrine\ORM\Tools\SchemaTool;
|
|
|
|
|
|
|
+use App\Service\Cron\Job\DbCheck;
|
|
|
use Psr\Log\LoggerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
-use Symfony\Component\Console\Input\InputOption;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
-use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -48,129 +43,14 @@ use Symfony\Contracts\Service\Attribute\Required;
|
|
|
)]
|
|
)]
|
|
|
class OtDbCheckCommand extends Command
|
|
class OtDbCheckCommand extends Command
|
|
|
{
|
|
{
|
|
|
- private LoggerInterface $logger;
|
|
|
|
|
- const IGNORE_EMPTY_TABLES = [
|
|
|
|
|
- 'messenger_messages',
|
|
|
|
|
- 'enqueue',
|
|
|
|
|
- 'zzz_.*',
|
|
|
|
|
- 'CycleByEducation', // Table a priori non utilisée
|
|
|
|
|
- 'educationalproject_file',
|
|
|
|
|
- 'Odyssee',
|
|
|
|
|
- 'PeriodNotation', // Table a priori non utilisée
|
|
|
|
|
- 'Presence',
|
|
|
|
|
- 'tag_accessWish',
|
|
|
|
|
- 'tag_control',
|
|
|
|
|
- 'tag_educationNotation',
|
|
|
|
|
- 'tag_educationStudent',
|
|
|
|
|
- 'tag_repair',
|
|
|
|
|
- 'Audit_.*'
|
|
|
|
|
- ];
|
|
|
|
|
-
|
|
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
- private readonly EntityManagerInterface $entityManager,
|
|
|
|
|
|
|
+ private readonly DbCheck $dbCheck,
|
|
|
) {
|
|
) {
|
|
|
parent::__construct();
|
|
parent::__construct();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /** @noinspection PhpUnused */
|
|
|
|
|
- #[Required]
|
|
|
|
|
- public function setLoggerInterface(LoggerInterface $cronLogger): void
|
|
|
|
|
- {
|
|
|
|
|
- $this->logger = $cronLogger;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
{
|
|
{
|
|
|
- $this->logger->info('Démarrage de la vérification de l\'intégrité de la base de données');
|
|
|
|
|
-
|
|
|
|
|
- $this->logger->info('1. Vérification de la connexion à la base de données');
|
|
|
|
|
- try {
|
|
|
|
|
- $connection = $this->getDbConnection();
|
|
|
|
|
- } catch (\Exception $e) {
|
|
|
|
|
- $this->logger->error($e->getMessage());
|
|
|
|
|
- return Command::FAILURE;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $this->logger->info('2. Vérification de l\'intégrité des données');
|
|
|
|
|
- try {
|
|
|
|
|
- $schemaManager = $connection->createSchemaManager();
|
|
|
|
|
- $tables = $schemaManager->listTableNames();
|
|
|
|
|
- $this->logger->debug(sprintf('Nombre total de tables: %d', count($tables)));
|
|
|
|
|
-
|
|
|
|
|
- $emptyTables = [];
|
|
|
|
|
-
|
|
|
|
|
- foreach ($tables as $table) {
|
|
|
|
|
- try {
|
|
|
|
|
- if (preg_match('/^' . implode('|', self::IGNORE_EMPTY_TABLES) . '$/', $table)) {
|
|
|
|
|
- $this->logger->debug(sprintf('Table %s: -- ignored --', $table));
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $count = (int) $connection->executeQuery("SELECT COUNT(*) FROM {$table}")->fetchOne();
|
|
|
|
|
-
|
|
|
|
|
- if (!$count > 0) {
|
|
|
|
|
- $emptyTables[] = $table;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $this->logger->debug(sprintf('Table %s: %d enregistrements', $table, $count));
|
|
|
|
|
- } catch (\Exception $tableException) {
|
|
|
|
|
- $this->logger->error(sprintf('Impossible de vérifier la table %s: %s', $table, $tableException->getMessage()));
|
|
|
|
|
- return Command::FAILURE;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (count($emptyTables) > 0) {
|
|
|
|
|
- $this->logger->error('%d tables vides détectées sur %d tables au total');
|
|
|
|
|
- $this->logger->error('Tables vides:');
|
|
|
|
|
- foreach ($emptyTables as $table) {
|
|
|
|
|
- $this->logger->error('- ' . $table);
|
|
|
|
|
- }
|
|
|
|
|
- return Command::FAILURE;
|
|
|
|
|
- } else {
|
|
|
|
|
- $this->logger->info('Toutes les tables sont présentes et contiennent des données');
|
|
|
|
|
- }
|
|
|
|
|
- } catch (\Exception $e) {
|
|
|
|
|
- $this->logger->error('Erreur lors de la vérification de l\'intégrité des données: ' . $e->getMessage());
|
|
|
|
|
- $this->logger->debug('Stack trace: ' . $e->getTraceAsString());
|
|
|
|
|
- return Command::FAILURE;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return Command::SUCCESS;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * Establishes a connection to the database.
|
|
|
|
|
- *
|
|
|
|
|
- * @return Connection The active database connection.
|
|
|
|
|
- * @throws \RuntimeException If the connection could not be established
|
|
|
|
|
- * or if an error occurs during the process.
|
|
|
|
|
- */
|
|
|
|
|
- private function getDbConnection(): Connection
|
|
|
|
|
- {
|
|
|
|
|
- try {
|
|
|
|
|
- /** @var Connection $connection */
|
|
|
|
|
- $connection = $this->entityManager->getConnection();
|
|
|
|
|
- $connection->connect();
|
|
|
|
|
-
|
|
|
|
|
- if ($connection->isConnected()) {
|
|
|
|
|
- $this->logger->info('Connexion à la base de données établie avec succès');
|
|
|
|
|
-
|
|
|
|
|
- $params = $connection->getParams();
|
|
|
|
|
- $this->logger->debug(sprintf(
|
|
|
|
|
- 'Paramètres de connexion: Driver=%s, Host=%s, Port=%s, Database=%s, User=%s',
|
|
|
|
|
- $params['driver'] ?? 'N/A',
|
|
|
|
|
- $params['host'] ?? 'N/A',
|
|
|
|
|
- $params['port'] ?? 'N/A',
|
|
|
|
|
- $params['dbname'] ?? 'N/A',
|
|
|
|
|
- $params['user'] ?? 'N/A'
|
|
|
|
|
- ));
|
|
|
|
|
- } else {
|
|
|
|
|
- throw new \RuntimeException('Impossible de se connecter à la base de données');
|
|
|
|
|
- }
|
|
|
|
|
- } catch (\Exception $e) {
|
|
|
|
|
- throw new \RuntimeException('Erreur lors de la connexion à la base de données: ' . $e->getMessage());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return $connection;
|
|
|
|
|
|
|
+ return $this->dbCheck->execute();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|