CRM-16642 add setting & functions for opportunistic vs deterministic cache clearing
[civicrm-core.git] / CRM / Upgrade / Incremental / php / FourSeven.php
index bd1c17560afa39f4d1c4507eea3529e0d64e632e..c4be97e4a39393e8f33afab456322e931b7481e7 100644 (file)
@@ -200,6 +200,17 @@ class CRM_Upgrade_Incremental_php_FourSeven extends CRM_Upgrade_Incremental_Base
     }
   }
 
+  /**
+   * 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
@@ -590,4 +601,128 @@ FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_
     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,
+    ));
+  }
+
 }