Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Mapping - Check FK on delete with SQL instead of PHP (dev/core#2757) #21198

Merged
merged 1 commit into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions CRM/Core/BAO/Mapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,12 @@ public static function retrieve(&$params, &$defaults) {
* 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]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/DAO/MappingField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/MappingField.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:d2ce640ccb1f30190097cced450038e0)
* (GenCodeChecksum:4333a20d925fb437b4764219e31ee017)
*/

/**
Expand Down
27 changes: 19 additions & 8 deletions CRM/Upgrade/Incremental/php/FiveFortyThree.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/

/**
* Upgrade logic for FiveFortyThree */
* Upgrade logic for FiveFortyThree
*/
class CRM_Upgrade_Incremental_php_FiveFortyThree extends CRM_Upgrade_Incremental_Base {

/**
Expand Down Expand Up @@ -49,12 +50,6 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
// }
}

/*
* 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.
*
Expand All @@ -63,7 +58,7 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
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
);
Expand All @@ -75,6 +70,22 @@ public function upgrade_5_43_alpha1(string $rev): void {
);
}

/**
* @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\'');
Expand Down
1 change: 1 addition & 0 deletions xml/schema/Core/MappingField.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<table>civicrm_mapping</table>
<key>id</key>
<add>1.2</add>
<onDelete>CASCADE</onDelete>
</foreignKey>
<field>
<name>name</name>
Expand Down