CustomGroup - Ensure 'name' is always unique
authorColeman Watts <coleman@civicrm.org>
Tue, 1 Feb 2022 20:03:37 +0000 (15:03 -0500)
committerColeman Watts <coleman@civicrm.org>
Wed, 2 Feb 2022 19:07:38 +0000 (14:07 -0500)
Previously, unique 'name' was only enforced in tandem with 'extends'
Now it is required to be unique unconditionally.

CRM/Core/DAO/CustomGroup.php
CRM/Upgrade/Incremental/php/FiveFortySeven.php
CRM/Upgrade/Incremental/sql/5.47.alpha1.mysql.tpl
tests/phpunit/api/v3/ContributionTest.php
xml/schema/Core/CustomGroup.xml

index 6bb2ce18e0df8373910f260e20bd02ccdc38d0df..41f78998d694180f4e455acf7dc92bc74eea4916 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Core/CustomGroup.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:0c41f19103b41d2f82bcd3b60aaf2165)
+ * (GenCodeChecksum:979bca796163b9af253a7860d9d4a01d)
  */
 
 /**
@@ -698,15 +698,14 @@ class CRM_Core_DAO_CustomGroup extends CRM_Core_DAO {
         '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;
index 398e91d87e233ccd296547ce1b05d0135ce2f0d2..1f5fe04ea315b60bfa3a4ea3332415ee742238f6 100644 (file)
@@ -39,6 +39,13 @@ class CRM_Upgrade_Incremental_php_FiveFortySeven extends CRM_Upgrade_Incremental
         $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>';
+      }
     }
   }
 
@@ -63,6 +70,8 @@ class CRM_Upgrade_Incremental_php_FiveFortySeven extends CRM_Upgrade_Incremental
       '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');
   }
 
   /**
index f5d36fc1b6cbd617c57a747cd6ef9534ce13b388..3943ae4308730cf3cbce2c9841494f9078609f5a 100644 (file)
@@ -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;
index 720e48f52ec6a8852d2b492f665f5a74d2385a3d..92955eb687379fe4037d499a4c54980af4acd065 100644 (file)
@@ -506,8 +506,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
    * @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']);
index 3fd5522cdf1cc5d8391a6b1dd210483172ba3bb5..cd38ce06701141465999b2cc3423f601a66cba80 100644 (file)
     <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>