From: Coleman Watts Date: Fri, 20 Aug 2021 17:24:48 +0000 (-0400) Subject: Mapping - Check FK on delete with SQL instead of with PHP X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9f3b9e969597d718a23a22f904c703338ce186fa;p=civicrm-core.git Mapping - Check FK on delete with SQL instead of with PHP --- diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index d026cf8df7..d7b72b9e76 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -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]); } /** diff --git a/CRM/Core/DAO/MappingField.php b/CRM/Core/DAO/MappingField.php index 9c97623102..833fdff45f 100644 --- a/CRM/Core/DAO/MappingField.php +++ b/CRM/Core/DAO/MappingField.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Core/MappingField.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:d2ce640ccb1f30190097cced450038e0) + * (GenCodeChecksum:4333a20d925fb437b4764219e31ee017) */ /** diff --git a/CRM/Upgrade/Incremental/php/FiveFortyThree.php b/CRM/Upgrade/Incremental/php/FiveFortyThree.php index 413b61e62b..48b8f98ef4 100644 --- a/CRM/Upgrade/Incremental/php/FiveFortyThree.php +++ b/CRM/Upgrade/Incremental/php/FiveFortyThree.php @@ -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\''); diff --git a/xml/schema/Core/MappingField.xml b/xml/schema/Core/MappingField.xml index 0f4114aacf..10cf1c4d54 100644 --- a/xml/schema/Core/MappingField.xml +++ b/xml/schema/Core/MappingField.xml @@ -38,6 +38,7 @@ civicrm_mapping
id 1.2 + CASCADE name