Skip to content

Commit

Permalink
Merge pull request #8636 from eileenmcnaughton/slow_query
Browse files Browse the repository at this point in the history
CRM-19027 clean up use of slow query to get tables.
  • Loading branch information
eileenmcnaughton authored Jul 14, 2016
2 parents 178cf31 + 3fa9688 commit 679e67f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 45 deletions.
4 changes: 2 additions & 2 deletions CRM/Admin/Form/Setting/UF.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ function_exists('module_exists') &&
)
) {
$dsnArray = DB::parseDSN($config->dsn);
$tableNames = CRM_Core_DAO::GetStorageValues(NULL, 0, 'Name');
$tableNames = CRM_Core_DAO::getTableNames();
$tablePrefixes = '$databases[\'default\'][\'default\'][\'prefix\']= array(';
$tablePrefixes .= "\n 'default' => '$drupal_prefix',"; // add default prefix: the drupal database prefix
$prefix = "";
if ($config->dsn != $config->userFrameworkDSN) {
$prefix = "`{$dsnArray['database']}`.";
}
foreach ($tableNames as $tableName => $value) {
foreach ($tableNames as $tableName) {
$tablePrefixes .= "\n '" . str_pad($tableName . "'", 41) . " => '{$prefix}',";
}
$tablePrefixes .= "\n);";
Expand Down
73 changes: 30 additions & 43 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,48 +766,22 @@ public static function checkFieldExists($tableName, $columnName, $i18nRewrite =
}

/**
* Returns the storage engine used by given table-name(optional).
* Otherwise scans all the tables and return an array of all the
* distinct storage engines being used.
*
* @param string $tableName
*
* @param int $maxTablesToCheck
* @param string $fieldName
* Scans all the tables using a slow query and table name.
*
* @return array
*/
public static function getStorageValues($tableName = NULL, $maxTablesToCheck = 10, $fieldName = 'Engine') {
$values = array();
$query = "SHOW TABLE STATUS LIKE %1";

$params = array();
public static function getTableNames() {
$dao = CRM_Core_DAO::executeQuery(
"SELECT TABLE_NAME
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = '" . CRM_Core_DAO::getDatabaseName() . "'
AND TABLE_NAME LIKE 'civicrm_%'
AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
AND TABLE_NAME NOT LIKE '%temp'
");

if (isset($tableName)) {
$params = array(1 => array($tableName, 'String'));
}
else {
$params = array(1 => array('civicrm_%', 'String'));
}

$dao = CRM_Core_DAO::executeQuery($query, $params);

$count = 0;
while ($dao->fetch()) {
if (isset($values[$dao->$fieldName]) ||
// ignore import and other temp tables
strpos($dao->Name, 'civicrm_import_job_') !== FALSE ||
strpos($dao->Name, '_temp') !== FALSE
) {
continue;
}
$values[$dao->$fieldName] = 1;
$count++;
if ($maxTablesToCheck &&
$count >= $maxTablesToCheck
) {
break;
}
$values[] = $dao->TABLE_NAME;
}
$dao->free();
return $values;
Expand All @@ -819,12 +793,25 @@ public static function getStorageValues($tableName = NULL, $maxTablesToCheck = 1
* @return bool
*/
public static function isDBMyISAM($maxTablesToCheck = 10) {
// show error if any of the tables, use 'MyISAM' storage engine.
$engines = self::getStorageValues(NULL, $maxTablesToCheck);
if (array_key_exists('MyISAM', $engines)) {
return TRUE;
}
return FALSE;
return CRM_Core_DAO::singleValueQuery(
"SELECT count(*)
FROM information_schema.TABLES
WHERE ENGINE = 'MyISAM'
AND TABLE_SCHEMA = '" . CRM_Core_DAO::getDatabaseName() . "'
AND TABLE_NAME LIKE 'civicrm_%'
AND TABLE_NAME NOT LIKE 'civicrm_import_job_%'
AND TABLE_NAME NOT LIKE '%temp'
");
}

/**
* Get the name of the CiviCRM database.
*
* @return string
*/
public static function getDatabaseName() {
$daoObj = new CRM_Core_DAO();
return $daoObj->database();
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/phpunit/CRM/Core/DAOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,14 @@ public function testRequireValidDBName() {
}
}

/**
* Test the function designed to find myIsam tables.
*/
public function testMyISAMCheck() {
$this->assertEquals(0, CRM_Core_DAO::isDBMyISAM());
CRM_Core_DAO::executeQuery('CREATE TABLE civicrm_my_isam (`id` int(10) unsigned NOT NULL) ENGINE = MyISAM');
$this->assertEquals(1, CRM_Core_DAO::isDBMyISAM());
CRM_Core_DAO::executeQuery('DROP TABLE civicrm_my_isam');
}

}

0 comments on commit 679e67f

Please sign in to comment.