UpdateDatabase.php
[Pman.Core] / UpdateDatabase.php
1 <?php
2
3
4
5 require_once 'Pman.php';
6 class Pman_Core_UpdateDatabase extends Pman
7 {
8     
9     static $cli_desc = "Update SQL - Beta";
10  
11  
12     
13     var $cli = false;
14     function getAuth() {
15         
16         
17         $ff = HTML_FlexyFramework::get();
18         if (!empty($ff->cli)) {
19             $this->cli = true;
20             return true;
21         }
22         
23         parent::getAuth(); // load company!
24         $au = $this->getAuthUser();
25         if (!$au || $au->company()->comptype != 'OWNER') {
26             $this->jerr("Not authenticated", array('authFailure' => true));
27         }
28         $this->authUser = $au;
29         return true;
30     }
31      
32     function get()
33     {
34         $this->importSQL();
35          
36     }
37     function output() {
38         return '';
39     }
40      /**
41      * imports SQL files from all DataObjects directories....
42      * 
43      * except any matching /migrate/
44      */
45     function importSQL()
46     {
47         
48         $ff = HTML_Flexyframework::get();
49         
50         $url = parse_url($ff->DB_DataObject['database']);
51         
52         $this->{'import' . $url['scheme']}($url);
53         
54     }
55     function importmysql($url)
56     {
57         
58         // hide stuff for web..
59         
60         require_once 'System.php';
61         $cat = System::which('cat');
62         $mysql = System::which('mysql');
63         
64         $ar = $this->modulesList();
65         
66            
67         $mysql_cmd = $mysql .
68             ' -h ' . $url['host'] .
69             ' -u' . escapeshellarg($url['user']) .
70             (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass'])  :  '') .
71             ' ' . basename($url['path']);
72         echo $mysql_cmd . "\n" ;
73         
74         
75         
76         
77         foreach($ar as $m) {
78             
79             $fd = $this->rootDir. "/Pman/$m/DataObjects";
80             
81             foreach(glob($fd.'/*.sql') as $fn) {
82                 
83                  
84                 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
85                     continue;
86                 }
87                 
88                 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
89                 
90                 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
91                 
92                 passthru($cmd);
93             
94                 
95             }
96         }
97         
98         
99         
100     }
101     /**
102      * postgresql import..
103      */
104     function importpgsql($url)
105     {
106         
107         // hide stuff for web..
108         
109         require_once 'System.php';
110         $cat = System::which('cat');
111         $psql = System::which('psql');
112         
113         $ar = $this->modulesList();
114         if (!empty($url['pass'])) { 
115             putenv("PGPASSWORD=". $url['pass']);
116         }
117            
118         $psql_cmd = $psql .
119             ' -h ' . $url['host'] .
120             ' -U' . escapeshellarg($url['user']) .
121              ' ' . basename($url['path']);
122         echo $psql_cmd . "\n" ;
123         
124         
125         
126         
127         foreach($ar as $m) {
128             
129             $fd = $this->rootDir. "/Pman/$m/DataObjects";
130             
131             foreach(glob($fd.'/*.sql') as $bfn) {
132                 
133                  
134                 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
135                     continue;
136                 }
137                 
138                 // files ending in .pg.sql are native postgres files..
139                 $fn = preg_match('/\.pg\.sql$/', $bfn) ? false : $this->convertToPG($bfn);
140                 
141                 $cmd = "$psql_cmd -f  " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
142                 
143                 echo "$bfn:   $cmd ". ($this->cli ? "\n" : "<BR>\n");
144                 
145                 
146                 $res = `$cmd`;
147                 
148                 if ($fn) {
149                     unlink($fn);
150                 }
151             }
152         }
153         
154     }
155     /**
156      * simple regex based convert mysql to pgsql...
157      */
158     function convertToPG($src)
159     {
160         $fn = $this->tempName('sql');
161         
162         $ret = array( ); // pad it a bit.
163         $extra = array("", "" );
164         
165         $tbl = false;
166         foreach(file($src) as $l) {
167             $l = trim($l);
168             
169             if (!strlen($l) || $l[0] == '#') {
170                 continue;
171             }
172             $m = array();
173             if (preg_match('#create\s+table\s+([a-z0-9_]+)#i',  $l, $m)) {
174                 $tbl = $m[1];
175                // $extra[]  =   "drop table {$tbl};";
176              }
177             // autoinc
178             if ($tbl && preg_match('#auto_increment#i',  $l, $m)) {
179                 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
180                 $extra[]  =   "create sequence {$tbl}_seq;";
181               
182             }
183             $m = array();
184             if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i',  $l, $m)) {
185                $l = "CREATE INDEX  {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
186              }
187             // ALTER TABLE core_event_audit ADD     INDEX looku
188             // CREATE INDEX 
189             
190             // basic types..
191             $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
192             
193             $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
194             $l = preg_replace('# blob #i', ' TEXT ', $l);
195              $l = preg_replace('# longtext #i', ' TEXT ', $l);
196             //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
197                             
198             $ret[] = $l;
199             
200             
201             
202             
203             
204             
205             
206         }
207         $ret = array_merge($extra,$ret);
208         
209         //echo implode("\n", $ret); //exit;
210         file_put_contents($fn, implode("\n", $ret));
211         
212         return $fn;
213     }
214                 
215     
216 }