CRM-16642 add setting & functions for opportunistic vs deterministic cache clearing
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
index 7d6a32749b4104a2e61d39b302c64effb1c0d797..c4be97e4a39393e8f33afab456322e931b7481e7 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -151,6 +151,7 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base
    * @param string $rev
    */
   public function upgrade_4_7_1($rev) {
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
     $this->addTask('Add Index to civicrm_contribution creditnote_id field', 'addIndexContributionCreditNoteID');
   }
 
@@ -160,10 +161,72 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base
    * @param string $rev
    */
   public function upgrade_4_7_2($rev) {
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
     $this->addTask('Fix Index on civicrm_financial_item combined entity_id + entity_table', 'addCombinedIndexFinancialItemEntityIDEntityType');
     $this->addTask('enable financial account relationships for chargeback & refund', 'addRefundAndChargeBackAccountsIfNotExist');
+    $this->addTask('Add Index to civicrm_contribution.source', 'addIndexContributionSource');
+  }
+
+  /**
+   * Upgrade function.
+   *
+   * @param string $rev
+   */
+  public function upgrade_4_7_3($rev) {
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
+    $this->addTask('Add Index to civicrm_contribution.total_amount', 'addIndexContributionAmount');
+  }
+
+  /**
+   * Upgrade function.
+   *
+   * @param string $rev
+   */
+  public function upgrade_4_7_4($rev) {
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
+    $this->addTask('Add Contact Deleted by Merge Activity Type', 'addDeletedByMergeActivityType');
+  }
+
+  /**
+   * Upgrade function.
+   *
+   * @param string $rev
+   */
+  public function upgrade_4_7_7($rev) {
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
+    // https://issues.civicrm.org/jira/browse/CRM-18006
+    if (CRM_Core_DAO::checkTableExists('civicrm_install_canary')) {
+      CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_install_canary ENGINE=InnoDB');
+    }
   }
 
+  /**
+   * Upgrade function.
+   *
+   * @param string $rev
+   */
+  public function upgrade_4_7_8($rev) {
+    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
+    $this->addTask('Upgrade mailing foreign key constraints', 'upgradeMailingFKs');
+    $this->addSmartGroupRefreshOptions();
+  }
+
+  /*
+   * Important! All upgrade functions MUST call the 'runSql' task.
+   * Uncomment and use the following template for a new upgrade version
+   * (change the x in the function name):
+   */
+
+  //  /**
+  //   * Upgrade function.
+  //   *
+  //   * @param string $rev
+  //   */
+  //  public function upgrade_4_7_x($rev) {
+  //    $this->addTask(ts('Upgrade DB to %1: SQL', array(1 => $rev)), 'runSql', $rev);
+  //    // Additional tasks here...
+  //  }
+
   /**
    * CRM-16354
    *
@@ -268,7 +331,7 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base
    * @return bool
    */
   public function addGettingStartedDashlet(CRM_Queue_TaskContext $ctx) {
-    $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='gettingStarted'";
+    $sql = "SELECT count(*) FROM civicrm_dashboard WHERE name='getting-started'";
     $res = CRM_Core_DAO::singleValueQuery($sql);
     $domainId = CRM_Core_Config::domainID();
     if ($res <= 0) {
@@ -278,7 +341,7 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base
       // Add default position for Getting Started Dashlet ( left column)
       $sql = "INSERT INTO `civicrm_dashboard_contact` (dashboard_id, contact_id, column_no, is_active)
 SELECT (SELECT MAX(id) FROM `civicrm_dashboard`), contact_id, 0, IF (SUM(is_active) > 0, 1, 0)
-FROM `civicrm_dashboard_contact` WHERE 1 GROUP BY contact_id";
+FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_contact.contact_id = civicrm_contact.id GROUP BY contact_id";
       CRM_Core_DAO::executeQuery($sql);
     }
     return TRUE;
@@ -487,4 +550,179 @@ FROM `civicrm_dashboard_contact` WHERE 1 GROUP BY contact_id";
     return TRUE;
   }
 
+  /**
+   * CRM-17999 Add index to civicrm_contribution.source.
+   *
+   * @param \CRM_Queue_TaskContext $ctx
+   *
+   * @return bool
+   */
+  public function addIndexContributionSource(CRM_Queue_TaskContext $ctx) {
+    CRM_Core_BAO_SchemaHandler::createIndexes(array('civicrm_contribution' => array('source')));
+    return TRUE;
+  }
+
+  /**
+   * CRM-18124 Add index to civicrm_contribution.total_amount.
+   *
+   * Note that I made this a combined index with receive_date because the issue included
+   * both criteria and they seemed likely to be used in conjunction to me in other cases.
+   *
+   * @param \CRM_Queue_TaskContext $ctx
+   *
+   * @return bool
+   */
+  public function addIndexContributionAmount(CRM_Queue_TaskContext $ctx) {
+    CRM_Core_BAO_SchemaHandler::createIndexes(array(
+      'civicrm_contribution' => array(array('total_amount', 'receive_date')),
+    ));
+    return TRUE;
+  }
+
+  /**
+   * CRM-18124 Add index to civicrm_contribution.total_amount.
+   *
+   * Note that I made this a combined index with receive_date because the issue included
+   * both criteria and they seemed likely to be used in conjunction to me in other cases.
+   *
+   * @param \CRM_Queue_TaskContext $ctx
+   *
+   * @return bool
+   */
+  public function addDeletedByMergeActivityType(CRM_Queue_TaskContext $ctx) {
+    CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
+      'option_group_id' => 'activity_type',
+      'name' => 'Contact Deleted by Merge',
+      'label' => ts('Contact Deleted by Merge'),
+      'description' => ts('Contact was merged into another contact'),
+      'is_active' => TRUE,
+      'filter' => 1,
+    ));
+    return TRUE;
+  }
+
+  /**
+   * Remove a foreign key from a table if it exists
+   *
+   * @param $table_name
+   * @param $constraint_name
+   */
+  public function safeRemoveFK($table_name, $constraint_name) {
+
+    $config = CRM_Core_Config::singleton();
+    $dbUf = DB::parseDSN($config->dsn);
+    $query = "
+      SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
+      WHERE TABLE_SCHEMA = %1
+      AND TABLE_NAME = %2
+      AND CONSTRAINT_NAME = %3
+      AND CONSTRAINT_TYPE = 'FOREIGN KEY'
+    ";
+    $params = array(
+      1 => array($dbUf['database'], 'String'),
+      2 => array($table_name, 'String'),
+      3 => array($constraint_name, 'String'),
+    );
+    $dao = CRM_Core_DAO::executeQuery($query, $params);
+
+    if ($dao->fetch()) {
+      CRM_Core_DAO::executeQuery("ALTER TABLE {$table_name} DROP FOREIGN KEY {$constraint_name}", array());
+    }
+
+  }
+
+  /**
+   * CRM-18345 Don't delete mailing data on email/phone deletion
+   * Implemented here in CRM-18526
+   *
+   * @param \CRM_Queue_TaskContext $ctx
+   *
+   * @return bool
+   */
+  public function upgradeMailingFKs(CRM_Queue_TaskContext $ctx) {
+
+    // Safely drop the foreign keys
+    self::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_email_id');
+    self::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_phone_id');
+    self::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_email_id');
+    self::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_phone_id');
+
+    // Set up the new foreign keys
+    CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 0;");
+
+    CRM_Core_DAO::executeQuery("
+      ALTER TABLE `civicrm_mailing_event_queue`
+        ADD CONSTRAINT `FK_civicrm_mailing_event_queue_email_id`
+        FOREIGN KEY (`email_id`)
+        REFERENCES `civicrm_email`(`id`)
+        ON DELETE SET NULL
+        ON UPDATE RESTRICT;
+    ");
+
+    CRM_Core_DAO::executeQuery("
+      ALTER TABLE `civicrm_mailing_event_queue`
+        ADD CONSTRAINT `FK_civicrm_mailing_event_queue_phone_id`
+        FOREIGN KEY (`phone_id`)
+        REFERENCES `civicrm_phone`(`id`)
+        ON DELETE SET NULL
+        ON UPDATE RESTRICT;
+    ");
+
+    CRM_Core_DAO::executeQuery("
+      ALTER TABLE `civicrm_mailing_recipients`
+        ADD CONSTRAINT `FK_civicrm_mailing_recipients_email_id`
+        FOREIGN KEY (`email_id`)
+        REFERENCES `civicrm_email`(`id`)
+        ON DELETE SET NULL
+        ON UPDATE RESTRICT;
+    ");
+
+    CRM_Core_DAO::executeQuery("
+      ALTER TABLE `civicrm_mailing_recipients`
+        ADD CONSTRAINT `FK_civicrm_mailing_recipients_phone_id`
+        FOREIGN KEY (`phone_id`)
+        REFERENCES `civicrm_phone`(`id`)
+        ON DELETE SET NULL
+        ON UPDATE RESTRICT;
+    ");
+
+    CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");
+
+    return TRUE;
+  }
+
+  /**
+   * CRM-16642 Add option for smart group refreshing.
+   *
+   * @param \CRM_Queue_TaskContext $ctx
+   *
+   * @return bool
+   */
+  public function addSmartGroupRefreshOptions(CRM_Queue_TaskContext $ctx) {
+    $optionGroupID = CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array(
+      'name' => 'smart_group_cache_refresh_mode',
+      'title' => ts('Mode for refreshing smart group cache'),
+      'description' => ts('This provides the option for the smart group cache setting'),
+      'is_reserved' => 1,
+    ));
+    CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
+      'option_group_id' => $optionGroupID,
+      'name' => 'opportunistic',
+      'label' => ts('Opportunistic'),
+      'description' => ts('Purge the cache in response to user actions'),
+      'is_active' => TRUE,
+      'filter' => 1,
+      'is_reserved' => 1,
+    ));
+    CRM_Core_BAO_OptionValue::ensureOptionValueExists(array(
+      'option_group_id' => $optionGroupID,
+      'name' => 'deterministic',
+      'label' => ts('Deterministic'),
+      'description' => ts('Only purge the cache on system jobs'),
+      'is_active' => TRUE,
+      'filter' => 1,
+      'is_reserved' => 1,
+    ));
+  }
+
 }