DataObjects/Core_watch.php
[Pman.Core] / UpdateDatabase.php
index 9539f59..f40ad96 100644 (file)
@@ -44,7 +44,7 @@ class Pman_Core_UpdateDatabase extends Pman
         'add-company-with-type' => array(
             'desc' => 'the type of company (default OWNER)',
             'short' => 't',
-            'default' => '',
+            'default' => 'OWNER',
             'min' => 1,
             'max' => 1,
         ),
@@ -61,6 +61,13 @@ class Pman_Core_UpdateDatabase extends Pman
             'min' => 1,
             'max' => 1,
         ),
+        'procedures-only' => array(
+            'desc' => 'Only import procedures (not supported by most modules yet) - ignores sql directory',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
+        
         
         'json-person' => array(
             'desc' => 'Person JSON file',
@@ -121,7 +128,6 @@ class Pman_Core_UpdateDatabase extends Pman
      
     function get($args, $opts)
     {
-        
         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
    
         
@@ -145,8 +151,12 @@ class Pman_Core_UpdateDatabase extends Pman
         
         
         if (!empty($opts['add-company'])) {
+            // make sure we have a good cache...?
+           
             DB_DataObject::factory('companies')->initCompanies($this, $opts);
         }
+        $this->fixLinks();
+         
          
     }
     function output() {
@@ -160,128 +170,57 @@ class Pman_Core_UpdateDatabase extends Pman
     function importSQL()
     {
         
-        $ff = HTML_Flexyframework::get();
+        // loop through all the modules, and see if they have a importSQL method?
         
-        $url = parse_url($ff->DB_DataObject['database']);
         
-        $this->{'import' . $url['scheme']}($url);
+        $ff = HTML_Flexyframework::get();
         
-    }
-    
-    /**
-     * mysql - does not support conversions.
-     * 
-     *
-     */
-    
-    
-    function importmysqldir($dburl, $dir)
-    {
-        echo "Import MYSQL :: $dir\n";
+        $dburl = parse_url($ff->DB_DataObject['database']);
         
+        //$this->{'import' . $url['scheme']}($url);
         
-        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" ;
-       
-       
-        foreach(glob($dir.'/*.sql') 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);
-            
-                
-        }
-       
+        $dbtype = $dburl['scheme'];
+        $dirmethod = 'import' . $dburl['scheme'] . 'dir';
         
         
-    }
-    
-    
-    
-    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");
                 
             }
             
@@ -289,32 +228,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');
@@ -332,7 +279,21 @@ class Pman_Core_UpdateDatabase extends Pman
         
         echo $psql_cmd . "\n" ;
         echo "scan : $dir\n";
-        foreach(glob($dir.'/*.sql') as $bfn) {
+        
+        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) {
 
 
             if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
@@ -356,8 +317,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);
 
@@ -370,6 +330,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...
      */
@@ -509,9 +530,11 @@ class Pman_Core_UpdateDatabase extends Pman
             if (!preg_match('/^json-/', $o) || empty($v)) {
                 continue;
             }
-            $type = str_replace('_', '-', substr($o,6));
-            $data= json_decode(file_get_contents($file),true);
-            DB_DataObject::factory($type)->importFromArray($this,$data,$opts);
+            $type = str_replace('_', '-', substr($o,5));
+            
+            $data= json_decode(file_get_contents($v),true);
+            $pg = HTML_FlexyFramework::get()->page;
+            DB_DataObject::factory($type)->importFromArray($pg ,$data,$opts);
             
         }
         
@@ -519,6 +542,8 @@ class Pman_Core_UpdateDatabase extends Pman
         
     }
     
+    
+    
     function runUpdateModulesData()
     {
         
@@ -557,7 +582,9 @@ class Pman_Core_UpdateDatabase extends Pman
     
     function updateDataEnums()
     {
+        
         $enum = DB_DataObject::Factory('core_enum');
+        //DB_DAtaObject::debugLevel(1);
         $enum->initEnums(
             array(
                 array(
@@ -627,7 +654,10 @@ class Pman_Core_UpdateDatabase extends Pman
                 'name' => 'bcc-email', // group who are bcc'ed on all requests.
                 'type' => 0, // system
             ),
-            
+            array(
+                'name' => 'system-email-from',
+                'type' => 0, // system
+            ),
         ));
         
     }
@@ -658,7 +688,7 @@ class Pman_Core_UpdateDatabase extends Pman
         $c->query("
             UPDATE Companies 
                 SET
-                    comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype)
+                    comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype LIMIT 1)
                 WHERE
                     comptype_id = 0
                     AND
@@ -674,7 +704,7 @@ class Pman_Core_UpdateDatabase extends Pman
     function updateData()
     {
         // fill i18n data..
-        
+        HTML_FlexyFramework::get()->generateDataobjectsCache(true);
         $this->updateDataEnums();
         $this->updateDataGroups();
         $this->updateDataCompanies();
@@ -686,12 +716,55 @@ 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()
     {
+     
+     
         //DB_DataObject::debugLevel(1);
         $cs = DB_DataObject::factory('core_enum');
         $cs->query("
-         SELECT 'ALTER SEQUENCE '|| quote_ident(min(schema_name)) ||'.'|| quote_ident(min(seq_name))
+         SELECT
+                    'ALTER SEQUENCE '||
+                    CASE WHEN strpos(seq_name, '.') > 0 THEN
+                        min(seq_name)
+                    ELSE 
+                        quote_ident(min(schema_name)) ||'.'|| quote_ident(min(seq_name))
+                    END 
+                    
                     ||' OWNED BY '|| quote_ident(min(schema_name)) || '.' ||
                     quote_ident(min(table_name)) ||'.'|| quote_ident(min(column_name)) ||';' as cmd
              FROM (
@@ -700,7 +773,7 @@ class Pman_Core_UpdateDatabase extends Pman
                      n.nspname AS schema_name,
                      c.relname AS table_name,
                      a.attname AS column_name, 
-                     regexp_replace(regexp_replace(d.adsrc, E'nextval\\\\(+[''\"]*', ''),E'[''\"]*::.*\$','') AS seq_name 
+                     regexp_replace(regexp_replace(d.adsrc, E'nextval\\\\(+[''\\\"]*', ''),E'[''\\\"]*::.*\$','') AS seq_name 
                  FROM pg_class c 
                  JOIN pg_attribute a ON (c.oid=a.attrelid) 
                  JOIN pg_attrdef d ON (a.attrelid=d.adrelid AND a.attnum=d.adnum) 
@@ -712,6 +785,13 @@ class Pman_Core_UpdateDatabase extends Pman
                    AND d.adsrc ~ '^nextval'
               
              ) seq
+             WHERE
+                 CASE WHEN strpos(seq_name, '.') > 0 THEN
+                     substring(seq_name, 1,strpos(seq_name,'.')-1)
+                ELSE
+                    schema_name
+                END = schema_name
+             
              GROUP BY seq_name HAVING count(*)=1
              ");
         $cmds = array();
@@ -720,13 +800,14 @@ class Pman_Core_UpdateDatabase extends Pman
         }
         foreach($cmds as $cmd) {
             $cs = DB_DataObject::factory('core_enum');
+            echo "$cmd\n";
             $cs->query($cmd);
         }
         $cs = DB_DataObject::factory('core_enum');
          $cs->query("
                SELECT  'SELECT SETVAL(' ||
                          quote_literal(quote_ident(nspname) || '.' || quote_ident(S.relname)) ||
-                        ', MAX(' || quote_ident(C.attname)|| ') )  FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd 
+                        ', MAX(' || quote_ident(C.attname)|| ')::integer )  FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd 
                 FROM pg_class AS S,
                     pg_depend AS D,
                     pg_class AS T,
@@ -738,7 +819,7 @@ class Pman_Core_UpdateDatabase extends Pman
                     AND D.refobjid = C.attrelid
                     AND D.refobjsubid = C.attnum
                     AND NS.oid = T.relnamespace
-                ORDER BY S.relname;     
+                ORDER BY S.relname   
         ");
          $cmds = array();
         while ($cs->fetch()) {
@@ -746,9 +827,31 @@ class Pman_Core_UpdateDatabase extends Pman
         }
         foreach($cmds as $cmd) {
             $cs = DB_DataObject::factory('core_enum');
+            echo "$cmd\n";
             $cs->query($cmd);
         }
        
     }
     
+    function fixLinks()
+    {
+        
+        $ff = HTML_Flexyframework::get();
+        
+        $dburl = parse_url($ff->DB_DataObject['database']);
+        
+        $dbtype = $dburl['scheme'];
+       
+        $scls = ucfirst($dbtype). 'Links';
+        $cls = 'Pman_Core_UpdateDatabase_'. $scls;
+        $fn = implode('/',explode('_', $cls)).'.php';
+        if (!file_exists(__DIR__.'/UpdateDatabase/'. $scls .'.php')) {
+            return;
+        }
+        require_once $fn;
+        $c = new $cls();
+        
+        
+        
+    }
 }
\ No newline at end of file