From 8714d4b4d25dc3d2c224bd0beb8e3733c3831572 Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Tue, 8 Jun 2021 13:05:02 +0530 Subject: [PATCH] Add product_id FK --- CRM/Contribute/DAO/ContributionProduct.php | 4 +++- .../Incremental/php/FiveThirtyNine.php | 23 +++++++++++++++++++ xml/schema/Contribute/ContributionProduct.xml | 7 ++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CRM/Contribute/DAO/ContributionProduct.php b/CRM/Contribute/DAO/ContributionProduct.php index 9ba71809a7..4c6063fd49 100644 --- a/CRM/Contribute/DAO/ContributionProduct.php +++ b/CRM/Contribute/DAO/ContributionProduct.php @@ -6,7 +6,7 @@ * * Generated from xml/schema/CRM/Contribute/ContributionProduct.xml * DO NOT EDIT. Generated by CRM_Core_CodeGen - * (GenCodeChecksum:874d8c0fea5d04fa5db38546c30f8c3d) + * (GenCodeChecksum:641405fdb810dd88fcd6859510a65390) */ /** @@ -118,6 +118,7 @@ class CRM_Contribute_DAO_ContributionProduct extends CRM_Core_DAO { public static function getReferenceColumns() { if (!isset(Civi::$statics[__CLASS__]['links'])) { Civi::$statics[__CLASS__]['links'] = static::createReferenceColumns(__CLASS__); + Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'product_id', 'civicrm_product', 'id'); Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'contribution_id', 'civicrm_contribution', 'id'); Civi::$statics[__CLASS__]['links'][] = new CRM_Core_Reference_Basic(self::getTableName(), 'financial_type_id', 'civicrm_financial_type', 'id'); CRM_Core_DAO_AllCoreTables::invoke(__CLASS__, 'links_callback', Civi::$statics[__CLASS__]['links']); @@ -159,6 +160,7 @@ class CRM_Contribute_DAO_ContributionProduct extends CRM_Core_DAO { 'entity' => 'ContributionProduct', 'bao' => 'CRM_Contribute_DAO_ContributionProduct', 'localizable' => 0, + 'FKClassName' => 'CRM_Contribute_DAO_Product', 'add' => '1.4', ], 'contribution_id' => [ diff --git a/CRM/Upgrade/Incremental/php/FiveThirtyNine.php b/CRM/Upgrade/Incremental/php/FiveThirtyNine.php index 4ad6125a39..b3e1d61512 100644 --- a/CRM/Upgrade/Incremental/php/FiveThirtyNine.php +++ b/CRM/Upgrade/Incremental/php/FiveThirtyNine.php @@ -49,6 +49,29 @@ class CRM_Upgrade_Incremental_php_FiveThirtyNine extends CRM_Upgrade_Incremental // } } + public function upgrade_5_39_alpha1($rev) { + $this->addTask(ts('Upgrade DB to %1: SQL', [1 => $rev]), 'runSql', $rev); + $this->addTask('core-issue#2486 - Add product_id foreign key to civicrm_contribution_product', 'addContributionProductFK'); + } + + /** + * @param \CRM_Queue_TaskContext $ctx + * + * @return bool + */ + public static function addContributionProductFK(CRM_Queue_TaskContext $ctx): bool { + if (!self::checkFKExists('civicrm_contribution_product', 'FK_civicrm_contribution_product_product_id')) { + CRM_Core_DAO::executeQuery(" + ALTER TABLE `civicrm_contribution_product` + ADD CONSTRAINT `FK_civicrm_contribution_product_product_id` + FOREIGN KEY (`product_id`) REFERENCES `civicrm_product` (`id`) + ON DELETE CASCADE; + ", [], TRUE, NULL, FALSE, FALSE); + } + + return TRUE; + } + /* * Important! All upgrade functions MUST add a 'runSql' task. * Uncomment and use the following template for a new upgrade version diff --git a/xml/schema/Contribute/ContributionProduct.xml b/xml/schema/Contribute/ContributionProduct.xml index 314e015f6e..202324429d 100644 --- a/xml/schema/Contribute/ContributionProduct.xml +++ b/xml/schema/Contribute/ContributionProduct.xml @@ -28,6 +28,13 @@ true 1.4 + + product_id + civicrm_product
+ id + 5.39 + CASCADE +
contribution_id Contribution ID -- 2.25.1