5 * This applies database files from
6 * a) OLD - {MODULE}/DataObjects/XXXX.{dbtype}.sql
8 * b) NEW - {MODULE}/sql/XXX.sql (SHARED or translable)
9 * and {MODULE}/{dbtype}/XXX.sql (SHARED or translable)
14 require_once 'Pman.php';
15 class Pman_Core_UpdateDatabase extends Pman
18 static $cli_desc = "Update SQL - Beta (it will run updateData of all modules)";
26 $ff = HTML_FlexyFramework::get();
27 if (!empty($ff->cli)) {
32 parent::getAuth(); // load company!
33 $au = $this->getAuthUser();
34 if (!$au || $au->company()->comptype != 'OWNER') {
35 $this->jerr("Not authenticated", array('authFailure' => true));
37 $this->authUser = $au;
44 $this->runUpdateModulesData();
51 * imports SQL files from all DataObjects directories....
53 * except any matching /migrate/
58 $ff = HTML_Flexyframework::get();
60 $url = parse_url($ff->DB_DataObject['database']);
62 $this->{'import' . $url['scheme']}($url);
67 * mysql - does not support conversions.
74 function importmysql($url)
77 // hide stuff for web..
79 require_once 'System.php';
80 $cat = System::which('cat');
81 $mysql = System::which('mysql');
83 $ar = $this->modulesList();
87 ' -h ' . $url['host'] .
88 ' -u' . escapeshellarg($url['user']) .
89 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
90 ' ' . basename($url['path']);
91 echo $mysql_cmd . "\n" ;
94 // old -- DAtaObjects/*.sql
98 $fd = $this->rootDir. "/Pman/$m/DataObjects";
100 foreach(glob($fd.'/*.sql') as $fn) {
103 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
106 // .my.sql but not .pg.sql
107 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
108 && !preg_match('#\.my\.sql#i', basename($fn))
109 ) { // skip migration scripts at present..
112 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
114 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
120 // new -- sql directory..
121 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
122 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
123 $fd = $this->rootDir. "/Pman/$m/sql";
125 foreach(glob($fd.'/*.sql') as $fn) {
126 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
127 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
130 $fd = $this->rootDir. "/Pman/$m/mysql";
132 foreach(glob($fd.'/*.sql') as $fn) {
133 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
134 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
149 * postgresql import..
151 function importpgsql($url)
154 // hide stuff for web..
156 require_once 'System.php';
157 $cat = System::which('cat');
158 $psql = System::which('psql');
160 $ar = $this->modulesList();
161 if (!empty($url['pass'])) {
162 putenv("PGPASSWORD=". $url['pass']);
166 ' -h ' . $url['host'] .
167 ' -U' . escapeshellarg($url['user']) .
168 ' ' . basename($url['path']);
169 echo $psql_cmd . "\n" ;
176 $fd = $this->rootDir. "/Pman/$m/DataObjects";
178 foreach(glob($fd.'/*.sql') as $bfn) {
181 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
184 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
185 && !preg_match('#\.pg\.sql#i', basename($bfn))
186 ) { // skip migration scripts at present..
189 // files ending in .pg.sql are native postgres files..
190 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
192 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
194 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
206 $fd = $this->rootDir. "/Pman/$m/sql";
207 // sql directory - we try to convert..
208 foreach(glob($fd.'/*.sql') as $bfn) {
209 $fn = $this->convertToPG($bfn);
210 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
211 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
215 // postgres specific directory..
217 $fd = $this->rootDir. "/Pman/$m/pgsql";
219 foreach(glob($fd.'/*.sql') as $fn) {
220 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
221 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
231 * simple regex based convert mysql to pgsql...
233 function convertToPG($src)
235 $fn = $this->tempName('sql');
237 $ret = array( ); // pad it a bit.
238 $extra = array("", "" );
241 foreach(file($src) as $l) {
244 if (!strlen($l) || $l[0] == '#') {
248 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
250 // $extra[] = "drop table {$tbl};";
253 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
254 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
255 $extra[] = "create sequence {$tbl}_seq;";
259 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
260 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
262 // ALTER TABLE core_event_audit ADD INDEX looku
266 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
268 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
269 $l = preg_replace('# blob #i', ' TEXT ', $l);
270 $l = preg_replace('# longtext #i', ' TEXT ', $l);
271 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
282 $ret = array_merge($extra,$ret);
284 //echo implode("\n", $ret); //exit;
285 file_put_contents($fn, implode("\n", $ret));
290 function runUpdateModulesData()
293 $modules = $this->modulesList();
294 foreach ($modules as $module){
295 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
296 if($module == 'Core' || !file_exists($file)){
300 $class = "Pman_{$module}_UpdateDatabase";
302 if(!method_exists($x, 'updateData')){
310 function updateData()
312 $enum = DB_DataObject::Factory('core_enum');
316 'name' => 'COMPTYPE',
317 'display_name' => 'Company Types',
321 'display_name' => 'Owner',
322 'seqid' => 999, // last...