CRM-16642 add setting & functions for opportunistic vs deterministic cache clearing
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
index dab2d8ccd1796f2a559e6eeb9d17195db6e76706..c4be97e4a39393e8f33afab456322e931b7481e7 100644 (file)
@@ -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,6 +161,7 @@ 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');
@@ -171,6 +173,7 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base
    * @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');
   }
 
@@ -180,9 +183,50 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base
    * @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
    *
@@ -552,8 +596,133 @@ FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_
       '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,
+    ));
+  }
+
 }