DataObjects/Core_watch.php
[Pman.Core] / UpdateDatabase.php
1 <?php
2
3 /**
4  *
5  * This applies database files from
6  * a) OLD - {MODULE}/DataObjects/XXXX.{dbtype}.sql
7  *
8  * b) NEW - {MODULE}/sql/XXX.sql (SHARED or translable)
9  *  and {MODULE}/{dbtype}/XXX.sql (SHARED or translable)
10  *
11  *
12  */
13
14 require_once 'Pman.php';
15 class Pman_Core_UpdateDatabase extends Pman
16 {
17     
18     static $cli_desc = "Update SQL - Beta";
19  
20  
21     
22     var $cli = false;
23     function getAuth() {
24         
25         
26         $ff = HTML_FlexyFramework::get();
27         if (!empty($ff->cli)) {
28             $this->cli = true;
29             return true;
30         }
31         
32         parent::getAuth(); // load company!
33         $au = $this->getAuthUser();
34         if (!$au || $au->company()->comptype != 'OWNER') {
35             $this->jerr("Not authenticated", array('authFailure' => true));
36         }
37         $this->authUser = $au;
38         return true;
39     }
40      
41     function get()
42     {
43         $this->importSQL();
44          
45     }
46     function output() {
47         return '';
48     }
49      /**
50      * imports SQL files from all DataObjects directories....
51      * 
52      * except any matching /migrate/
53      */
54     function importSQL()
55     {
56         
57         $ff = HTML_Flexyframework::get();
58         
59         $url = parse_url($ff->DB_DataObject['database']);
60         
61         $this->{'import' . $url['scheme']}($url);
62         
63     }
64     
65     /**
66      * mysql - does not support conversions.
67      * 
68      *
69      */ 
70     
71     function importmysql($url)
72     {
73         
74         // hide stuff for web..
75         
76         require_once 'System.php';
77         $cat = System::which('cat');
78         $mysql = System::which('mysql');
79         
80         $ar = $this->modulesList();
81         
82            
83         $mysql_cmd = $mysql .
84             ' -h ' . $url['host'] .
85             ' -u' . escapeshellarg($url['user']) .
86             (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass'])  :  '') .
87             ' ' . basename($url['path']);
88         echo $mysql_cmd . "\n" ;
89         
90         
91         // old -- DAtaObjects/*.sql
92         
93         foreach($ar as $m) {
94             
95             $fd = $this->rootDir. "/Pman/$m/DataObjects";
96             
97             foreach(glob($fd.'/*.sql') as $fn) {
98                 
99                  
100                 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
101                     continue;
102                 }
103                 // .my.sql but not .pg.sql
104                 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
105                     && !preg_match('#\.my\.sql#i', basename($fn))
106                 ) { // skip migration scripts at present..
107                     continue;
108                 }
109                 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
110                 
111                 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
112                 
113                 passthru($cmd);
114             
115                 
116             }
117             // new -- sql directory..
118             // new style will not support migrate ... they have to go into mysql-migrate.... directories..
119             // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
120             $fd = $this->rootDir. "/Pman/$m/sql";
121             
122             foreach(glob($fd.'/*.sql') as $fn) {
123                 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
124                 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
125                 passthru($cmd);
126             }
127             $fd = $this->rootDir. "/Pman/$m/mysql";
128             
129             foreach(glob($fd.'/*.sql') as $fn) {
130                 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
131                 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
132                 passthru($cmd);
133             }
134               
135             
136             
137             
138             
139             
140         }
141         
142         
143         
144     }
145     /**
146      * postgresql import..
147      */
148     function importpgsql($url)
149     {
150         
151         // hide stuff for web..
152         
153         require_once 'System.php';
154         $cat = System::which('cat');
155         $psql = System::which('psql');
156         
157         $ar = $this->modulesList();
158         if (!empty($url['pass'])) { 
159             putenv("PGPASSWORD=". $url['pass']);
160         }
161            
162         $psql_cmd = $psql .
163             ' -h ' . $url['host'] .
164             ' -U' . escapeshellarg($url['user']) .
165              ' ' . basename($url['path']);
166         echo $psql_cmd . "\n" ;
167         
168         
169         
170         
171         foreach($ar as $m) {
172             
173             $fd = $this->rootDir. "/Pman/$m/DataObjects";
174             
175             foreach(glob($fd.'/*.sql') as $bfn) {
176                 
177                  
178                 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
179                     continue;
180                 }
181                 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
182                     && !preg_match('#\.pg\.sql#i', basename($bfn))
183                 ) { // skip migration scripts at present..
184                     continue;
185                 }
186                 // files ending in .pg.sql are native postgres files..
187                 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
188                 
189                 $cmd = "$psql_cmd  < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
190                 
191                 echo "$bfn:   $cmd ". ($this->cli ? "\n" : "<BR>\n");
192                 
193                 
194                 passthru($cmd);
195                 
196                 if ($fn) {
197                     unlink($fn);
198                 }
199             }
200             
201             
202             
203             $fd = $this->rootDir. "/Pman/$m/sql";
204             // sql directory  - we try to convert..
205             foreach(glob($fd.'/*.sql') as $bfn) {
206                 $fn =  $this->convertToPG($bfn);
207                 $cmd = "$psql_cmd  < " . escapeshellarg($fn) ;
208                 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
209                 passthru($cmd);
210             }
211             
212             // postgres specific directory..
213             
214             $fd = $this->rootDir. "/Pman/$m/pgsql";
215             
216             foreach(glob($fd.'/*.sql') as $fn) {
217                 $cmd = "$psql_cmd   < " . escapeshellarg($fn) ;
218                 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
219                 passthru($cmd);
220             }
221             
222             
223             
224         }
225         
226     }
227     /**
228      * simple regex based convert mysql to pgsql...
229      */
230     function convertToPG($src)
231     {
232         $fn = $this->tempName('sql');
233         
234         $ret = array( ); // pad it a bit.
235         $extra = array("", "" );
236         
237         $tbl = false;
238         foreach(file($src) as $l) {
239             $l = trim($l);
240             
241             if (!strlen($l) || $l[0] == '#') {
242                 continue;
243             }
244             $m = array();
245             if (preg_match('#create\s+table\s+([a-z0-9_]+)#i',  $l, $m)) {
246                 $tbl = $m[1];
247                // $extra[]  =   "drop table {$tbl};";
248              }
249             // autoinc
250             if ($tbl && preg_match('#auto_increment#i',  $l, $m)) {
251                 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
252                 $extra[]  =   "create sequence {$tbl}_seq;";
253               
254             }
255             $m = array();
256             if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i',  $l, $m)) {
257                $l = "CREATE INDEX  {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
258              }
259             // ALTER TABLE core_event_audit ADD     INDEX looku
260             // CREATE INDEX 
261             
262             // basic types..
263             $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
264             
265             $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
266             $l = preg_replace('# blob #i', ' TEXT ', $l);
267              $l = preg_replace('# longtext #i', ' TEXT ', $l);
268             //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
269                             
270             $ret[] = $l;
271             
272             
273             
274             
275             
276             
277             
278         }
279         $ret = array_merge($extra,$ret);
280         
281         //echo implode("\n", $ret); //exit;
282         file_put_contents($fn, implode("\n", $ret));
283         
284         return $fn;
285     }
286                 
287     
288 }