Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Schema calculation of usePrefix to cope with rpow: #20471

Merged
merged 1 commit into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions CRM/Logging/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ class CRM_Logging_Schema {
private $logs = [];
private $tables = [];

/**
* Name of the database where the logging data is stored.
*
* @var string
*/
private $db;

private $useDBPrefix = TRUE;

private $reports = [
Expand Down Expand Up @@ -113,8 +119,7 @@ public static function onToggle($oldValue, $newValue, $metadata) {
* Populate $this->tables and $this->logs with current db state.
*/
public function __construct() {
$dao = new CRM_Contact_DAO_Contact();
$civiDBName = $dao->_database;
$civiDBName = $this->getCiviCRMDatabaseName();

$dao = CRM_Core_DAO::executeQuery("
SELECT TABLE_NAME
Expand Down Expand Up @@ -157,17 +162,8 @@ public function __construct() {
$this->tables = array_keys($this->logTableSpec);
$nonStandardTableNameString = $this->getNonStandardTableNameFilterString();

if (defined('CIVICRM_LOGGING_DSN')) {
$dsn = CRM_Utils_SQL::autoSwitchDSN(CIVICRM_LOGGING_DSN);
$dsn = DB::parseDSN($dsn);
$this->useDBPrefix = (CIVICRM_LOGGING_DSN != CIVICRM_DSN);
}
else {
$dsn = CRM_Utils_SQL::autoSwitchDSN(CIVICRM_DSN);
$dsn = DB::parseDSN($dsn);
$this->useDBPrefix = FALSE;
}
$this->db = $dsn['database'];
$this->db = $this->getDatabaseNameFromDSN(defined('CIVICRM_LOGGING_DSN') ? CIVICRM_LOGGING_DSN : CIVICRM_DSN);
$this->useDBPrefix = $this->db !== $civiDBName;

$dao = CRM_Core_DAO::executeQuery("
SELECT TABLE_NAME
Expand Down Expand Up @@ -1064,4 +1060,31 @@ public function getMissingLogTables() {
return [];
}

/**
* Get the name of the database from the dsn string.
*
* @param string $dsnString
*
* @return string
*/
protected function getDatabaseNameFromDSN($dsnString): string {
$dsn = CRM_Utils_SQL::autoSwitchDSN($dsnString);
$dsn = DB::parseDSN($dsn);
return $dsn['database'];
}

/**
* Get the database name for the CiviCRM connection.
*
* Note that we want to get it from the database connection,
* not the dsn, because there is at least one extension
* (https://github.com/totten/rpow) that 'meddles' with
* the DSN string.
*
* @return string
*/
protected function getCiviCRMDatabaseName(): string {
return (new CRM_Contact_DAO_Contact())->_database;
}

}