Skip to content

Commit

Permalink
Add unique index to DedupeRuleGroup.name field
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Aug 25, 2022
1 parent 0339ce3 commit 9135b37
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
26 changes: 8 additions & 18 deletions CRM/Contact/Form/DedupeRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,14 @@ public function postProcess() {
CRM_Core_DAO::executeQuery($query, $queryParams);
}

$rgDao = new CRM_Dedupe_DAO_DedupeRuleGroup();
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();
}
$rgDao = CRM_Dedupe_BAO_DedupeRuleGroup::writeRecord([
'id' => $this->_rgid,
'contact_type' => $this->_contactType,
'title' => $values['title'],
'is_reserved' => $values['is_reserved'] ?? FALSE,
'used' => $values['used'],
'threshold' => $values['threshold'],
]);

// lets skip updating of fields for reserved dedupe group
if (!empty($this->_defaults['is_reserved'])) {
Expand Down
6 changes: 2 additions & 4 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ public static function writeRecord(array $record): CRM_Core_DAO {
$values = array_intersect_key($record, $fields);
$instance->copyValues($values);
if (empty($values[$idField]) && array_key_exists('name', $fields) && empty($values['name'])) {
$instance->makeNameFromLabel(!empty($fields['name']['required']));
$instance->makeNameFromLabel();
}
$instance->save();

Expand Down Expand Up @@ -3326,10 +3326,8 @@ public static function getEntityIcon(string $entityName, int $entityId) {
* create a unique, clean name derived from the label.
*
* Note: this function does nothing unless a unique index exists for "name" column.
*
* @var bool $isRequired
*/
private function makeNameFromLabel(bool $isRequired): void {
private function makeNameFromLabel(): void {
$indexNameWith = NULL;
// Look for a unique index which includes the "name" field
if (method_exists($this, 'indices')) {
Expand Down
24 changes: 17 additions & 7 deletions CRM/Dedupe/DAO/DedupeRuleGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Dedupe/DedupeRuleGroup.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:7bcb6791c8bff662caa873128c101da8)
* (GenCodeChecksum:dc8957103d3accc4eb60eb94a3ee2282)
*/

/**
Expand Down Expand Up @@ -67,10 +67,10 @@ class CRM_Dedupe_DAO_DedupeRuleGroup extends CRM_Core_DAO {
public $used;

/**
* Name of the rule group
* Unique name of rule group
*
* @var string|null
* (SQL type: varchar(64))
* (SQL type: varchar(255))
* Note that values will be retrieved from the database as a string.
*/
public $name;
Expand Down Expand Up @@ -200,9 +200,9 @@ public static function &fields() {
'name' => 'name',
'type' => CRM_Utils_Type::T_STRING,
'title' => ts('Name'),
'description' => ts('Name of the rule group'),
'maxlength' => 64,
'size' => CRM_Utils_Type::BIG,
'description' => ts('Unique name of rule group'),
'maxlength' => 255,
'size' => CRM_Utils_Type::HUGE,
'where' => 'civicrm_dedupe_rule_group.name',
'table_name' => 'civicrm_dedupe_rule_group',
'entity' => 'DedupeRuleGroup',
Expand Down Expand Up @@ -313,7 +313,17 @@ public static function &export($prefix = FALSE) {
* @return array
*/
public static function indices($localize = TRUE) {
$indices = [];
$indices = [
'UI_name' => [
'name' => 'UI_name',
'field' => [
0 => 'name',
],
'localizable' => FALSE,
'unique' => TRUE,
'sig' => 'civicrm_dedupe_rule_group::1::name',
],
];
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
}

Expand Down
13 changes: 13 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveFiftyFour.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NU
public function upgrade_5_54_alpha1($rev): void {
$this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
$this->addTask('Add "created_id" column to "civicrm_participant"', 'addCreatedIDColumnToParticipant');
$this->addTask('Increase field length of civicrm_dedupe_rule_group.name', 'alterDedupeRuleGroupName');
$this->addTask('Add index civicrm_dedupe_rule_group.UI_name', 'addIndex', 'civicrm_dedupe_rule_group', 'name', 'UI');
$this->addTask('Install Elavon Payment Processor Extension as needed', 'installElavonPaymentProcessorExtension');
}

Expand All @@ -50,6 +52,17 @@ public static function addCreatedIDColumnToParticipant($ctx): bool {
return TRUE;
}

/**
* @param \CRM_Queue_TaskContext $ctx
*
* @return bool
*/
public static function alterDedupeRuleGroupName(CRM_Queue_TaskContext $ctx) {
CRM_Core_DAO::executeQuery("ALTER TABLE `civicrm_dedupe_rule_group` CHANGE COLUMN `name` `name` varchar(255) COMMENT 'Unique name of rule group'", [], TRUE, NULL, FALSE, FALSE);
CRM_Core_DAO::executeQuery("UPDATE `civicrm_dedupe_rule_group` g1, `civicrm_dedupe_rule_group` g2 SET g1.name = CONCAT(g1.name, '_', g1.id) WHERE g1.name = g2.name AND g1.id > g2.id", [], TRUE, NULL, FALSE, FALSE);
return TRUE;
}

/**
* @param CRM_Queue_TaskContext $ctx
* @return bool
Expand Down
10 changes: 8 additions & 2 deletions xml/schema/Dedupe/DedupeRuleGroup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,16 @@
<name>name</name>
<title>Name</title>
<type>varchar</type>
<length>64</length>
<comment>Name of the rule group</comment>
<length>255</length>
<comment>Unique name of rule group</comment>
<add>2.1</add>
</field>
<index>
<name>UI_name</name>
<fieldName>name</fieldName>
<unique>true</unique>
<add>5.54</add>
</index>
<field>
<name>title</name>
<title>Title</title>
Expand Down

0 comments on commit 9135b37

Please sign in to comment.