From 32cb839817d7c5ecacbb94f84d28405cf78a83bf Mon Sep 17 00:00:00 2001
From: Adrian Lorenc <adrian.lorenc@gmail.com>
Date: Wed, 26 Mar 2025 15:51:53 +0100
Subject: [PATCH] ISAICP-9614: Use DB API instead of entity.

---
 .../Commands/WebformAssessmentCommands.php    | 22 ++++++++-----------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/web/modules/custom/joinup_assessment/src/Drush/Commands/WebformAssessmentCommands.php b/web/modules/custom/joinup_assessment/src/Drush/Commands/WebformAssessmentCommands.php
index 018ffc228e..36594a4e9b 100644
--- a/web/modules/custom/joinup_assessment/src/Drush/Commands/WebformAssessmentCommands.php
+++ b/web/modules/custom/joinup_assessment/src/Drush/Commands/WebformAssessmentCommands.php
@@ -9,6 +9,7 @@
 use Drupal\Core\DependencyInjection\AutowireTrait;
 use Drupal\Core\Entity\EntityTypeManagerInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\joinup_assessment\AssessmentInterface;
 use Drupal\mysql\Driver\Database\mysql\Connection;
 use Drush\Attributes as CLI;
 use Drush\Commands\DrushCommands;
@@ -54,25 +55,20 @@ public function disableDefaultWebformSanitization(InputInterface $input): void {
    */
   #[CLI\Hook(type: HookManager::POST_COMMAND_HOOK, target: 'sql-sanitize')]
   public function sanitize($result, CommandData $commandData): void {
-    $webformStorage = $this->entityTypeManager->getStorage('webform_submission');
-    $querySubmission = $webformStorage->getQuery()
-      ->condition('webform_id', 'assessment', '!=')
-      ->accessCheck(FALSE);
+    $this->database->delete('webform_submission')
+      ->condition('webform_id', AssessmentInterface::WEBFORM_ID, '!=')
+      ->execute();
 
-    do {
-      $sids = $querySubmission->range(0, 50)->execute();
-
-      if (!empty($sids)) {
-        $webformStorage->delete(
-          $webformStorage->loadMultiple($sids)
-        );
-      }
-    } while (!empty($sids));
+    $this->database->delete('webform_submission_data')
+      ->condition('webform_id', AssessmentInterface::WEBFORM_ID, '!=')
+      ->execute();
 
     if ($this->moduleHandler->moduleExists('webform_submission_log')) {
       $this->database->truncate('webform_submission_log')->execute();
     }
 
+    $this->entityTypeManager->getStorage('webform_submission')->resetCache();
+
     $this->logger()
       ->success('Webform submission tables truncated except for the assessment.');
   }
-- 
GitLab