From 6440c50da91f265a9326bcb0ea20e4ed1f193bb8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 6 Aug 2021 19:51:05 +1200 Subject: [PATCH] Views db cleanup (alternative) I took a look at https://github.com/civicrm/civicrm-core/pull/20682 and it seemed clear that in it's current state it would fix one CMS (d9) & break another (d7). This is an alternate that will hopefully be non-breaky --- CRM/Admin/Form/Setting/UF.php | 12 +++--------- CRM/Utils/System/Backdrop.php | 12 ++++++++++++ CRM/Utils/System/Base.php | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CRM/Admin/Form/Setting/UF.php b/CRM/Admin/Form/Setting/UF.php index 6ffda9f229..16aedbc90f 100644 --- a/CRM/Admin/Form/Setting/UF.php +++ b/CRM/Admin/Form/Setting/UF.php @@ -67,8 +67,8 @@ class CRM_Admin_Form_Setting_UF extends CRM_Admin_Form_Setting { $config->dsn != $config->userFrameworkDSN || !empty($drupal_prefix) ) ) { - $dsn = CRM_Utils_SQL::autoSwitchDSN($config->dsn); - $dsnArray = DB::parseDSN($dsn); + + $dsnArray = DB::parseDSN(CRM_Utils_SQL::autoSwitchDSN($config->dsn)); $tableNames = CRM_Core_DAO::getTableNames(); asort($tableNames); $tablePrefixes = '$databases[\'default\'][\'default\'][\'prefix\']= ['; @@ -77,13 +77,7 @@ class CRM_Admin_Form_Setting_UF extends CRM_Admin_Form_Setting { } // add default prefix: the drupal database prefix $tablePrefixes .= "\n 'default' => '$drupal_prefix',"; - $prefix = ""; - if ($config->dsn != $config->userFrameworkDSN) { - $prefix = "`{$dsnArray['database']}`."; - if ($config->userFramework === 'Backdrop') { - $prefix = "{$dsnArray['database']}."; - } - } + $prefix = $config->userSystem->getCRMDatabasePrefix(); foreach ($tableNames as $tableName) { $tablePrefixes .= "\n '" . str_pad($tableName . "'", 41) . " => '{$prefix}',"; } diff --git a/CRM/Utils/System/Backdrop.php b/CRM/Utils/System/Backdrop.php index 3d72779f15..3fb3b6cc77 100644 --- a/CRM/Utils/System/Backdrop.php +++ b/CRM/Utils/System/Backdrop.php @@ -1056,4 +1056,16 @@ AND u.status = 1 return ['ufAccessURL' => url('admin/config/people/permissions')]; } + /** + * Get the CRM database as a 'prefix'. + * + * This returns a string that can be prepended to a query to include a CRM table. + * + * However, this string should contain backticks, or not, in accordance with the + * CMS's drupal views expectations, if any. + */ + public function getCRMDatabasePrefix(): string { + return str_replace(parent::getCRMDatabasePrefix(), '`', ''); + } + } diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index e08bad2c06..933a952a59 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1051,6 +1051,23 @@ abstract class CRM_Utils_System_Base { return []; } + /** + * Get the CRM database as a 'prefix'. + * + * This returns a string that can be prepended to a query to include a CRM table. + * + * However, this string should contain backticks, or not, in accordance with the + * CMS's drupal views expectations, if any. + */ + public function getCRMDatabasePrefix(): string { + $crmDatabase = DB::parseDSN(CRM_Core_Config::singleton()->dsn)['database']; + $cmsDatabase = DB::parseDSN(CRM_Core_Config::singleton()->userFrameworkDSN)['database']; + if ($crmDatabase === $cmsDatabase) { + return ''; + } + return "`$crmDatabase`."; + } + /** * Invalidates the cache of dynamic routes and forces a rebuild. */ -- 2.25.1