From a8dd306eb289cbdce03ef3d3cb4d26847eef8e31 Mon Sep 17 00:00:00 2001
From: "Donald A. Lobo" <lobo@civicrm.org>
Date: Tue, 15 Oct 2013 12:52:37 -0700
Subject: [PATCH] CRM-11794 - drop all triggers with civicrm_ prefix

----------------------------------------
* CRM-11794: Drop Trigger IF Exists fatals if custom table too long
  http://issues.civicrm.org/jira/browse/CRM-11794
---
 CRM/Core/DAO.php                   |  6 +++---
 CRM/Logging/Schema.php             | 14 ++++++++++++++
 tests/phpunit/CRM/Core/DAOTest.php |  2 +-
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php
index 395552221f..0069666815 100644
--- a/CRM/Core/DAO.php
+++ b/CRM/Core/DAO.php
@@ -1922,9 +1922,9 @@ EOS;
     // the string is longer than the length and we need a uniq string
     // for the same tablename we need the same uniq string everytime
     // hence we use md5 on the string, which is not random
-    // we'll append 16 characters to the end of the tableName
-    $md5string = substr(md5($string), 0, 16);
-    return substr($string, 0, $length - 17) . "_{$md5string}";
+    // we'll append 8 characters to the end of the tableName
+    $md5string = substr(md5($string), 0, 8);
+    return substr($string, 0, $length - 8) . "_{$md5string}";
   }
 
 }
diff --git a/CRM/Logging/Schema.php b/CRM/Logging/Schema.php
index bb7efcd098..877be2dad1 100644
--- a/CRM/Logging/Schema.php
+++ b/CRM/Logging/Schema.php
@@ -165,6 +165,20 @@ AND    TABLE_NAME LIKE 'log_civicrm_%'
       $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_update");
       $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_delete");
     }
+
+    // now lets also be safe and drop all triggers that start with
+    // civicrm_ if we are dropping all triggers
+    // we need to do this to capture all the leftover triggers since
+    // we did the shortening trigger name for CRM-11794
+    if ($tableName === NULL) {
+      $triggers = $dao->executeQuery("SHOW TRIGGERS LIKE 'civicrm_%'");
+
+      while ($triggers->fetch()) {
+        // note that drop trigger has a wierd syntax and hence we do not
+        // send the trigger name as a string (i.e. its not quoted
+        $dao->executeQuery("DROP TRIGGER IF EXISTS {$triggers->Trigger}");
+      }
+    }
   }
 
   /**
diff --git a/tests/phpunit/CRM/Core/DAOTest.php b/tests/phpunit/CRM/Core/DAOTest.php
index d6eedd98fd..d0bba05048 100644
--- a/tests/phpunit/CRM/Core/DAOTest.php
+++ b/tests/phpunit/CRM/Core/DAOTest.php
@@ -168,7 +168,7 @@ class CRM_Core_DAOTest extends CiviUnitTestCase {
       array('this is an even longer string which is exactly 60 character', 60, TRUE , 'this is an even longer string which is exactly 60 character'),
 
       array('this is an even longer string which is a bit more than 60 character', 60, FALSE, 'this is an even longer string which is a bit more than 60 ch'),
-      array('this is an even longer string which is a bit more than 60 character', 60, TRUE , 'this is an even longer string which is a bi_c1cbd5198187eb96'),
+      array('this is an even longer string which is a bit more than 60 character', 60, TRUE , 'this is an even longer string which is a bit more th_c1cbd519'),
     );
   }
 
-- 
GitLab