Skip to content

Commit

Permalink
Merge pull request civicrm#25633 from eileenmcnaughton/559
Browse files Browse the repository at this point in the history
PEAR Exception handling - Mitigation of scenario where an import table has been deleted and the metadata is out of date
  • Loading branch information
totten authored Feb 22, 2023
2 parents a7a482e + 8672f69 commit 1cb8fbf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions ext/civiimport/Civi/Api4/Import/ImportSpecProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion ext/civiimport/Civi/BAO/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 1cb8fbf

Please sign in to comment.