DataObjects/Core_notify.php
[Pman.Core] / UpdateDatabase.php
index 716ca83..b2bbfda 100644 (file)
@@ -1,14 +1,53 @@
 <?php
 
-
+/**
+ *
+ * This applies database files from
+ * a) OLD - {MODULE}/DataObjects/XXXX.{dbtype}.sql
+ *
+ * b) NEW - {MODULE}/sql/XXX.sql (SHARED or translable)
+ *  and {MODULE}/{dbtype}/XXX.sql (SHARED or translable)
+ *
+ *
+ */
 
 require_once 'Pman.php';
 class Pman_Core_UpdateDatabase extends Pman
 {
     
-    static $cli_desc = "Update SQL - Beta";
+    static $cli_desc = "Update SQL - Beta (it will run updateData of all modules)";
  
+    static $cli_opts = array(
+        'source' => array(
+            'desc' => 'Source directory for json files.',
+            'short' => 'f',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
+        'prefix' => array(
+            'desc' => 'prefix for the passwrod',
+            'short' => 'p',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
+        'name' => array(
+            'desc' => 'name of the company',
+            'short' => 'n',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
+        'comptype' => array(
+            'desc' => 'the type of company',
+            'short' => 't',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        )
+        
+    );
     
     var $cli = false;
     function getAuth() {
@@ -29,9 +68,34 @@ class Pman_Core_UpdateDatabase extends Pman
         return true;
     }
      
-    function get()
+    function get($args, $opt)
     {
+        if($args == 'Person'){
+            if(empty($opt['source']) || empty($opt['prefix'])){
+                die("Missing Source directory for person json files or prefix for the passwrod! Try -f [JSON file path] -p [prefix] \n");
+            }
+            if (!file_exists($opt['source'])) {
+                die("can not found person json file : {$opt['source']} \n");
+            }
+            
+            $persons = json_decode(file_get_contents($opt['source']),true);
+            
+            DB_DataObject::factory('person')->importFromArray($this, $persons, $opt['prefix']);
+            die("DONE! \n");
+        }
+        
+        if($args == 'Company'){
+            if(empty($opt['name']) || empty($opt['comptype'])){
+                die("Missing company name or type! Try --name=[the name of company] -- comptype=[the type of company] \n");
+            }
+            
+            DB_DataObject::factory('companies')->initCompanies($this, $opt['name'], $opt['comptype']);
+            
+            die("DONE! \n");
+        }
+        
         $this->importSQL();
+        $this->runUpdateModulesData();
          
     }
     function output() {
@@ -52,6 +116,15 @@ class Pman_Core_UpdateDatabase extends Pman
         $this->{'import' . $url['scheme']}($url);
         
     }
+    
+    /**
+     * mysql - does not support conversions.
+     * 
+     *
+     */
+    
+    
+    
     function importmysql($url)
     {
         
@@ -72,7 +145,7 @@ class Pman_Core_UpdateDatabase extends Pman
         echo $mysql_cmd . "\n" ;
         
         
-        
+        // old -- DAtaObjects/*.sql
         
         foreach($ar as $m) {
             
@@ -85,8 +158,8 @@ class Pman_Core_UpdateDatabase extends Pman
                     continue;
                 }
                 // .my.sql but not .pg.sql
-                if (preg_match('/#.[a-z]{2}\.sql#i', basename($bfn))
-                    && !preg_match('/#\.my\.sql#i', basename($bfn))
+                if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
+                    && !preg_match('#\.my\.sql#i', basename($fn))
                 ) { // skip migration scripts at present..
                     continue;
                 }
@@ -98,6 +171,29 @@ class Pman_Core_UpdateDatabase extends Pman
             
                 
             }
+            // 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..
+            $fd = $this->rootDir. "/Pman/$m/sql";
+            
+            foreach(glob($fd.'/*.sql') as $fn) {
+                $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
+                echo $cmd. ($this->cli ? "\n" : "<BR>\n");
+                passthru($cmd);
+            }
+            $fd = $this->rootDir. "/Pman/$m/mysql";
+            
+            foreach(glob($fd.'/*.sql') as $fn) {
+                $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
+                echo $cmd. ($this->cli ? "\n" : "<BR>\n");
+                passthru($cmd);
+            }
+              
+            
+            
+            
+            
+            
         }
         
         
@@ -116,6 +212,7 @@ class Pman_Core_UpdateDatabase extends Pman
         $psql = System::which('psql');
         
         $ar = $this->modulesList();
+        
         if (!empty($url['pass'])) { 
             putenv("PGPASSWORD=". $url['pass']);
         }
@@ -147,17 +244,41 @@ class Pman_Core_UpdateDatabase extends Pman
                 // files ending in .pg.sql are native postgres files..
                 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
                 
-                $cmd = "$psql_cmd -f  " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
+                $cmd = "$psql_cmd  < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
                 
                 echo "$bfn:   $cmd ". ($this->cli ? "\n" : "<BR>\n");
                 
                 
-                $res = `$cmd`;
+                passthru($cmd);
                 
                 if ($fn) {
                     unlink($fn);
                 }
             }
+            
+            
+            
+            $fd = $this->rootDir. "/Pman/$m/sql";
+            // sql directory  - we try to convert..
+            foreach(glob($fd.'/*.sql') as $bfn) {
+                $fn =  $this->convertToPG($bfn);
+                $cmd = "$psql_cmd  < " . escapeshellarg($fn) ;
+                echo $cmd. ($this->cli ? "\n" : "<BR>\n");
+                passthru($cmd);
+            }
+            
+            // postgres specific directory..
+            
+            $fd = $this->rootDir. "/Pman/$m/pgsql";
+            
+            foreach(glob($fd.'/*.sql') as $fn) {
+                $cmd = "$psql_cmd   < " . escapeshellarg($fn) ;
+                echo $cmd. ($this->cli ? "\n" : "<BR>\n");
+                passthru($cmd);
+            }
+            
+            
+            
         }
         
     }
@@ -220,6 +341,57 @@ class Pman_Core_UpdateDatabase extends Pman
         
         return $fn;
     }
+    
+    function runUpdateModulesData()
+    {
+        // runs core...
+        $this->updateData(); 
+        $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;
+            };
+            $x->updateData();
+        }
                 
+    }
+    
+    function updateData()
+    {
+        // fill i18n data..
+        
+        $enum = DB_DataObject::Factory('core_enum');
+        $enum->initEnums(
+            array(
+                array(
+                    'etype' => '',
+                    'name' => 'COMPTYPE',
+                    'display_name' =>  'Company Types',
+                    'cn' => array(
+                        array(
+                            'name' => 'OWNER',
+                            'display_name' => 'Owner',
+                            'seqid' => 999, // last...
+                        )
+                        
+                    )
+                )
+            )
+        ); 
+        
+        $groups = DB_DataObject::factory('groups');
+        $groups->initGroups();
+    }
+    
     
 }
\ No newline at end of file