If there are pending upgrades, checks if table exists before attempting to read from it.
return $result;
}
+ /**
+ * Checks if this DAO's table ought to exist.
+ *
+ * If there are pending DB updates, this function compares the CiviCRM version of the table to the current schema version.
+ *
+ * @return bool
+ * @throws CRM_Core_Exception
+ */
+ public static function tableHasBeenAdded() {
+ if (CRM_Utils_System::version() === CRM_Core_BAO_Domain::version()) {
+ return TRUE;
+ }
+ $daoExt = constant(static::class . '::EXT');
+ $daoVersion = constant(static::class . '::TABLE_ADDED') ?? '1.0';
+ return !($daoExt === 'civicrm' && version_compare(CRM_Core_BAO_Domain::version(), $daoVersion, '<'));
+ }
+
/**
* Check if there is a given table in the database.
*
protected $having = [];
public function _run(Result $result) {
+ // Early return if table doesn't exist yet due to pending upgrade
+ $baoName = $this->getBaoName();
+ if (!$baoName::tableHasBeenAdded()) {
+ return;
+ }
+
$this->setDefaultWhereClause();
$this->expandSelectClauseWildcards();
$this->getObjects($result);
$this->assertArrayNotHasKey('api_key', $permissionedContactFields);
}
+ public function testTableHasBeenAdded() {
+ // Hack a different db version
+ CRM_Core_BAO_Domain::getDomain()->version = '5.28.0';
+
+ // Table was added in 5.29
+ $this->assertFalse(CRM_Contact_DAO_RelationshipCache::tableHasBeenAdded());
+
+ // Remove domain version override:
+ CRM_Core_BAO_Domain::version(TRUE);
+
+ $this->assertTrue(CRM_Contact_DAO_RelationshipCache::tableHasBeenAdded());
+ }
+
}