Pārlūkot izejas kodu

resolve conflicts

Olivier Massot 1 gadu atpakaļ
vecāks
revīzija
e4cd5dfdca
1 mainītis faili ar 41 papildinājumiem un 17 dzēšanām
  1. 41 17
      src/Service/Cron/Job/CleanDb.php

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

@@ -55,6 +55,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
      *
      * @throws \Exception
      * @throws \Doctrine\DBAL\Driver\Exception
+     * @throws \Throwable
      */
     protected function purgeDb(bool $commit = true): void
     {
@@ -64,7 +65,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);
 
@@ -86,6 +86,7 @@ class CleanDb extends BaseCronJob implements CronjobInterface
             }
         } catch (\Throwable $exception) {
             $this->connection->rollback();
+            throw $exception;
         }
     }
 
@@ -98,7 +99,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();
 
@@ -125,7 +126,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.
      *
      * @throws \Doctrine\DBAL\Exception
      */
@@ -133,19 +134,30 @@ class CleanDb extends BaseCronJob implements CronjobInterface
     {
         $this->ui->print('Purge the DB from the messages created before the '.$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.
      *
      * @throws \Doctrine\DBAL\Exception
      */
@@ -153,13 +165,25 @@ class CleanDb extends BaseCronJob implements CronjobInterface
     {
         $this->ui->print('Purge the DB from the notifications created before the '.$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;
     }
 }