From 5beb1de02307e77ca59db295e1bd52200a9a1029 Mon Sep 17 00:00:00 2001
From: Pradeep Nayak <pradeep@pradeep.(none)>
Date: Mon, 12 Aug 2013 16:15:21 +0530
Subject: [PATCH] -- fixed for CRM-13094

----------------------------------------
* CRM-13094: civicrm_contribution_recur schema inconsistent in fresh installs vs upgrades
  http://issues.civicrm.org/jira/browse/CRM-13094
---
 CRM/Upgrade/Incremental/php/FourThree.php   | 35 ++++++++++++++++++++-
 xml/schema/Contribute/ContributionRecur.xml |  4 +--
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/CRM/Upgrade/Incremental/php/FourThree.php b/CRM/Upgrade/Incremental/php/FourThree.php
index 66be1bf9df..89ff8bed09 100644
--- a/CRM/Upgrade/Incremental/php/FourThree.php
+++ b/CRM/Upgrade/Incremental/php/FourThree.php
@@ -326,6 +326,8 @@ ADD CONSTRAINT `FK_civicrm_financial_item_contact_id` FOREIGN KEY (`contact_id`)
   }
 
   function upgrade_4_3_6($rev) {
+    //CRM-13094
+    $this->addTask(ts('Add mising contraints'), 'addMissingConstraints', $rev);
     $this->addTask(ts('Upgrade DB to 4.3.6: SQL'), 'task_4_3_x_runSql', $rev);
     // CRM-12844
     // update line_item, financial_trxn and financial_item table for recurring contributions
@@ -870,12 +872,43 @@ ALTER TABLE civicrm_financial_account
     return TRUE;
   }
   
+  /**
+   * change index and add missing constraints for civicrm_contribution_recur
+   */
+  function addMissingConstraints(CRM_Queue_TaskContext $ctx) {
+    $query = "SHOW KEYS FROM `civicrm_contribution_recur` WHERE key_name = 'UI_contrib_payment_instrument_id'";
+    $dao = CRM_Core_DAO::executeQuery($query);
+    if ($dao->N) {
+      CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contribution_recur DROP INDEX UI_contrib_payment_instrument_id');
+      CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_contribution_recur ADD INDEX UI_contribution_recur_payment_instrument_id (payment_instrument_id)');
+    }
+    $constraintArray = array(
+      'contact_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_contact_id` FOREIGN KEY (`contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE CASCADE ", 
+      'payment_processor_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_payment_processor_id` FOREIGN KEY (`payment_processor_id`) REFERENCES `civicrm_payment_processor` (`id`) ON DELETE SET NULL ", 
+      'financial_type_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_financial_type_id` FOREIGN KEY (`financial_type_id`) REFERENCES `civicrm_financial_type` (`id`) ON DELETE SET NULL ", 
+      'campaign_id' => " ADD CONSTRAINT `FK_civicrm_contribution_recur_campaign_id` FOREIGN KEY (`campaign_id`) REFERENCES `civicrm_campaign` (`id`) ON DELETE SET NULL ",
+    );
+    $constraint = array();
+    foreach ($constraintArray as $constraintKey => $value) {
+      $foreignKeyExists = CRM_Core_DAO::checkFKConstraintInFormat('civicrm_contribution_recur', $constraintKey);
+      if (!$foreignKeyExists) {
+        $constraint[] = $value;
+      }
+    }
+    if (!empty($constraint)) {
+      $query = "ALTER TABLE civicrm_contribution_recur " . implode(' , ', $constraint);
+      CRM_Core_DAO::executeQuery($query);
+    }
+    return TRUE;    
+  }
+
   /**
    * Update financial_account_id for bad data in financial_trxn table
    * CRM-12844
    * 
    */
   function updateFinancialTrxnData(CRM_Queue_TaskContext $ctx) {
+    $upgrade = new CRM_Upgrade_Form();
     $sql = "SELECT cc.id contribution_id, cc.contribution_recur_id, cft.payment_processor_id, 
 cft.id financial_trxn_id, cfi.entity_table, cft.from_financial_account_id, cft.to_financial_account_id
 
@@ -937,7 +970,7 @@ id IN (' . implode(',', $val) . ')';
    * 
    */
   function updateLineItemData(CRM_Queue_TaskContext $ctx) {
-     $sql = "SELECT cc.id contribution_id, cc.contribution_recur_id,
+    $sql = "SELECT cc.id contribution_id, cc.contribution_recur_id,
 cc.financial_type_id contribution_financial_type, 
 cli.financial_type_id line_financial_type_id,
 cli.price_field_id, cli.price_field_value_id, cli.label, cli.id line_item_id,
diff --git a/xml/schema/Contribute/ContributionRecur.xml b/xml/schema/Contribute/ContributionRecur.xml
index e2a2171a04..540a8e6de6 100644
--- a/xml/schema/Contribute/ContributionRecur.xml
+++ b/xml/schema/Contribute/ContributionRecur.xml
@@ -164,7 +164,7 @@
   <field>
     <name>next_sched_contribution</name>
     <type>datetime</type>
-    <comment>At Groundspring this was used by the cron job which triggered payments. If we\'re not doing that but we know about payments, it might still be useful to store for display to org andor contributors.</comment>
+    <comment>At Groundspring this was used by the cron job which triggered payments. If we''re not doing that but we know about payments, it might still be useful to store for display to org andor contributors.</comment>
     <add>1.6</add>
   </field>
   <field>
@@ -241,7 +241,7 @@
     <add>4.1</add>
   </field>
   <index>
-    <name>UI_contrib_payment_instrument_id</name>
+    <name>UI_contribution_recur_payment_instrument_id</name>
     <fieldName>payment_instrument_id</fieldName>
     <add>4.1</add>
   </index>
-- 
GitLab