/** * Test creating an index. * * We want to be sure it creates an index and exits gracefully if the index * already exists. */ public function testCreateIndex() { $tables = array('civicrm_uf_join' => array('weight')); CRM_Core_BAO_SchemaHandler::createIndexes($tables); CRM_Core_BAO_SchemaHandler::createIndexes($tables); $dao = CRM_Core_DAO::executeQuery("SHOW INDEX FROM civicrm_uf_join"); $count = 0; while ($dao->fetch()) { if ($dao->Column_name == 'weight') { $count++; CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_uf_join DROP INDEX " . $dao->Key_name); } } $this->assertEquals(1, $count); }
/** * Function to process the form * * @access public * @return None */ public function postProcess() { $values = $this->exportValues(); $rgDao =& new CRM_Dedupe_DAO_RuleGroup(); if ($this->_action & CRM_Core_Action::UPDATE) { $rgDao->id = $this->_rgid; } $rgDao->threshold = $values['threshold']; $rgDao->name = $values['name']; $rgDao->level = $values['level']; $rgDao->contact_type = $this->_contactType; $rgDao->save(); $ruleDao =& new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->delete(); $ruleDao->free(); $tables = array(); for ($count = 0; $count < self::RULES_COUNT; $count++) { if (!CRM_Utils_Array::value("where_{$count}", $values)) { continue; } list($table, $field) = explode('.', CRM_Utils_Array::value("where_{$count}", $values)); $length = CRM_Utils_Array::value("length_{$count}", $values) ? CRM_Utils_Array::value("length_{$count}", $values) : null; $weight = $values["weight_{$count}"]; if ($table and $field) { $ruleDao =& new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->rule_table = $table; $ruleDao->rule_field = $field; $ruleDao->rule_length = $length; $ruleDao->rule_weight = $weight; $ruleDao->save(); $ruleDao->free(); if (!array_key_exists($table, $tables)) { $tables[$table] = array(); } $tables[$table][] = $field; } } // also create an index for this dedupe rule // CRM-3837 require_once 'CRM/Core/BAO/SchemaHandler.php'; CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index'); }
/** * Process the form submission. * * * @return void */ public function postProcess() { $values = $this->exportValues(); //FIXME: Handle logic to replace is_default column by usage // reset used column to General (since there can only // be one 'Supervised' or 'Unsupervised' rule) if ($values['used'] != 'General') { $query = "\nUPDATE civicrm_dedupe_rule_group\n SET used = 'General'\n WHERE contact_type = %1\n AND used = %2"; $queryParams = array(1 => array($this->_contactType, 'String'), 2 => array($values['used'], 'String')); CRM_Core_DAO::executeQuery($query, $queryParams); } $rgDao = new CRM_Dedupe_DAO_RuleGroup(); if ($this->_action & CRM_Core_Action::UPDATE) { $rgDao->id = $this->_rgid; } $rgDao->title = $values['title']; $rgDao->is_reserved = CRM_Utils_Array::value('is_reserved', $values, FALSE); $rgDao->used = $values['used']; $rgDao->contact_type = $this->_contactType; $rgDao->threshold = $values['threshold']; $rgDao->save(); // make sure name is set only during insert if ($this->_action & CRM_Core_Action::ADD) { // generate name based on title $rgDao->name = CRM_Utils_String::titleToVar($values['title']) . "_{$rgDao->id}"; $rgDao->save(); } // lets skip updating of fields for reserved dedupe group if ($rgDao->is_reserved) { CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success'); return; } $ruleDao = new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->delete(); $ruleDao->free(); $substrLenghts = array(); $tables = array(); $daoObj = new CRM_Core_DAO(); $database = $daoObj->database(); for ($count = 0; $count < self::RULES_COUNT; $count++) { if (empty($values["where_{$count}"])) { continue; } list($table, $field) = explode('.', CRM_Utils_Array::value("where_{$count}", $values)); $length = !empty($values["length_{$count}"]) ? CRM_Utils_Array::value("length_{$count}", $values) : NULL; $weight = $values["weight_{$count}"]; if ($table and $field) { $ruleDao = new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->rule_table = $table; $ruleDao->rule_field = $field; $ruleDao->rule_length = $length; $ruleDao->rule_weight = $weight; $ruleDao->save(); $ruleDao->free(); if (!array_key_exists($table, $tables)) { $tables[$table] = array(); } $tables[$table][] = $field; } // CRM-6245: we must pass table/field/length triples to the createIndexes() call below if ($length) { if (!isset($substrLenghts[$table])) { $substrLenghts[$table] = array(); } //CRM-13417 to avoid fatal error "Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys, 1089" $schemaQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS\n WHERE TABLE_SCHEMA = '{$database}' AND\n TABLE_NAME = '{$table}' AND COLUMN_NAME = '{$field}';"; $dao = CRM_Core_DAO::executeQuery($schemaQuery); if ($dao->fetch()) { // set the length to null for all the fields where prefix length is not supported. eg. int,tinyint,date,enum etc dataTypes. if ($dao->COLUMN_NAME == $field && !in_array($dao->DATA_TYPE, array('char', 'varchar', 'binary', 'varbinary', 'text', 'blob'))) { $length = NULL; } elseif ($dao->COLUMN_NAME == $field && !empty($dao->CHARACTER_MAXIMUM_LENGTH) && $length > $dao->CHARACTER_MAXIMUM_LENGTH) { //set the length to CHARACTER_MAXIMUM_LENGTH in case the length provided by the user is greater than the limit $length = $dao->CHARACTER_MAXIMUM_LENGTH; } } $substrLenghts[$table][$field] = $length; } } // also create an index for this dedupe rule // CRM-3837 CRM_Utils_Hook::dupeQuery($ruleDao, 'dedupeIndexes', $tables); CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index', $substrLenghts); //need to clear cache of deduped contacts //based on the previous rule $cacheKey = "merge {$this->_contactType}_{$this->_rgid}_%"; CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey); CRM_Core_Session::setStatus(ts("The rule '%1' has been saved.", array(1 => $rgDao->title)), ts('Saved'), 'success'); }
/** * Test the drop index if exists function. */ public function testDropIndexExists() { CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_contact', 'index_hash'); $this->assertFalse(CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_contact', 'index_hash')); // Recreate it to clean up after the test. CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contact' => array('hash'))); }
/** * Function to process the form * * @access public * @return None */ public function postProcess() { $values = $this->exportValues(); $isDefault = CRM_Utils_Array::value('is_default', $values, false); // reset defaults if ($isDefault) { $query = "\nUPDATE civicrm_dedupe_rule_group \n SET is_default = 0\n WHERE contact_type = %1 \n AND level = %2"; $queryParams = array(1 => array($this->_contactType, 'String'), 2 => array($values['level'], 'String')); CRM_Core_DAO::executeQuery($query, $queryParams); } $rgDao = new CRM_Dedupe_DAO_RuleGroup(); if ($this->_action & CRM_Core_Action::UPDATE) { $rgDao->id = $this->_rgid; } $rgDao->threshold = $values['threshold']; $rgDao->name = $values['name']; $rgDao->level = $values['level']; $rgDao->contact_type = $this->_contactType; $rgDao->is_default = $isDefault; $rgDao->save(); $ruleDao = new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->delete(); $ruleDao->free(); $substrLenghts = array(); $tables = array(); for ($count = 0; $count < self::RULES_COUNT; $count++) { if (!CRM_Utils_Array::value("where_{$count}", $values)) { continue; } list($table, $field) = explode('.', CRM_Utils_Array::value("where_{$count}", $values)); $length = CRM_Utils_Array::value("length_{$count}", $values) ? CRM_Utils_Array::value("length_{$count}", $values) : null; $weight = $values["weight_{$count}"]; if ($table and $field) { $ruleDao = new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->rule_table = $table; $ruleDao->rule_field = $field; $ruleDao->rule_length = $length; $ruleDao->rule_weight = $weight; $ruleDao->save(); $ruleDao->free(); if (!array_key_exists($table, $tables)) { $tables[$table] = array(); } $tables[$table][] = $field; } // CRM-6245: we must pass table/field/length triples to the createIndexes() call below if ($length) { if (!isset($substrLenghts[$table])) { $substrLenghts[$table] = array(); } $substrLenghts[$table][$field] = $length; } } // also create an index for this dedupe rule // CRM-3837 require_once 'CRM/Core/BAO/SchemaHandler.php'; CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index', $substrLenghts); }
/** * CRM-17999 Add index to civicrm_contribution.source. * * @param \CRM_Queue_TaskContext $ctx * * @return bool */ public function addIndexContributionSource(CRM_Queue_TaskContext $ctx) { CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contribution' => array('source'))); return TRUE; }
/** * CRM-18124 Add index to civicrm_contribution.total_amount. * * Note that I made this a combined index with receive_date because the issue included * both criteria and they seemed likely to be used in conjunction to me in other cases. * * @param \CRM_Queue_TaskContext $ctx * * @return bool */ public function addIndexContributionAmount(CRM_Queue_TaskContext $ctx) { CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contribution' => array(array('total_amount', 'receive_date')))); return TRUE; }
/** * CRM-17775 Add correct index for table civicrm_financial_item. * * Note that the entity ID should always precede the entity_table as * it is more unique. This is better for performance and does not cause fallback * to no index if table it omitted. * * @return bool */ public function addCombinedIndexFinancialItemEntityIDEntityType() { CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'UI_id'); CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_financial_item', 'IX_Entity'); CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_financial_item' => array(array('entity_id', 'entity_table')))); return TRUE; }
/** * CRM-17882 Add index to civicrm_contribution.credit_note_id. * * @param \CRM_Queue_TaskContext $ctx * * @return bool */ public function addIndexContributionCreditNoteID(CRM_Queue_TaskContext $ctx) { $tables = array('civicrm_contribution' => array('creditnote_id')); CRM_Core_BAO_SchemaHandler::createIndexes($tables); return TRUE; }
/** * CRM-19100 - Alter Index and Type for Image URL * @return bool */ public static function alterIndexAndTypeForImageURL() { $length = array(); CRM_Core_BAO_SchemaHandler::dropIndexIfExists('civicrm_contact', 'index_image_url'); CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_contact` CHANGE `image_URL` `image_URL` TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT 'optional URL for preferred image (photo, logo, etc.) to display for this contact.'"); $length['civicrm_contact']['image_URL'] = 128; CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contact' => array('image_URL')), 'index', $length); return TRUE; }
/** * Function to process the form * * @access public * * @return None */ public function postProcess() { $values = $this->exportValues(); $isDefault = CRM_Utils_Array::value('is_default', $values, FALSE); // reset defaults if ($isDefault) { $query = "\nUPDATE civicrm_dedupe_rule_group \n SET is_default = 0\n WHERE contact_type = %1 \n AND level = %2"; $queryParams = array(1 => array($this->_contactType, 'String'), 2 => array($values['level'], 'String')); CRM_Core_DAO::executeQuery($query, $queryParams); } $rgDao = new CRM_Dedupe_DAO_RuleGroup(); if ($this->_action & CRM_Core_Action::UPDATE) { $rgDao->id = $this->_rgid; } $rgDao->title = $values['title']; $rgDao->is_reserved = CRM_Utils_Array::value('is_reserved', $values, FALSE); $rgDao->is_default = $isDefault; $rgDao->level = $values['level']; $rgDao->contact_type = $this->_contactType; $rgDao->threshold = $values['threshold']; $rgDao->save(); // make sure name is set only during insert if ($this->_action & CRM_Core_Action::ADD) { // generate name based on title $rgDao->name = CRM_Utils_String::titleToVar($values['title']) . "_{$rgDao->id}"; $rgDao->save(); } // lets skip updating of fields for reserved dedupe group if ($rgDao->is_reserved) { CRM_Core_Session::setStatus(ts('The rule \'%1\' has been saved.', array(1 => $rgDao->title))); return; } $ruleDao = new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->delete(); $ruleDao->free(); $substrLenghts = array(); $tables = array(); for ($count = 0; $count < self::RULES_COUNT; $count++) { if (!CRM_Utils_Array::value("where_{$count}", $values)) { continue; } list($table, $field) = explode('.', CRM_Utils_Array::value("where_{$count}", $values)); $length = CRM_Utils_Array::value("length_{$count}", $values) ? CRM_Utils_Array::value("length_{$count}", $values) : NULL; $weight = $values["weight_{$count}"]; if ($table and $field) { $ruleDao = new CRM_Dedupe_DAO_Rule(); $ruleDao->dedupe_rule_group_id = $rgDao->id; $ruleDao->rule_table = $table; $ruleDao->rule_field = $field; $ruleDao->rule_length = $length; $ruleDao->rule_weight = $weight; $ruleDao->save(); $ruleDao->free(); if (!array_key_exists($table, $tables)) { $tables[$table] = array(); } $tables[$table][] = $field; } // CRM-6245: we must pass table/field/length triples to the createIndexes() call below if ($length) { if (!isset($substrLenghts[$table])) { $substrLenghts[$table] = array(); } $substrLenghts[$table][$field] = $length; } } // also create an index for this dedupe rule // CRM-3837 CRM_Core_BAO_SchemaHandler::createIndexes($tables, 'dedupe_index', $substrLenghts); //need to clear cache of deduped contacts //based on the previous rule $cacheKey = "merge {$this->_contactType}_{$this->_rgid}_%"; CRM_Core_BAO_PrevNextCache::deleteItem(NULL, $cacheKey); CRM_Core_Session::setStatus(ts('The rule \'%1\' has been saved.', array(1 => $rgDao->title))); }