Mitigation of scenario where an import table has been deleted and the metadata is...
authorEileen <emcnaughton@wikimedia.org>
Mon, 20 Feb 2023 23:04:23 +0000 (23:04 +0000)
committerEileen <emcnaughton@wikimedia.org>
Mon, 20 Feb 2023 23:04:23 +0000 (23:04 +0000)
ext/civiimport/Civi/Api4/Import/ImportSpecProvider.php
ext/civiimport/Civi/BAO/Import.php

index 333f5a7c206660203880227b90e7b31a52de810a..c423e509d706a155d01463d73edb9cbddbadedd5 100644 (file)
@@ -32,8 +32,14 @@ class ImportSpecProvider extends AutoService implements SpecProviderInterface {
    */
   public function modifySpec(RequestSpec $spec): void {
     $tableName = $spec->getEntityTableName();
-    $columns = Import::getFieldsForTable($tableName);
-    $action = $spec->getAction();
+    try {
+      $columns = Import::getFieldsForTable($tableName);
+    }
+    catch (\CRM_Core_Exception $e) {
+      // The api metadata may retain the expectation that this entity exists after the
+      // table is deleted - & hence we get an error.
+      return;
+    }
     // CheckPermissions does not reach us here - so we will have to rely on earlier permission filters.
     $userJobID = substr($spec->getEntity(), (strpos($spec->getEntity(), '_') + 1));
     $userJob = UserJob::get(FALSE)->addWhere('id', '=', $userJobID)->addSelect('metadata', 'job_type', 'created_id')->execute()->first();
index eff6f36c4b370593a5c437d7711dddb5cb71d238..2a7e807da7d515e0c56522df568ecea81590d06d 100644 (file)
@@ -203,7 +203,12 @@ class Import extends CRM_Core_DAO {
     $headers = UserJob::get(FALSE)
       ->addWhere('metadata', 'LIKE', '%' . $tableName . '%')
       ->addSelect('metadata')->execute()->first()['metadata']['DataSource']['column_headers'] ?? [];
-    $result = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $tableName");
+    try {
+      $result = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $tableName");
+    }
+    catch (\PEAR_Exception $e) {
+      throw new CRM_Core_Exception('Import table no longer exists');
+    }
     $userFieldIndex = 0;
     while ($result->fetch()) {
       $columns[$result->Field] = ['name' => $result->Field, 'table_name' => $tableName];