Skip to content

Commit

Permalink
Add product_id FK
Browse files Browse the repository at this point in the history
  • Loading branch information
monishdeb committed Jun 9, 2021
1 parent a6f6f86 commit fd54d5e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CRM/Contribute/DAO/ContributionProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contribute/ContributionProduct.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:908e3aa238e68db7e91731a9c441ee18)
* (GenCodeChecksum:641405fdb810dd88fcd6859510a65390)
*/

/**
Expand Down Expand Up @@ -118,6 +118,7 @@ public static function getEntityTitle($plural = FALSE) {
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']);
Expand Down Expand Up @@ -159,6 +160,7 @@ public static function &fields() {
'entity' => 'ContributionProduct',
'bao' => 'CRM_Contribute_DAO_ContributionProduct',
'localizable' => 0,
'FKClassName' => 'CRM_Contribute_DAO_Product',
'add' => '1.4',
],
'contribution_id' => [
Expand Down
23 changes: 23 additions & 0 deletions CRM/Upgrade/Incremental/php/FiveThirtyNine.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ public function setPostUpgradeMessage(&$postUpgradeMessage, $rev) {
// }
}

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
Expand Down
7 changes: 7 additions & 0 deletions xml/schema/Contribute/ContributionProduct.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
<required>true</required>
<add>1.4</add>
</field>
<foreignKey>
<name>product_id</name>
<table>civicrm_product</table>
<key>id</key>
<add>5.39</add>
<onDelete>CASCADE</onDelete>
</foreignKey>
<field>
<name>contribution_id</name>
<title>Contribution ID</title>
Expand Down

0 comments on commit fd54d5e

Please sign in to comment.