Explorar o código

make the DbCheck command into a cron job, add connection to test9

Olivier Massot hai 4 meses
pai
achega
69a420adf2

+ 13 - 0
config/packages/doctrine.yaml

@@ -21,6 +21,10 @@ doctrine:
                 # IMPORTANT: You MUST configure your server version,
                 # either here or in the DATABASE_URL env var (see .env file)
                 server_version: '5.7'
+
+            testBackup:
+                url: '%env(resolve:DATABASE_TEST_BACKUP_URL)%'
+                server_version: '5.7'
         types:
             uuid: Ramsey\Uuid\Doctrine\UuidType
 
@@ -55,4 +59,13 @@ doctrine:
                 connection: audit
             adminassos:
                 connection: adminassos
+            testBackup:
+                connection: testBackup
+                mappings:
+                    App:
+                        is_bundle: false
+                        type: attribute
+                        dir: '%kernel.project_dir%/src/Entity'
+                        prefix: 'App\Entity'
+                        alias: App
 

+ 3 - 0
config/secrets/docker/docker.DATABASE_TEST_BACKUP_URL.6c9874.php

@@ -0,0 +1,3 @@
+<?php // docker.DATABASE_TEST_BACKUP_URL.6c9874 on Tue, 05 Aug 2025 09:07:06 +0000
+
+return "\x8B\x80\xB7N\xD5\xAA\x3AX\x81\x2B\x8F\x06\x0D\x06\x92\x7D\xADj\xF1\xD2Et\xCF\x97\x27m\x2C\xE5\xDE\xAD\x844\xA7c\x0Bt\x5D\xD1\x19s\x3F\xAF\xF6\x25\xB6\xB5E\xEA\xA7I\x1B\xA4\x06M\xE4\x82D\xD1Rb\x7F\x7D5_0\x7C\xBE\xB7\xAB\x1AX\x1D\x968\x89\xFER\xB9\x03xI\x07~\x86\xE0\x188X\x11\x80\x83\x29\x7B\xD1\x60\x0F\xE8\x7B\x1E\x0F\xF9r\xBBj\xBAS\x84\xF9\xF9\x93\xB01\xC3x\x288Ja\x83D\x16R\x19\xE3\xA1";

+ 1 - 0
config/secrets/docker/docker.list.php

@@ -3,6 +3,7 @@
 return [
     'DATABASE_ADMINASSOS_URL' => null,
     'DATABASE_AUDIT_URL' => null,
+    'DATABASE_TEST_BACKUP_URL' => null,
     'DATABASE_URL' => null,
     'DOLIBARR_API_TOKEN' => null,
     'MERCURE_JWT_SECRET' => null,

+ 3 - 0
config/secrets/prod/prod.DATABASE_TEST_BACKUP_URL.6c9874.php

@@ -0,0 +1,3 @@
+<?php // prod.DATABASE_TEST_BACKUP_URL.6c9874 on Tue, 05 Aug 2025 09:09:13 +0000
+
+return "\xF2\xD9\x94\xC5\xD0\xE6t\xB8p\x97\xAFfz\xA6\xA7\x9DX9\xB92\x9C\xFB\xD5\xA4c\x92\xA8\x1D\x0C\xB6\x23-e\xB6\x20t\xE1\xC7\x17\x3FCX\x16zQ\x27\x887\x12\x8DJ\xA5\x22.\x83\xD2o\xD3\x7Cz\x80\x86\x24\xA6\x16\x86V\xF9\x60.I3\xADT\xE1\xAD\x0F\x89\xAF\x0E\x87\xFF\x07\x83\x89\x7BX\x9B\x1E\xD2\x04\xC2\x3E\x0C\x87Y\xBD\x81\xCA\xF8\x3B\xE4x\xB1\x9EN\xF0\xB7\xD7\x1A\x84\x2C\xFC\xB3\xE5S\x29\x14\xAD_\x40\x7F\x9E\xB6\x83";

+ 1 - 0
config/secrets/prod/prod.list.php

@@ -3,6 +3,7 @@
 return [
     'DATABASE_ADMINASSOS_URL' => null,
     'DATABASE_AUDIT_URL' => null,
+    'DATABASE_TEST_BACKUP_URL' => null,
     'DATABASE_URL' => null,
     'DOLIBARR_API_TOKEN' => null,
     'MERCURE_JWT_SECRET' => null,

+ 3 - 0
config/secrets/staging/staging.DATABASE_TEST_BACKUP_URL.6c9874.php

@@ -0,0 +1,3 @@
+<?php // staging.DATABASE_TEST_BACKUP_URL.6c9874 on Tue, 05 Aug 2025 09:09:50 +0000
+
+return "\x0A\xCD\xEC\xEB\xA7\xDC\xFD\x60\x5E\x3A\x28\x09\xAB\xCC\x0C\xC5\xBC\x0B\x1A7O\x1B1\x86_\xC8\x3A\x05Zy\x25\x11\xDB\x89\x1CQ\x14l\xCC\x8F\xF2R\x9DNv\x9F\xED\x8A";

+ 1 - 0
config/secrets/staging/staging.list.php

@@ -3,6 +3,7 @@
 return [
     'DATABASE_ADMINASSOS_URL' => null,
     'DATABASE_AUDIT_URL' => null,
+    'DATABASE_TEST_BACKUP_URL' => null,
     'DATABASE_URL' => null,
     'DOLIBARR_API_TOKEN' => null,
 ];

+ 3 - 0
config/secrets/test/test.DATABASE_TEST_BACKUP_URL.6c9874.php

@@ -0,0 +1,3 @@
+<?php // test.DATABASE_TEST_BACKUP_URL.6c9874 on Tue, 05 Aug 2025 09:09:36 +0000
+
+return "\x22\x0D\x1C\x3C\xD0\x0A\x3C\xDFO\x1B\xB2\xB7\xDBr\xEC\xE6gy\x00\xE8n\x80\x0E\x3A\xA78A\xEB\x3Cv\x92UqJ\x2A\xD3\x29\x3B~z\x18\x07H\x02\x1A\xD4\xC9\xD2\xF5\x8E\xE1\x5D\xC9l\x83Y5\x0EA\xDAO\xFE\x0FC\xCB\xAC\x23\x08\x7C\xF5J\x60\xC4_c\x0Fmh03\xF1\xD5\x95\xE0\x9B\xD1\xC1\x04\x5Em\x15D\x9DM\xA7\x02\xA0\xF7\x04K\xAC\xFE\x18\x898\x82\x14\xF9\x11O\xDE\x3E\xEE\xFE\x7D\x2F\x7C\x07\xAA\x01\xBB\xBF\xEE\xA0\x2C";

+ 1 - 0
config/secrets/test/test.list.php

@@ -3,6 +3,7 @@
 return [
     'DATABASE_ADMINASSOS_URL' => null,
     'DATABASE_AUDIT_URL' => null,
+    'DATABASE_TEST_BACKUP_URL' => null,
     'DATABASE_URL' => null,
     'DOLIBARR_API_TOKEN' => null,
     'MERCURE_JWT_SECRET' => null,

+ 3 - 123
src/Commands/Doctrine/OtDbCheckCommand.php

@@ -4,17 +4,12 @@ declare(strict_types=1);
 
 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 Symfony\Component\Console\Attribute\AsCommand;
 use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
-use Symfony\Component\Console\Style\SymfonyStyle;
 use Symfony\Contracts\Service\Attribute\Required;
 
 /**
@@ -48,129 +43,14 @@ use Symfony\Contracts\Service\Attribute\Required;
 )]
 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(
-        private readonly EntityManagerInterface $entityManager,
+        private readonly DbCheck $dbCheck,
     ) {
         parent::__construct();
     }
 
-    /** @noinspection PhpUnused */
-    #[Required]
-    public function setLoggerInterface(LoggerInterface $cronLogger): void
-    {
-        $this->logger = $cronLogger;
-    }
-
     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();
     }
 }

+ 136 - 0
src/Service/Cron/Job/DbCheck.php

@@ -0,0 +1,136 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Service\Cron\Job;
+
+use App\Entity\Organization\Organization;
+use App\Service\Cron\BaseCronJob;
+use Doctrine\DBAL\Connection;
+use Doctrine\ORM\EntityManagerInterface;
+use Symfony\Component\Console\Command\Command;
+
+class DbCheck extends BaseCronJob
+{
+    const IGNORE_EMPTY_TABLES = [
+        'messenger_messages',
+        'enqueue',
+        'zzz_.*',
+        'CycleByEducation', // Table a priori non utilisée
+        'educationalproject_file',
+        'ElasticaSynchro',
+        'Odyssee',
+        'PeriodNotation', // Table a priori non utilisée
+        'Presence',
+        'tag_accessWish',
+        'tag_control',
+        'tag_educationNotation',
+        'tag_educationStudent',
+        'tag_repair',
+        'Audit_.*'
+    ];
+
+    public function __construct(
+        private readonly EntityManagerInterface $testBackupEntityManager,
+    ) {
+        parent::__construct();
+    }
+
+    public function preview(): void {
+        $this->ui->print('No preview available for this job');
+    }
+
+    /**
+     * Execute database integrity checks
+     *
+     * @return int Command::SUCCESS or Command::FAILURE
+     */
+    public function execute(): void
+    {
+        $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->critical($e->getMessage());
+        }
+
+        $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->critical(sprintf('Impossible de vérifier la table %s: %s', $table, $tableException->getMessage()));
+                }
+            }
+
+            if (count($emptyTables) > 0) {
+                $this->logger->critical('%d tables vides détectées sur %d tables au total');
+                $this->logger->error('Tables vides:');
+                foreach ($emptyTables as $table) {
+                    $this->logger->error('- ' . $table);
+                }
+            } else {
+                $this->logger->info('Toutes les tables sont présentes et contiennent des données');
+            }
+        } catch (\Exception $e) {
+            $this->logger->critical('Erreur lors de la vérification de l\'intégrité des données: ' . $e->getMessage());
+            $this->logger->debug('Stack trace: ' . $e->getTraceAsString());
+        }
+    }
+
+    /**
+     * 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->testBackupEntityManager->getConnection();
+            $connection->connect();
+
+            if ($connection->isConnected()) {
+                $this->logger->info('Connexion à la base de données testBackup établie avec succès');
+
+                $params = $connection->getParams();
+                $this->logger->debug(sprintf(
+                    'Paramètres de connexion testBackup: 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;
+    }
+}