From: eileen Date: Wed, 29 Jun 2016 02:46:56 +0000 (+1200) Subject: CRM-19027 clean up use of slow query to get tables. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=3fa9688a5;p=civicrm-core.git CRM-19027 clean up use of slow query to get tables. This is mainly targetting the MyISam DB check - but I also cleaned up the function that gets the code to render the list of tables on the screen civicrm/admin/setting/uf?reset=1 (on drupal when views is enabled --- diff --git a/CRM/Admin/Form/Setting/UF.php b/CRM/Admin/Form/Setting/UF.php index 7b014d3543..029d77eb92 100644 --- a/CRM/Admin/Form/Setting/UF.php +++ b/CRM/Admin/Form/Setting/UF.php @@ -79,14 +79,14 @@ class CRM_Admin_Form_Setting_UF extends CRM_Admin_Form_Setting { ) ) { $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);"; diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index a168da8b3e..70f7245ffb 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -766,48 +766,22 @@ LIKE %1 } /** - * 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; @@ -819,12 +793,25 @@ LIKE %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(); } /** diff --git a/tests/phpunit/CRM/Core/DAOTest.php b/tests/phpunit/CRM/Core/DAOTest.php index c2f67a04c7..be89d34efc 100644 --- a/tests/phpunit/CRM/Core/DAOTest.php +++ b/tests/phpunit/CRM/Core/DAOTest.php @@ -249,4 +249,14 @@ class CRM_Core_DAOTest extends CiviUnitTestCase { } } + /** + * 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'); + } + }