UpdateDatabase.php
[Pman.Core] / UpdateDatabase.php
index ca4d0eb..79de922 100644 (file)
@@ -54,10 +54,50 @@ class Pman_Core_UpdateDatabase extends Pman
             'max' => 1,
         ),
         
-        
+        'json-company' => array(
+            'desc' => 'Company JSON file',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+            
+        ),
+        'json-person' => array(
+            'desc' => 'Person JSON file',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+            
+        ),
     );
     
-    var $cli_options = false;
+    static function cli_opts()
+    {
+        
+        $ret = self::$cli_opts;
+        $ff = HTML_FlexyFramework::get();
+        $a = new Pman();
+        $mods = $a->modulesList();
+        foreach($mods as $m) {
+            
+            $fd = $ff->rootDir. "/Pman/$m/UpdateDatabase.php";
+            if (!file_exists($fd)) {
+                continue;
+            }
+            
+            require_once $fd;
+            
+            $cls = new ReflectionClass('Pman_'. $m . '_UpdateDatabase');
+            
+            $ret = array_merge($ret, $cls->getStaticPropertyValue('cli_opts'));
+            
+            
+        }
+        
+        return $ret;
+    }
+    
+    var $opts = false;
+    
     
     var $cli = false;
     function getAuth() {
@@ -81,7 +121,13 @@ class Pman_Core_UpdateDatabase extends Pman
     function get($args, $opt)
     {
         
-        $this->cli_options = $opt;
+        $this->opts = $opt;
+        
+        // ask all the modules to verify the opts
+        
+        $this->checkOpts($opt);
+        
+        
         
         if($args == 'Person'){
             if(empty($opt['source']) || empty($opt['prefix'])){
@@ -235,9 +281,9 @@ class Pman_Core_UpdateDatabase extends Pman
             
             // if init has been called
             // look in pgsql.ini
-            if (!empty($this->cli_options['init'])) {
+            if (!empty($this->opts['init'])) {
                 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.init");
-                $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.initdata");
+                
             }
             
             
@@ -252,13 +298,23 @@ class Pman_Core_UpdateDatabase extends Pman
             
             $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/sql");
             $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql");
+            
+            
+            
+            if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/pgsql.initdata")) {
+                HTML_FlexyFramework::get()->generateDataobjectsCache(true);
+                
+                $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.initdata");
+                $this->fixSequencesPgsql();
+                
+            }
               
             
         }
        
           
     }
-    function importpgsqldir($url, $dir)
+    function importpgsqldir($url, $dir, $disable_triggers = false)
     {
         require_once 'System.php';
         $cat = System::which('cat');
@@ -290,7 +346,7 @@ class Pman_Core_UpdateDatabase extends Pman
             }
             $fn = false;
 
-            if (basename($dir) != 'pgsql') {
+            if (!preg_match('/pgsql/', basename($dir) )) {
                  if ( !preg_match('#\.pg\.sql$#', basename($bfn))) {
                     $fn = $this->convertToPG($bfn);
                 }
@@ -415,9 +471,67 @@ class Pman_Core_UpdateDatabase extends Pman
         return $fn;
     }
     
+    
+    function checkOpts($opts)
+    {
+        
+        
+        foreach($opts as $o=>$v) {
+            if (!preg_match('/^json-/', $o) || empty($v)) {
+                continue;
+            }
+            if (!file_exists($v)) {
+                die("File does not exist : OPTION --{$o} = {$v} \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, 'checkOpts')){
+                continue;
+            };
+            $x->checkOpts($opts);
+        }
+                
+    }
+    static function jsonImportFromArray($opts)
+    {
+        foreach($opts as $o=>$v) {
+            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($data);
+            
+        }
+        
+        
+        
+    }
+    
     function runUpdateModulesData()
     {
+        
+        
+        HTML_FlexyFramework::get()->generateDataobjectsCache(true);
+        echo "Running jsonImportFromArray\n";
+        Pman_Core_UpdateDatabase::jsonImportFromArray($this->opts);
+        
+        
+        echo "Running updateData on modules\n";
         // runs core...
+        echo "Core\n";
         $this->updateData(); 
         $modules = array_reverse($this->modulesList());
         
@@ -428,12 +542,14 @@ class Pman_Core_UpdateDatabase extends Pman
             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();
         }
                 
@@ -571,6 +687,38 @@ class Pman_Core_UpdateDatabase extends Pman
         
         
     }
-    
+    function fixSequencesPgsql()
+    {
+        DB_DataObject::debugLevel(1);
+        $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 
+                FROM pg_class AS S,
+                    pg_depend AS D,
+                    pg_class AS T,
+                    pg_attribute AS C,
+                    pg_namespace AS NS
+                WHERE S.relkind = 'S'
+                    AND S.oid = D.objid
+                    AND D.refobjid = T.oid
+                    AND D.refobjid = C.attrelid
+                    AND D.refobjsubid = C.attnum
+                    AND NS.oid = T.relnamespace
+                ORDER BY S.relname;     
+        ");
+        while ($cs->fetch()) {
+            $cmds[] = $cs->cmd;
+        }
+        foreach($cmds as $cmd) {
+            $cs = DB_DataObject::factory('core_enum');
+            $cs->query($cmd);
+        }
+        
+         
+        
+        
+    }
     
 }
\ No newline at end of file