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)";
20 static $cli_opts = array(
22 'desc' => 'Source directory for json files.',
29 'desc' => 'prefix for the passwrod',
36 'desc' => 'name of the company',
43 'desc' => 'the type of company',
56 $ff = HTML_FlexyFramework::get();
57 if (!empty($ff->cli)) {
62 parent::getAuth(); // load company!
63 $au = $this->getAuthUser();
64 if (!$au || $au->company()->comptype != 'OWNER') {
65 $this->jerr("Not authenticated", array('authFailure' => true));
67 $this->authUser = $au;
71 function get($args, $opt)
73 if($args == 'Person'){
74 if(empty($opt['source']) || empty($opt['prefix'])){
75 die("Missing Source directory for person json files or prefix for the passwrod! Try -f [JSON file path] -p [prefix] \n");
77 if (!file_exists($opt['source'])) {
78 die("can not found person json file : {$opt['source']} \n");
81 $persons = json_decode(file_get_contents($opt['source']),true);
83 DB_DataObject::factory('person')->importFromArray($this, $persons, $opt['prefix']);
87 if($args == 'Company'){
88 if(empty($opt['name']) || empty($opt['comptype'])){
89 die("Missing company name or type! Try --name=[the name of company] -- comptype=[the type of company] \n");
92 DB_DataObject::factory('companies')->initCompanies($this, $opt['name'], $opt['comptype']);
98 $this->runUpdateModulesData();
105 * imports SQL files from all DataObjects directories....
107 * except any matching /migrate/
112 $ff = HTML_Flexyframework::get();
114 $url = parse_url($ff->DB_DataObject['database']);
116 $this->{'import' . $url['scheme']}($url);
121 * mysql - does not support conversions.
127 function importmysqldir($dburl, $dir)
129 echo "Import MYSQL :: $dir\n";
132 require_once 'System.php';
133 $cat = System::which('cat');
134 $mysql = System::which('mysql');
138 $mysql_cmd = $mysql .
139 ' -h ' . $dburl['host'] .
140 ' -u' . escapeshellarg($dburl['user']) .
141 (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass']) : '') .
142 ' ' . basename($dburl['path']);
143 //echo $mysql_cmd . "\n" ;
146 foreach(glob($dir.'/*.sql') as $fn) {
149 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
152 // .my.sql but not .pg.sql
153 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
154 && !preg_match('#\.my\.sql#i', basename($fn))
155 ) { // skip migration scripts at present..
158 if (!strlen(trim($fn))) {
162 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
164 echo basename($dir).'/'. basename($fn) . '::' . $cmd. ($this->cli ? "\n" : "<BR>\n");
177 function importmysql($dburl)
180 // hide stuff for web..
181 $ar = $this->modulesList();
185 // old -- DAtaObjects/*.sql
189 $fd = $this->rootDir. "/Pman/$m/DataObjects";
191 $this->importmysqldir($dburl, $fd);
193 // new -- sql directory..
194 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
195 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
197 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/sql");
198 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/mysql");
207 * postgresql import..
209 function importpgsql($url)
212 // hide stuff for web..
217 $ar = $this->modulesList();
221 $fd = $this->rootDir. "/Pman/$m/DataObjects";
223 $this->importpgsqldir($dburl, $fd);
225 // new -- sql directory..
226 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
227 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
229 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/sql");
230 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql");
237 function importpgsqldir($url, $dir)
239 require_once 'System.php';
240 $cat = System::which('cat');
241 $psql = System::which('psql');
244 if (!empty($url['pass'])) {
245 putenv("PGPASSWORD=". $url['pass']);
249 ' -h ' . $url['host'] .
250 ' -U' . escapeshellarg($url['user']) .
251 ' ' . basename($url['path']);
252 echo $psql_cmd . "\n" ;
253 foreach(glob($dir.'/*.sql') as $bfn) {
256 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
259 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
260 && !preg_match('#\.pg\.sql#i', basename($bfn))
261 ) { // skip migration scripts at present..
266 if (basename($dir) != 'pgsql') {
267 if ( !preg_match('#\.pg\.sql$#', basename($bfn))) {
268 $fn = $this->convertToPG($bfn);
272 // files ending in .pg.sql are native postgres files.. ## depricated
275 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
277 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
292 * simple regex based convert mysql to pgsql...
294 function convertToPG($src)
296 //echo "Convert $src\n";
298 $fn = $this->tempName('sql');
300 $ret = array( ); // pad it a bit.
301 $extra = array("", "" );
304 foreach(file($src) as $l) {
307 if (!strlen($l) || $l[0] == '#') {
311 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
314 if (preg_match('#create\s+table\s+\`([a-z0-9_]+)\`#i', $l, $m)) {
315 $tbl = 'shop_' . strtolower($m[1]);
316 $l = preg_replace('#\`{0,1}([a-z0-9_]+)\`{0,1}#i', "{$tbl}", $l);
318 if (preg_match('#\`([a-z0-9_]+)\`#i', $l, $m)) {
319 $l = preg_replace('#\`([a-z0-9_]+)\`#i', "{$m[1]}_name", $l);
324 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
325 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
326 $extra[] = "create sequence {$tbl}_seq;";
331 if ($tbl && preg_match('#alter\s+table\s+#i', $l, $m)) {
333 if ($tbl && preg_match('#([\w]+)\s+(enum\([\w|\W]+\))#i', $l, $m)) {
334 $extra[] = "CREATE TYPE {$tbl}_{$m[1]}_enum AS {$m[2]};";
340 if ($tbl && preg_match('#([\w]+)\s+(enum\([\w|\W]+\))#i', $l, $m)) {
341 $l = preg_replace('#enum\([\w|\W]+\)#i', "{$tbl}_{$m[1]}_enum", $l);
345 // UNIQUE KEY .. ignore
346 if ($tbl && preg_match('#UNIQUE KEY#i', $l, $m)) {
347 // preg_match('#UNIQUE\s+KEY\s+([\w]+)\s+([\w|\W]+)#i', $l, $m);
349 // $unique[] = 'CREATE UNIQUE INDEX ' . $m[1] . '_idx ON "' . $tbl . '" USING btree ' . $m[2] . ';';
350 $last = array_pop($ret);
351 $ret[] = trim($last, ",");
355 // INDEX lookup ..ignore
356 if ($tbl && preg_match('#INDEX lookup+([\w|\W]+)#i', $l, $m)) {
357 // $unique[] = 'CREATE INDEX lookup_idx ON "' . $tbl . '" USING btree ' . $m[1] . ';';
358 $last = array_pop($ret);
359 $ret[] = trim($last, ",");
365 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
366 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
368 // ALTER TABLE core_event_audit ADD INDEX looku
372 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
374 $l = preg_replace('# datetime#i', ' TIMESTAMP WITHOUT TIME ZONE', $l);
375 $l = preg_replace('# blob#i', ' TEXT', $l);
376 $l = preg_replace('# longtext#i', ' TEXT', $l);
377 $l = preg_replace('# tinyint#i', ' BOOLEAN', $l);
380 if(preg_match('#([\w]+)\s+([\w|\W]+)#i', $l, $m) && !preg_match('#CREATE|PRIMARY|ALERT#i', $l)){
381 if($m[1] == 'group'){
382 $m[1] = 'group_name';
385 $l = "{$m[1]} {$m[2]}";
392 $ret = array_merge($extra,$ret);
393 echo implode("\n", $ret); exit;
395 file_put_contents($fn, implode("\n", $ret));
400 function runUpdateModulesData()
404 $modules = array_reverse($this->modulesList());
406 // move 'project' one to the end...
408 foreach ($modules as $module){
409 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
410 if($module == 'Core' || !file_exists($file)){
414 $class = "Pman_{$module}_UpdateDatabase";
416 if(!method_exists($x, 'updateData')){
425 function updateDataEnums()
427 $enum = DB_DataObject::Factory('core_enum');
432 'name' => 'COMPTYPE',
433 'display_name' => 'Company Types',
434 'is_system_enum' => 1,
438 'display_name' => 'Owner',
439 'seqid' => 999, // last...
440 'is_system_enum' => 1,
447 'name' => 'HtmlEditor.font-family',
448 'display_name' => 'HTML Editor font families',
449 'is_system_enum' => 1,
452 'name' => 'Helvetica,Arial,sans-serif',
453 'display_name' => 'Helvetica',
458 'name' => 'Courier New',
459 'display_name' => 'Courier',
464 'display_name' => 'Tahoma',
468 'name' => 'Times New Roman,serif',
469 'display_name' => 'Times',
474 'display_name' => 'Verdana',
486 function updateDataGroups()
489 $groups = DB_DataObject::factory('groups');
490 $groups->initGroups();
492 $groups->initDatabase($this,array(
494 'name' => 'bcc-email', // group who are bcc'ed on all requests.
495 'type' => 0, // system
502 function updateDataCompanies()
505 // fix comptypes enums..
506 $c = DB_DataObject::Factory('Companies');
508 $c->selectAdd('distinct(comptype) as comptype');
509 $c->whereAdd("comptype != ''");
512 foreach($c->fetchAll('comptype') as $cts) {
516 $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
519 $c = DB_DataObject::Factory('core_enum');
522 //DB_DataObject::debugLevel(1);
524 $c = DB_DataObject::Factory('Companies');
528 comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype)
541 function updateData()
545 $this->updateDataEnums();
546 $this->updateDataGroups();
547 $this->updateDataCompanies();