Переглянути джерело

Merge branch 'hotfix/fix_cleandb_job'

Olivier Massot 1 рік тому
батько
коміт
6555d11411
1 змінених файлів з 42 додано та 17 видалено
  1. 42 17
      src/Service/Cron/Job/CleanDb.php

+ 42 - 17
src/Service/Cron/Job/CleanDb.php

@@ -10,6 +10,7 @@ use Doctrine\DBAL\Connection;
 use Doctrine\DBAL\DBALException;
 use Exception;
 use JetBrains\PhpStorm\Pure;
+use Throwable;
 
 /**
  * Cronjob that delete records older than N days in DB tables like Audit_ or Message
@@ -58,6 +59,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
      * @param bool $commit
      * @throws Exception
      * @throws \Doctrine\DBAL\Driver\Exception
+     * @throws Throwable
      */
     protected function purgeDb(bool $commit = true): void
     {
@@ -67,7 +69,6 @@ class CleanDb extends BaseCronJob implements CronjobInterface
         $maxDateAudit = DatesUtils::new();
         $maxDateAudit->sub(new \DateInterval('P' . self::PURGE_AUDIT_RECORDS_OLDER_THAN . 'D'));
 
-        $this->ui->print('Purge DB from temporary records modified before ' . $maxDate->format('c'));
         $this->connection->beginTransaction();
         $this->connection->setAutoCommit(false);
 
@@ -88,6 +89,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
             }
         }catch (\Throwable $exception){
             $this->connection->rollback();
+            throw $exception;
         }
     }
 
@@ -102,7 +104,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
      */
     protected function purgeAuditTables(DateTime $maxDate): int
     {
-        $this->ui->print('Purge Audit tables');
+        $this->ui->print('Purge Audit_* tables from the records created before the '.$maxDate->format('c'));
 
         $tableNames = $this->connection->getSchemaManager()->listTableNames();
 
@@ -129,7 +131,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
     }
 
     /**
-     * Purge Message table and returns the number of deleted records
+     * Purge Message and ReportMessage tables and returns the number of deleted records
      *
      * @param DateTime $maxDate
      * @return int
@@ -139,19 +141,30 @@ class CleanDb extends BaseCronJob implements CronjobInterface
     {
         $this->ui->print('Purge DB from records of message create before ' . $maxDate->format('c'));
 
-        $q = $this->connection->createQueryBuilder();
+        $sql = "DELETE r
+                FROM opentalent.Message m
+                inner join opentalent.ReportMessage r on r.message_id = m.id
+                where (m.dateSent < :maxDate or (m.dateSent is null and m.createDate < :maxDate)) and m.isSystem = true and m.id > 0;";
 
-        $q->delete('Message')
-            ->andWhere( $q->expr()->gt('id', 0))
-            ->andWhere( $q->expr()->lt('dateSent', $maxDate->format('Y-m-d')))
-            ->andWhere( $q->expr()->eq('isSystem', true))
-        ;
+        $stmt = $this->connection->prepare($sql);
+        $purgedReportMessage = $stmt->executeStatement(['maxDate' => $maxDate->format('c')]);
 
-        return $q->execute();
+        $this->ui->print('* ReportMessage : '.$purgedReportMessage.' lines to delete');
+
+        $sql = "DELETE
+                FROM opentalent.Message
+                where (m.dateSent < :maxDate or (m.dateSent is null and m.createDate < :maxDate)) and isSystem = true and id > 0;";
+
+        $stmt = $this->connection->prepare($sql);
+        $purgedMessage = $stmt->executeStatement(['maxDate' => $maxDate->format('c')]);
+
+        $this->ui->print('* Message : '.$purgedMessage.' lines to delete');
+
+        return $purgedReportMessage + $purgedMessage;
     }
 
     /**
-     * Purge Information (Notification Discr) table and returns the number of deleted records
+     * Purge Information (Notification Discr) and NotificationUser tables, and returns the number of deleted records
      *
      * @param DateTime $maxDate
      * @return int
@@ -161,13 +174,25 @@ class CleanDb extends BaseCronJob implements CronjobInterface
     {
         $this->ui->print('Purge DB from records of notifications create before ' . $maxDate->format('c'));
 
-        $q = $this->connection->createQueryBuilder();
+        $sql = "DELETE u
+                FROM opentalent.Information i
+                inner join opentalent.NotificationUser u on u.notification_id = i.id
+                where i.createDate < :maxDate and i.discr = 'notification';";
+
+        $stmt = $this->connection->prepare($sql);
+        $purgedNotificationUser = $stmt->executeStatement(['maxDate' => $maxDate->format('c')]);
+
+        $this->ui->print('* NotificationUser : '.$purgedNotificationUser.' lines to delete');
+
+        $sql = "DELETE
+                FROM opentalent.Information
+                where createDate < :maxDate and discr = 'notification';";
+
+        $stmt = $this->connection->prepare($sql);
+        $purgedNotification = $stmt->executeStatement(['maxDate' => $maxDate->format('c')]);
 
-        $q->delete('Information')
-            ->andWhere( $q->expr()->eq('discr', '"notification"'))
-            ->andWhere( $q->expr()->lt('createDate', $maxDate->format('Y-m-d')))
-        ;
+        $this->ui->print('* Information : '.$purgedNotification.' lines to delete');
 
-        return $q->execute();
+        return $purgedNotificationUser + $purgedNotification;
     }
 }