Skip to content

Commit

Permalink
Merge pull request #22675 from colemanw/customGroupNameIndex
Browse files Browse the repository at this point in the history
CustomGroup - Ensure 'name' is always unique
  • Loading branch information
colemanw authored Feb 2, 2022
2 parents 8b74e1e + 79b6ac2 commit 97bd997
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
9 changes: 4 additions & 5 deletions CRM/Core/DAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/CustomGroup.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:0c41f19103b41d2f82bcd3b60aaf2165)
* (GenCodeChecksum:979bca796163b9af253a7860d9d4a01d)
*/

/**
Expand Down Expand Up @@ -698,15 +698,14 @@ public static function indices($localize = TRUE) {
'unique' => TRUE,
'sig' => 'civicrm_custom_group::1::title::extends',
],
'UI_name_extends' => [
'name' => 'UI_name_extends',
'UI_name' => [
'name' => 'UI_name',
'field' => [
0 => 'name',
1 => 'extends',
],
'localizable' => FALSE,
'unique' => TRUE,
'sig' => 'civicrm_custom_group::1::name::extends',
'sig' => 'civicrm_custom_group::1::name',
],
];
return ($localize && !empty($indices)) ? CRM_Core_DAO_AllCoreTables::multilingualize(__CLASS__, $indices) : $indices;
Expand Down
9 changes: 9 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveFortySeven.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public function setPreUpgradeMessage(&$preUpgradeMessage, $rev, $currentVer = NU
$preUpgradeMessage .= '<p>' . ts('The contact field preferred mail format is being phased out. Modern email clients can handle receiving both formats so CiviCRM is moving towards always sending both and the field will be incrementally removed from the UI.')
. ' <a href="https://lab.civicrm.org/dev/core/-/issues/2866">' . ts('See the issue for more detail') . '</a></p>';
}
// Check for custom groups with duplicate names
$dupes = CRM_Core_DAO::singleValueQuery('SELECT COUNT(g1.id) FROM civicrm_custom_group g1, civicrm_custom_group g2 WHERE g1.name = g2.name AND g1.id > g2.id');
if ($dupes) {
$preUpgradeMessage .= '<p>' .
ts('Your system has custom field groups with duplicate internal names. To ensure data integrity, the internal names will be automatically changed; user-facing titles will not be affected. Please review any custom code you may be using which relies on custom field group names.') .
'</p>';
}
}
}

Expand All @@ -63,6 +70,8 @@ public function upgrade_5_47_alpha1($rev): void {
'civicrm_event', 'event_tz', "text NULL DEFAULT NULL COMMENT 'Event\'s native time zone'"
);
$this->addTask('core-issue#2122 - Set the timezone to the default for existing Events', 'setEventTZDefault');
$this->addTask('Drop CustomGroup UI_name_extends index', 'dropIndex', 'civicrm_custom_group', 'UI_name_extends');
$this->addTask('Add CustomGroup UI_name index', 'addIndex', 'civicrm_custom_group', ['name'], 'UI');
}

/**
Expand Down
3 changes: 3 additions & 0 deletions CRM/Upgrade/Incremental/sql/5.47.alpha1.mysql.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ ALTER TABLE `civicrm_event`
MODIFY COLUMN `end_date` timestamp NULL DEFAULT NULL COMMENT 'Date and time that event ends. May be NULL if no defined end date/time',
MODIFY COLUMN `registration_start_date` timestamp NULL DEFAULT NULL COMMENT 'Date and time that online registration starts.',
MODIFY COLUMN `registration_end_date` timestamp NULL DEFAULT NULL COMMENT 'Date and time that online registration ends.';

{* Ensure CustomGroup.name is unique *}
UPDATE civicrm_custom_group g1, civicrm_custom_group g2 SET g1.name = CONCAT(g1.name, '_1') WHERE g1.name = g2.name AND g1.id > g2.id;
4 changes: 2 additions & 2 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ public function testCreateWithCustom(): void {
* @throws \CRM_Core_Exception
*/
public function testCreateGetFieldsWithCustom(): void {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
$idsContact = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTest.php');
$ids = $this->entityCustomGroupWithSingleFieldCreate('ContributionCustomFields', __FILE__);
$idsContact = $this->entityCustomGroupWithSingleFieldCreate('ContactCustomFields', 'ContactTest.php');
$result = $this->callAPISuccess('Contribution', 'getfields', []);
$this->assertArrayHasKey('custom_' . $ids['custom_field_id'], $result['values']);
$this->assertArrayNotHasKey('custom_' . $idsContact['custom_field_id'], $result['values']);
Expand Down
5 changes: 2 additions & 3 deletions xml/schema/Core/CustomGroup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,10 @@
<add>2.1</add>
</index>
<index>
<name>UI_name_extends</name>
<name>UI_name</name>
<fieldName>name</fieldName>
<fieldName>extends</fieldName>
<unique>true</unique>
<add>2.1</add>
<add>5.47</add>
</index>
<field>
<name>created_id</name>
Expand Down

0 comments on commit 97bd997

Please sign in to comment.