DataObjects/Core_person_signup.php
[Pman.Core] / UpdateDatabase.php
index 81d076f..a81d927 100644 (file)
@@ -173,132 +173,52 @@ class Pman_Core_UpdateDatabase extends Pman
         
         $ff = HTML_Flexyframework::get();
         
-        $url = parse_url($ff->DB_DataObject['database']);
+        $dburl = parse_url($ff->DB_DataObject['database']);
         
-        $this->{'import' . $url['scheme']}($url);
+        //$this->{'import' . $url['scheme']}($url);
         
+        $dbtype = $dburl['scheme'];
+        $dirmethod = 'import' . $dburl['scheme'] . 'dir';
         
         
-        
-    }
-    
-    /**
-     * mysql - does not support conversions.
-     * 
-     *
-     */
-    
-    
-    function importmysqldir($dburl, $dir)
-    {
-        echo "Import MYSQL :: $dir\n";
-        
-        
-        require_once 'System.php';
-        $cat = System::which('cat');
-        $mysql = System::which('mysql');
-        
-       
-           
-        $mysql_cmd = $mysql .
-            ' -h ' . $dburl['host'] .
-            ' -u' . escapeshellarg($dburl['user']) .
-            (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass'])  :  '') .
-            ' ' . basename($dburl['path']);
-        //echo $mysql_cmd . "\n" ;
-        
-        $files = glob($dir.'/*.sql');
-        uksort($files, 'strcasecmp');
-        
-       
-        foreach($files as $fn) {
-                
-                 
-                if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
-                    continue;
-                }
-                // .my.sql but not .pg.sql
-                if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
-                    && !preg_match('#\.my\.sql#i', basename($fn))
-                ) { // skip migration scripts at present..
-                    continue;
-                }
-                if (!strlen(trim($fn))) {
-                    continue;
-                }
-                
-                $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
-                
-                echo basename($dir).'/'. basename($fn) .    '::' .  $cmd. ($this->cli ? "\n" : "<BR>\n");
-                
-                passthru($cmd);
-            
-                
-        }
        
         
-        
-    }
-    
-    
-    
-    function importmysql($dburl)
-    {
-        
-        // hide stuff for web..
         $ar = $this->modulesList();
-        
-         
-        
-        // old -- DAtaObjects/*.sql
+       
         
         foreach($ar as $m) {
-            
+             echo "Importing SQL from module $m\n";
             if (!empty($this->opts['only-module-sql']) && $m != $this->opts['only-module-sql']) {
                 continue;
             }
             
-            $fd = $this->rootDir. "/Pman/$m/DataObjects";
             
-            $this->importmysqldir($dburl, $fd);
+            // check to see if the class has
             
-            // new -- sql directory..
-            // new style will not support migrate ... they have to go into mysql-migrate.... directories..
-            // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
             
-            $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/sql");
-            $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/mysql");
-              
             
-        }
-        
-        
-        
-    }
-    /**
-     * postgresql import..
-     */
-    function importpgsql($dburl)
-    {
-        
-        // hide stuff for web..
-        
-        
-       
-        
-        $ar = $this->modulesList();
-       
-        print_R($ar);
-        foreach($ar as $m) {
-             echo "Importing SQL from module $m\n";
-            if (!empty($this->opts['only-module-sql']) && $m != $this->opts['only-module-sql']) {
-                continue;
-            }
+            $file = $this->rootDir. "/Pman/$m/UpdateDatabase.php";
+            if($m != 'Core' && file_exists($file)){
+                
+                require_once $file;
+                $class = "Pman_{$m}_UpdateDatabase";
+                $x = new $class;
+                if(method_exists($x, 'importModuleSQL')){
+                    echo "Importing SQL from module $m using Module::importModuleSQL\n";
+                    $x->opts = $this->opts;
+                    $x->rootDir = $this->rootDir;
+                    $x->importModuleSQL($dburl);
+                    continue;
+                }
+            };
+
             echo "Importing SQL from module $m\n";
+            
+            
             // if init has been called
             // look in pgsql.ini
             if (!empty($this->opts['init'])) {
-                $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.init");
+                $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
                 
             }
             
@@ -306,32 +226,40 @@ class Pman_Core_UpdateDatabase extends Pman
             
             $fd = $this->rootDir. "/Pman/$m/DataObjects";
             
-            $this->importpgsqldir($dburl, $fd);
+            $this->{$dirmethod}($dburl, $fd);
             
             // new -- sql directory..
             // new style will not support migrate ... they have to go into mysql-migrate.... directories..
             // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
             
-            $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/sql");
-            $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql");
+            $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/sql");
+            $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
             
             
             
-            if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/pgsql.initdata")) {
+            if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
                 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
                 
-                $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.initdata");
-                $this->fixSequencesPgsql();
+                $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
+                $this->{'fixSequences'. $dbtype}();
                 
             }
               
             
         }
-       
-          
+        
     }
+    
+    
+    
+     
+    /** -------------- code to handle importing a whole directory of files into the database  -------  **/
+    
+    
     function importpgsqldir($url, $dir, $disable_triggers = false)
     {
+        $ff = HTML_FlexyFramework::get();
+        
         require_once 'System.php';
         $cat = System::which('cat');
         $psql = System::which('psql');
@@ -350,13 +278,19 @@ class Pman_Core_UpdateDatabase extends Pman
         echo $psql_cmd . "\n" ;
         echo "scan : $dir\n";
         
-        $files = glob($dir.'/*.sql');
-        uksort($files, 'strcasecmp');
+        if (is_file($dir)) {
+            $files = array($dir);
+
+        } else {
+        
+        
+            $files = glob($dir.'/*.sql');
+            uksort($files, 'strcasecmp');
+        }
         //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
         //usort($files, $lsort);
         
         
-        
         foreach($files as $bfn) {
 
 
@@ -381,8 +315,7 @@ class Pman_Core_UpdateDatabase extends Pman
 
             $cmd = "$psql_cmd  < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
 
-            echo "$bfn:   $cmd ". ($this->cli ? "\n" : "<BR>\n");
-
+            echo "$bfn:   $cmd ". ($ff->cli ? "\n" : "<BR>\n");
 
             passthru($cmd);
 
@@ -395,6 +328,67 @@ class Pman_Core_UpdateDatabase extends Pman
              
         
     }
+    
+    
+    /**
+     * mysql - does not support conversions.
+     * 
+     *
+     */
+    
+    
+    function importmysqldir($dburl, $dir)
+    {
+        echo "Import MYSQL :: $dir\n";
+        
+        
+        require_once 'System.php';
+        $cat = System::which('cat');
+        $mysql = System::which('mysql');
+        
+       
+           
+        $mysql_cmd = $mysql .
+            ' -h ' . $dburl['host'] .
+            ' -u' . escapeshellarg($dburl['user']) .
+            (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass'])  :  '') .
+            ' ' . basename($dburl['path']);
+        //echo $mysql_cmd . "\n" ;
+        
+        $files = glob($dir.'/*.sql');
+        uksort($files, 'strcasecmp');
+        
+       
+        foreach($files as $fn) {
+                
+                 
+                if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
+                    continue;
+                }
+                // .my.sql but not .pg.sql
+                if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
+                    && !preg_match('#\.my\.sql#i', basename($fn))
+                ) { // skip migration scripts at present..
+                    continue;
+                }
+                if (!strlen(trim($fn))) {
+                    continue;
+                }
+                
+                $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
+                
+                echo basename($dir).'/'. basename($fn) .    '::' .  $cmd. ($this->cli ? "\n" : "<BR>\n");
+                
+                passthru($cmd);
+            
+                
+        }
+       
+        
+        
+    }
+    
+    
     /**
      * simple regex based convert mysql to pgsql...
      */
@@ -545,50 +539,7 @@ class Pman_Core_UpdateDatabase extends Pman
         
         
     }
-    /**
-     *
-     * modular import of SQL.
-     * -- if a module implements importSQL
-     *   - then we should not run the import SQL on the children of that folder as it is handled by the module.
-     *   
-     *
-     */
-    function runModulesImportSQL()
-    {
-        
-        
-        HTML_FlexyFramework::get()->generateDataobjectsCache(true);
-        echo "Running runModulesImportSQL\n";
-         
-        
-        echo "Running importSQL on modules\n";
-        // runs core...
-        echo "Core\n";
-        
-        
-        $modules = array_reverse($this->modulesList());
-        
-        // move 'project' one to the end...
-        
-        foreach ($modules as $module){
-            $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
-            if($module == 'Core' || !file_exists($file)){
-                continue;
-            }
-            
-            require_once $file;
-            $class = "Pman_{$module}_UpdateDatabase";
-            $x = new $class;
-            if(!method_exists($x, 'updateData')){
-                continue;
-            };
-            echo "$module\n";
-            $x->updateData();
-        }
-        
-        
-                
-    }
+    
     
     
     function runUpdateModulesData()
@@ -763,6 +714,40 @@ class Pman_Core_UpdateDatabase extends Pman
         
         
     }
+    
+    function fixMysqlInnodb()
+    {
+        // innodb in single files is far more efficient that MYD or one big innodb file.
+        // first check if database is using this format.
+        $db = DB_DataObject::factory('core_enum');
+        $db->query("show variables like 'innodb_file_per_table'");
+        $db->fetch();
+        var_dump($db);
+        // Value should == ON
+        
+        // I think it needs to look in the mysql directory.... to see if the idb exists..
+        $db = DB_DataObject::factory('core_enum');
+        $db->query("SHOW TABLE STATUS");
+        // Engine should be InnoDB - and file XXX.idb should exist...
+        
+        
+
+        
+        
+        
+        
+        
+    }
+    
+    
+    /** ------------- schema fixing ... there is an issue with data imported having the wrong sequence names... --- */
+    
+    function fixSequencesMysql()
+    {
+        // not required...
+    }
+    
     function fixSequencesPgsql()
     {