UpdateDatabase/MysqlLinks.php
[Pman.Core] / UpdateDatabase / MysqlLinks.php
index 2988ac8..f004455 100644 (file)
  *   -- {tablename}_trigger_{optional_string}_before_update_{column_name}(OLD.column, NEW.column}
  *   -- {tablename}_trigger_{optional_string}_before_insert_{column_name}(OLD.column}
  *
- *  
  *
+ * ------- Importing with triggers disabled.
+ *
+ *  SET @DISABLE_TRIGGER=1; (or anything you like except NULL) 
+ *  do imports
+ * SET @DISABLE_TRIGGER=NULL;
+ *
+ * ------ Call a method disabling a particular set of triggers
+ *  SET @DISABLE_TRIGGER_the_table_name=1; (or anything you like except NULL) 
+ *  do action
+ *  SET @DISABLE_TRIGGER_the_table_name=NULL;*
  */
 
 class Pman_Core_UpdateDatabase_MysqlLinks {
@@ -38,9 +47,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
           
         $this->loadIniFiles();
         $this->updateTableComments();
-        
-        $this->updateCharacterSet();
-        
+       
         $ff = HTML_FlexyFramework::get();
         if (!empty($ff->Pman['enable_trigger_tests'])) {
             
@@ -188,8 +195,8 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
                 BEFORE DELETE ON `{$target_table}`
             FOR EACH ROW
             BEGIN
-               DECLARE mid INT(11);
-             
+                DECLARE mid INT(11);
+                IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$target_table} IS NULL ) THEN  
                
             ";
             foreach($sources as $source=>$target) {
@@ -215,6 +222,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
             }
             
             $trigger .= "
+                END IF;
             END 
            
             ";
@@ -251,12 +259,15 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
             FOR EACH ROW
             BEGIN
                DECLARE mid INT(11);
-               
+                IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$tbl} IS NULL ) THEN 
                
             ";
+            $has_checks=  false;
+            $errs = array();
             foreach($map as $source_col=>$target) {
                 // check that source_col exists in schema.
                 if (!isset($this->schema[$tbl][$source_col])) {
+                    $errs[] = "SOURCE MISSING: $source_col => $target";
                     continue;
                 }
                 
@@ -266,6 +277,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
                 
                 if (!isset($this->schema[$target_table])) {
                     // skip... target table does not exist
+                    $errs[] = "TARGET MISSING: $source_col => $target";
                     continue;
                 }
                 
@@ -281,21 +293,29 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
                        
                     END IF;
                 ";
-                
+                $has_checks=  true;
                 
                 
             }
+            
             $ar = $this->listTriggerFunctions($tbl, 'insert');
             foreach($ar as $fn=>$col) {
                 $trigger .= "
                     CALL $fn( NEW.{$col});
                 ";
+                $has_checks=  true;
             }
             
             $trigger .= "
+                END IF;
             END 
            
             ";
+            
+            if (!$has_checks) {
+                echo "SKIP TRIGGER {$tbl}_before_insert (missing " . implode(", ", $errs) . ")\n";
+                continue;
+            }
             //echo $trigger; exit;
             //DB_DAtaObject::debugLevel(1);
             $q = DB_DataObject::factory('core_enum');
@@ -332,12 +352,15 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
             FOR EACH ROW
             BEGIN
                DECLARE mid INT(11);
-               
+               IF (@DISABLE_TRIGGER IS NULL AND @DISABLE_TRIGGER_{$tbl} IS NULL ) THEN  
                
             ";
+            $has_checks=  false;
+            $errs = array();
             foreach($map as $source_col=>$target) {
                 // check that source_col exists in schema.
                 if (!isset($this->schema[$tbl][$source_col])) {
+                    $errs[] = "SOURCE MISSING: $source_col => $target";
                     continue;
                 }
                 
@@ -347,6 +370,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
                 
                 if (!isset($this->schema[$target_table])) {
                     // skip... target table does not exist
+                    $errs[] = "TARGET MISSING: $source_col => $target";
                     continue;
                 }
                 
@@ -361,18 +385,26 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
                        
                     END IF;
                 ";
+                $has_checks=  true;
             }
             $ar = $this->listTriggerFunctions($tbl, 'update');
             foreach($ar as $fn=>$col) {
                 $trigger .= "
                     CALL $fn(OLD.{$col}, NEW.{$col});
                 ";
+                $has_checks=  true;
             }
             
             $trigger .= "
+                END IF;
             END 
            
             ";
+            if (!$has_checks) {
+                echo "SKIP TRIGGER {$tbl}_before_update (missing " . implode(", ", $errs) . ")\n";
+                continue;
+            }
+            
             //echo $trigger; exit;
             //DB_DAtaObject::debugLevel(1);
             $q = DB_DataObject::factory('core_enum');
@@ -435,39 +467,7 @@ class Pman_Core_UpdateDatabase_MysqlLinks {
         return $ret;
     }
         
-    function updateCharacterSet()
-    {
-        foreach (array_keys($this->schema) as $tbl){
-            
-            if(strpos($tbl, '__keys') !== false ){
-                continue;
-            }
-            
-            echo "CALL mysql_change_charset('{$tbl}') \n";
-            
-            $ce = DB_DataObject::factory('core_enum');
-            
-            $ce->query("
-                SELECT
-                        CCSA.character_set_name csname,
-                        CCSA.collation_name collatename
-                FROM
-                        information_schema.`TABLES` T,
-                        information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
-                WHERE
-                        CCSA.collation_name = T.table_collation
-                    AND
-                        T.table_schema = mydb COLLATE utf8_unicode_ci
-                    AND
-                        T.table_name = mytb COLLATE utf8_unicode_ci
-
-            ");
-            
-            
-            
-            
-        }
-    }
+    
     
 }