From 8672f69c859a7d7e2f918ac8032ed9e87a29cf04 Mon Sep 17 00:00:00 2001 From: Eileen Date: Mon, 20 Feb 2023 23:04:23 +0000 Subject: [PATCH] Mitigation of scenario where an import table has been deleted and the metadata is out of date --- ext/civiimport/Civi/Api4/Import/ImportSpecProvider.php | 10 ++++++++-- ext/civiimport/Civi/BAO/Import.php | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ext/civiimport/Civi/Api4/Import/ImportSpecProvider.php b/ext/civiimport/Civi/Api4/Import/ImportSpecProvider.php index 333f5a7c2066..c423e509d706 100644 --- a/ext/civiimport/Civi/Api4/Import/ImportSpecProvider.php +++ b/ext/civiimport/Civi/Api4/Import/ImportSpecProvider.php @@ -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(); diff --git a/ext/civiimport/Civi/BAO/Import.php b/ext/civiimport/Civi/BAO/Import.php index eff6f36c4b37..2a7e807da7d5 100644 --- a/ext/civiimport/Civi/BAO/Import.php +++ b/ext/civiimport/Civi/BAO/Import.php @@ -203,7 +203,12 @@ public static function getFieldsForTable(string $tableName): array { $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];