/** * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`. * Cleanup setting schema. * * @param CRM_Queue_TaskContext $ctx * @return bool */ public function migrateSettings(CRM_Queue_TaskContext $ctx) { CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name'); CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name'); CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)'); $domainDao = CRM_Core_DAO::executeQuery('SELECT id, config_backend FROM civicrm_domain'); while ($domainDao->fetch()) { $settings = CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($domainDao->id, $domainDao->config_backend); CRM_Core_Error::debug_var('convertBackendToSettings', array('domainId' => $domainDao->id, 'backend' => $domainDao->config_backend, 'settings' => $settings)); foreach ($settings as $name => $value) { $rowParams = array(1 => array($domainDao->id, 'Positive'), 2 => array($name, 'String'), 3 => array(serialize($value), 'String')); $settingId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2', $rowParams); if (!$settingId) { CRM_Core_DAO::executeQuery('INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)', $rowParams); } } } // TODO Should drop config_backend, but keeping it during alpha/beta cycle in case we miss something. // CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend'); return TRUE; }
/** * Load all explicit settings that apply to this domain or contact. * * @return $this */ public function loadValues() { // Note: Don't use DAO child classes. They require fields() which require // translations -- which are keyed off settings! $this->values = array(); $this->combined = NULL; // Ordinarily, we just load values from `civicrm_setting`. But upgrades require care. // In v4.0 and earlier, all values were stored in `civicrm_domain.config_backend`. // In v4.1-v4.6, values were split between `civicrm_domain` and `civicrm_setting`. // In v4.7+, all values are stored in `civicrm_setting`. // Whenever a value is available in civicrm_setting, it will take precedence. $isUpgradeMode = \CRM_Core_Config::isUpgradeMode(); if ($isUpgradeMode && empty($this->contactId) && \CRM_Core_DAO::checkFieldExists('civicrm_domain', 'config_backend', FALSE)) { $config_backend = \CRM_Core_DAO::singleValueQuery('SELECT config_backend FROM civicrm_domain WHERE id = %1', array(1 => array($this->domainId, 'Positive'))); $oldSettings = \CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($this->domainId, $config_backend); \CRM_Utils_Array::extend($this->values, $oldSettings); } // Normal case. Aside: Short-circuit prevents unnecessary query. if (!$isUpgradeMode || \CRM_Core_DAO::checkTableExists('civicrm_setting')) { $dao = \CRM_Core_DAO::executeQuery($this->createQuery()->toSQL()); while ($dao->fetch()) { $this->values[$dao->name] = $dao->value !== NULL ? unserialize($dao->value) : NULL; } } return $this; }
/** * Migrate any last remaining options from `civicrm_domain.config_backend` to `civicrm_setting`. * Cleanup setting schema. * * @param CRM_Queue_TaskContext $ctx * @return bool */ public function migrateSettings(CRM_Queue_TaskContext $ctx) { // Tip: If there are problems with adding the new uniqueness index, try inspecting: // SELECT name, domain_id, contact_id, count(*) AS dupes FROM civicrm_setting cs GROUP BY name, domain_id, contact_id HAVING dupes > 1; // Nav records are expendable. https://forum.civicrm.org/index.php?topic=36933.0 CRM_Core_DAO::executeQuery('DELETE FROM civicrm_setting WHERE contact_id IS NOT NULL AND name = "navigation"'); CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP INDEX index_group_name'); CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting DROP COLUMN group_name'); CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_setting ADD UNIQUE INDEX index_domain_contact_name (domain_id, contact_id, name)'); $domainDao = CRM_Core_DAO::executeQuery('SELECT id, config_backend FROM civicrm_domain'); while ($domainDao->fetch()) { $settings = CRM_Upgrade_Incremental_php_FourSeven::convertBackendToSettings($domainDao->id, $domainDao->config_backend); CRM_Core_Error::debug_var('convertBackendToSettings', array('domainId' => $domainDao->id, 'backend' => $domainDao->config_backend, 'settings' => $settings)); foreach ($settings as $name => $value) { $rowParams = array(1 => array($domainDao->id, 'Positive'), 2 => array($name, 'String'), 3 => array(serialize($value), 'String')); $settingId = CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_setting WHERE domain_id = %1 AND name = %2', $rowParams); if (!$settingId) { CRM_Core_DAO::executeQuery('INSERT INTO civicrm_setting (domain_id, name, value, is_domain) VALUES (%1,%2,%3,1)', $rowParams); } } } CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_domain DROP COLUMN config_backend'); return TRUE; }