Mapping - Check FK on delete with SQL instead of with PHP
authorColeman Watts <coleman@civicrm.org>
Fri, 20 Aug 2021 17:24:48 +0000 (13:24 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 10 Sep 2021 13:27:48 +0000 (09:27 -0400)
CRM/Core/BAO/Mapping.php
CRM/Core/DAO/MappingField.php
CRM/Upgrade/Incremental/php/FiveFortyThree.php
xml/schema/Core/MappingField.xml

index d026cf8df7bc25dff5e02918aaa0d7aec32a74bf..d7b72b9e766c3b62c1890c845f1392bdad2838dc 100644 (file)
@@ -48,24 +48,12 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping {
    * Delete the mapping.
    *
    * @param int $id
-   *   Mapping id.
    *
+   * @deprecated
    * @return bool
    */
   public static function del($id) {
-    // delete from mapping_field table
-    $mappingField = new CRM_Core_DAO_MappingField();
-    $mappingField->mapping_id = $id;
-    $mappingField->delete();
-
-    // delete from mapping table
-    $mapping = new CRM_Core_DAO_Mapping();
-    $mapping->id = $id;
-    if ($mapping->find(TRUE)) {
-      $result = $mapping->delete();
-      return $result;
-    }
-    return FALSE;
+    return (bool) static::deleteRecord(['id' => $id]);
   }
 
   /**
index 9c976231026f80f663620d16270064899a2b2f97..833fdff45f2dc4a443252d82fa86cbec28c010ed 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Generated from xml/schema/CRM/Core/MappingField.xml
  * DO NOT EDIT.  Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:d2ce640ccb1f30190097cced450038e0)
+ * (GenCodeChecksum:4333a20d925fb437b4764219e31ee017)
  */
 
 /**
index 413b61e62bb5a6ed4404360582c5fc5f759c39b8..48b8f98ef43d36c4f55ec7edd0529e830ef2a241 100644 (file)
@@ -10,7 +10,8 @@
  */
 
 /**
- * Upgrade logic for FiveFortyThree */
+ * Upgrade logic for FiveFortyThree
+ */
 class CRM_Upgrade_Incremental_php_FiveFortyThree extends CRM_Upgrade_Incremental_Base {
 
   /**
@@ -49,12 +50,6 @@ class CRM_Upgrade_Incremental_php_FiveFortyThree extends CRM_Upgrade_Incremental
     // }
   }
 
-  /*
-   * Important! All upgrade functions MUST add a 'runSql' task.
-   * Uncomment and use the following template for a new upgrade version
-   * (change the x in the function name):
-   */
-
   /**
    * Upgrade function.
    *
@@ -63,7 +58,7 @@ class CRM_Upgrade_Incremental_php_FiveFortyThree extends CRM_Upgrade_Incremental
   public function upgrade_5_43_alpha1(string $rev): void {
     $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev);
     $this->addTask('Fix DB Collation if needed on the relatonship cache table', 'fixRelationshipCacheTableCollation');
-
+    $this->addTask('Make mapping field foreign key cascade delete', 'alterMappingFK');
     $this->addTask('Replace legacy displayName smarty token in Online contribution workflow template',
       'updateMessageToken', 'contribution_online_receipt', '$displayName', 'contact.display_name', $rev
     );
@@ -75,6 +70,22 @@ class CRM_Upgrade_Incremental_php_FiveFortyThree extends CRM_Upgrade_Incremental
     );
   }
 
+  /**
+   * @param \CRM_Queue_TaskContext $ctx
+   *
+   * @return bool
+   */
+  public static function alterMappingFK(CRM_Queue_TaskContext $ctx): bool {
+    CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mapping_field', 'FK_civicrm_mapping_field_mapping_id');
+    CRM_Core_DAO::executeQuery('
+        ALTER TABLE civicrm_mapping_field
+        ADD CONSTRAINT `FK_civicrm_mapping_field_mapping_id`
+        FOREIGN KEY (`mapping_id`)
+        REFERENCES `civicrm_mapping`(`id`) ON DELETE CASCADE
+      ', [], TRUE, NULL, FALSE, FALSE);
+    return TRUE;
+  }
+
   public static function fixRelationshipCacheTableCollation():bool {
     $contactTableCollation = CRM_Core_BAO_SchemaHandler::getInUseCollation();
     $dao = CRM_Core_DAO::executeQuery('SHOW TABLE STATUS LIKE \'civicrm_relationship_cache\'');
index 0f4114aacf870af4e1ef250eb546f261c81b1b81..10cf1c4d542dfd03a7b0924f85ec8c763ba33c17 100644 (file)
@@ -38,6 +38,7 @@
     <table>civicrm_mapping</table>
     <key>id</key>
     <add>1.2</add>
+    <onDelete>CASCADE</onDelete>
   </foreignKey>
   <field>
     <name>name</name>