CRM-19038 NFC shift safeDropFK code to SchemaHandler Class from Upgrade Code
authorSeamus Lee <seamuslee001@gmail.com>
Fri, 1 Jul 2016 02:30:46 +0000 (12:30 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 1 Jul 2016 02:30:46 +0000 (12:30 +1000)
CRM/Core/BAO/SchemaHandler.php
CRM/Upgrade/Incremental/php/FourSeven.php
tests/phpunit/CRM/Core/BAO/SchemaHandlerTest.php

index a390e7de3d64ef4eb7bb318be26545629f380645..c8c8200105c33112b06d2fd39a9c1bfd98c9afaa 100644 (file)
@@ -598,4 +598,34 @@ MODIFY      {$columnName} varchar( $length )
     return FALSE;
   }
 
+  /**
+   * Remove a foreign key from a table if it exists
+   *
+   * @param $table_name
+   * @param $constraint_name
+   */
+  public static 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());
+    }
+
+  }
+
 }
index 9a7f81e160dd8222790e3eae008843fbab505259..ab9fee778095188306a1d773af92100e5f8849db 100644 (file)
@@ -666,36 +666,6 @@ 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
@@ -707,10 +677,10 @@ FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_
   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');
+    CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_email_id');
+    CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_event_queue', 'FK_civicrm_mailing_event_queue_phone_id');
+    CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_recipients', 'FK_civicrm_mailing_recipients_email_id');
+    CRM_Core_BAO_SchemaHandler::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;");
index 00a86bbc2d734da680cc8654b8a0129dd1e286b4..90089fb1ca10a6693d7ec5e60ad76c3a04c9ec42 100644 (file)
@@ -148,4 +148,20 @@ class CRM_Core_BAO_SchemaHandlerTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test to see if we can drop foreign key
+   *
+   */
+  public function testSafeDropForeignKey() {
+    $tests = array('FK_civicrm_mailing_recipients_email_id', 'FK_civicrm_mailing_recipients_id');
+    foreach ($tests as $test) {
+      if ($test == 'FK_civicrm_mailing_recipients_id') {
+        $this->assertFalse(CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_recipients', $test));
+      }
+      else {
+        $this->assertTrue(CRM_Core_BAO_SchemaHandler::safeRemoveFK('civicrm_mailing_recipients', $test));
+      }
+    }
+  }
+
 }