From 0383ef73a6f2dbbea4d33bc5804cfa4b2d860852 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Tue, 24 Mar 2015 17:47:16 +1300 Subject: [PATCH] CRM-16167 fix up cache clear to include report temp tables from all paths --- CRM/Core/BAO/Cache.php | 25 +----------------------- CRM/Core/Config.php | 44 +++++++++++++++++++++++++++--------------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/CRM/Core/BAO/Cache.php b/CRM/Core/BAO/Cache.php index 3e9fb559ce..ee164a9f69 100644 --- a/CRM/Core/BAO/Cache.php +++ b/CRM/Core/BAO/Cache.php @@ -325,30 +325,7 @@ class CRM_Core_BAO_Cache extends CRM_Core_DAO_Cache { } if ($table) { - // also delete all the action temp tables - // that were created the same interval ago - $dao = new CRM_Core_DAO(); - $query = " -SELECT TABLE_NAME as tableName -FROM INFORMATION_SCHEMA.TABLES -WHERE TABLE_SCHEMA = %1 -AND ( TABLE_NAME LIKE 'civicrm_task_action_temp_%' - OR TABLE_NAME LIKE 'civicrm_export_temp_%' - OR TABLE_NAME LIKE 'civicrm_import_job_%' ) -AND CREATE_TIME < date_sub( NOW( ), INTERVAL $timeIntervalDays day ) -"; - - $params = array(1 => array($dao->database(), 'String')); - $tableDAO = CRM_Core_DAO::executeQuery($query, $params); - $tables = array(); - while ($tableDAO->fetch()) { - $tables[] = $tableDAO->tableName; - } - if (!empty($tables)) { - $table = implode(',', $tables); - // drop leftover temporary tables - CRM_Core_DAO::executeQuery("DROP TABLE $table"); - } + CRM_Core_Config::clearTempTables($timeIntervalDays. ' day'); } if ($session) { diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index 17ea00eece..9237db624a 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -803,24 +803,36 @@ class CRM_Core_Config extends CRM_Core_Config_Variables { /** * Clear leftover temporary tables. + * + * This is called on upgrade, during tests and site move, from the cron and via clear caches in the UI. + * + * Currently the UI clear caches does not pass a time interval - which may need review as it does risk + * ripping the tables out from underneath a current action. This was considered but + * out-of-scope for CRM-16167 + * + * @param string|bool $timeInterval + * Optional time interval for mysql date function.g '2 day'. This can be used to prevent + * tables created recently from being deleted. */ - public static function clearTempTables() { - // CRM-5645 - $dao = CRM_Core_DAO::executeQuery("SELECT DATABASE();"); + public static function clearTempTables($timeInterval = FALSE) { + + $dao = new CRM_Core_DAO(); $query = " -SELECT TABLE_NAME as tableName -FROM INFORMATION_SCHEMA.TABLES -WHERE TABLE_SCHEMA = %1 -AND - ( TABLE_NAME LIKE 'civicrm_import_job_%' - OR TABLE_NAME LIKE 'civicrm_export_temp%' - OR TABLE_NAME LIKE 'civicrm_task_action_temp%' - OR TABLE_NAME LIKE 'civicrm_report_temp%' - ) -"; - - $params = array(1 => array($dao->database(), 'String')); - $tableDAO = CRM_Core_DAO::executeQuery($query, $params); + SELECT TABLE_NAME as tableName + FROM INFORMATION_SCHEMA.TABLES + WHERE TABLE_SCHEMA = %1 + AND ( + TABLE_NAME LIKE 'civicrm_import_job_%' + OR TABLE_NAME LIKE 'civicrm_export_temp%' + OR TABLE_NAME LIKE 'civicrm_task_action_temp%' + OR TABLE_NAME LIKE 'civicrm_report_temp%' + ) + "; + if ($timeInterval) { + $query .= " AND CREATE_TIME < DATE_SUB(NOW(), INTERVAL {$timeInterval})"; + } + + $tableDAO = CRM_Core_DAO::executeQuery($query, array(1 => array($dao->database(), 'String'))); $tables = array(); while ($tableDAO->fetch()) { $tables[] = $tableDAO->tableName; -- 2.25.1