Log warning when attempting to access nonexistant table from api
authorColeman Watts <coleman@civicrm.org>
Wed, 12 Aug 2020 20:50:32 +0000 (16:50 -0400)
committerColeman Watts <coleman@civicrm.org>
Thu, 13 Aug 2020 00:50:58 +0000 (20:50 -0400)
Civi/Api4/Generic/DAOGetAction.php
api/v3/utils.php

index cbdec8841e66199412eb7920dde34ffae560c4f6..d6a3f10eea2aaa74fa61909ef07634ef8711a0fb 100644 (file)
@@ -88,6 +88,7 @@ class DAOGetAction extends AbstractGetAction {
     // Early return if table doesn't exist yet due to pending upgrade
     $baoName = $this->getBaoName();
     if (!$baoName::tableHasBeenAdded()) {
+      \Civi::log()->warning("Could not read from {$this->getEntityName()} before table has been added. Upgrade required.", ['civi.tag' => 'upgrade_needed']);
       return;
     }
 
index 50268a400c1dba09291521f5b25adef03a2aa0ea..799dc7803197063a0168e80510ee71eb7e020659 100644 (file)
@@ -1221,7 +1221,7 @@ function formatCheckBoxField(&$checkboxFieldValue, $customFieldLabel, $entity) {
 /**
  * Function to do a 'standard' api get - when the api is only doing a $bao->find then use this.
  *
- * @param string $bao_name
+ * @param string|CRM_Core_DAO $bao_name
  *   Name of BAO.
  * @param array $params
  *   Params from api.
@@ -1240,20 +1240,27 @@ function _civicrm_api3_basic_get($bao_name, $params, $returnAsSuccess = TRUE, $e
   $entity = $entity ?: CRM_Core_DAO_AllCoreTables::getBriefName($bao_name);
   $options = _civicrm_api3_get_options_from_params($params);
 
-  $query = new \Civi\API\Api3SelectQuery($entity, CRM_Utils_Array::value('check_permissions', $params, FALSE));
-  $query->where = $params;
-  if ($options['is_count']) {
-    $query->select = ['count_rows'];
+  // Skip query if table doesn't exist yet due to pending upgrade
+  if (!$bao_name::tableHasBeenAdded()) {
+    \Civi::log()->warning("Could not read from {$entity} before table has been added. Upgrade required.", ['civi.tag' => 'upgrade_needed']);
+    $result = [];
   }
   else {
-    $query->select = array_keys(array_filter($options['return']));
-    $query->orderBy = $options['sort'];
-    $query->isFillUniqueFields = $uniqueFields;
-  }
-  $query->limit = $options['limit'];
-  $query->offset = $options['offset'];
-  $query->merge($sql);
-  $result = $query->run();
+    $query = new \Civi\API\Api3SelectQuery($entity, $params['check_permissions'] ?? FALSE);
+    $query->where = $params;
+    if ($options['is_count']) {
+      $query->select = ['count_rows'];
+    }
+    else {
+      $query->select = array_keys(array_filter($options['return']));
+      $query->orderBy = $options['sort'];
+      $query->isFillUniqueFields = $uniqueFields;
+    }
+    $query->limit = $options['limit'];
+    $query->offset = $options['offset'];
+    $query->merge($sql);
+    $result = $query->run();
+  }
 
   if ($returnAsSuccess) {
     return civicrm_api3_create_success($result, $params, $entity, 'get');