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',
50 'desc' => 'Initialize the database (pg only supported)',
60 var $cli_options = false;
66 $ff = HTML_FlexyFramework::get();
67 if (!empty($ff->cli)) {
72 parent::getAuth(); // load company!
73 $au = $this->getAuthUser();
74 if (!$au || $au->company()->comptype != 'OWNER') {
75 $this->jerr("Not authenticated", array('authFailure' => true));
77 $this->authUser = $au;
81 function get($args, $opt)
84 $this->cli_options = $opt;
86 if($args == 'Person'){
87 if(empty($opt['source']) || empty($opt['prefix'])){
88 die("Missing Source directory for person json files or prefix for the passwrod! Try -f [JSON file path] -p [prefix] \n");
90 if (!file_exists($opt['source'])) {
91 die("can not found person json file : {$opt['source']} \n");
94 $persons = json_decode(file_get_contents($opt['source']),true);
96 DB_DataObject::factory('person')->importFromArray($this, $persons, $opt['prefix']);
102 if($args == 'Company'){
103 if(empty($opt['name']) || empty($opt['comptype'])){
104 die("Missing company name or type! Try --name=[the name of company] -- comptype=[the type of company] \n");
107 DB_DataObject::factory('companies')->initCompanies($this, $opt['name'], $opt['comptype']);
113 $this->runUpdateModulesData();
120 * imports SQL files from all DataObjects directories....
122 * except any matching /migrate/
127 $ff = HTML_Flexyframework::get();
129 $url = parse_url($ff->DB_DataObject['database']);
131 $this->{'import' . $url['scheme']}($url);
136 * mysql - does not support conversions.
142 function importmysqldir($dburl, $dir)
144 echo "Import MYSQL :: $dir\n";
147 require_once 'System.php';
148 $cat = System::which('cat');
149 $mysql = System::which('mysql');
153 $mysql_cmd = $mysql .
154 ' -h ' . $dburl['host'] .
155 ' -u' . escapeshellarg($dburl['user']) .
156 (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass']) : '') .
157 ' ' . basename($dburl['path']);
158 //echo $mysql_cmd . "\n" ;
161 foreach(glob($dir.'/*.sql') as $fn) {
164 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
167 // .my.sql but not .pg.sql
168 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
169 && !preg_match('#\.my\.sql#i', basename($fn))
170 ) { // skip migration scripts at present..
173 if (!strlen(trim($fn))) {
177 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
179 echo basename($dir).'/'. basename($fn) . '::' . $cmd. ($this->cli ? "\n" : "<BR>\n");
192 function importmysql($dburl)
195 // hide stuff for web..
196 $ar = $this->modulesList();
200 // old -- DAtaObjects/*.sql
204 $fd = $this->rootDir. "/Pman/$m/DataObjects";
206 $this->importmysqldir($dburl, $fd);
208 // new -- sql directory..
209 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
210 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
212 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/sql");
213 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/mysql");
222 * postgresql import..
224 function importpgsql($dburl)
227 // hide stuff for web..
232 $ar = $this->modulesList();
236 // if init has been called
238 if (!empty($this->cli_options['init'])) {
239 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.init");
240 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql.initdata");
245 $fd = $this->rootDir. "/Pman/$m/DataObjects";
247 $this->importpgsqldir($dburl, $fd);
249 // new -- sql directory..
250 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
251 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
253 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/sql");
254 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql");
261 function importpgsqldir($url, $dir, $disable_triggers = false)
263 require_once 'System.php';
264 $cat = System::which('cat');
265 $psql = System::which('psql');
268 if (!empty($url['pass'])) {
269 putenv("PGPASSWORD=". $url['pass']);
273 ' -h ' . $url['host'] .
274 ' -U' . escapeshellarg($url['user']) .
275 ' ' . basename($url['path']);
278 echo $psql_cmd . "\n" ;
279 echo "scan : $dir\n";
280 foreach(glob($dir.'/*.sql') as $bfn) {
283 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
286 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
287 && !preg_match('#\.pg\.sql#i', basename($bfn))
288 ) { // skip migration scripts at present..
293 if (!preg_match('/pgsql/', basename($dir) )) {
294 if ( !preg_match('#\.pg\.sql$#', basename($bfn))) {
295 $fn = $this->convertToPG($bfn);
299 // files ending in .pg.sql are native postgres files.. ## depricated
302 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
304 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
319 * simple regex based convert mysql to pgsql...
321 function convertToPG($src)
323 //echo "Convert $src\n";
325 $fn = $this->tempName('sql');
327 $ret = array( ); // pad it a bit.
328 $extra = array("", "" );
331 foreach(file($src) as $l) {
334 if (!strlen($l) || $l[0] == '#') {
338 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
341 if (preg_match('#create\s+table\s+\`([a-z0-9_]+)\`#i', $l, $m)) {
342 $tbl = 'shop_' . strtolower($m[1]);
343 $l = preg_replace('#create\s+table\s+\`([a-z0-9_]+)\`#i', "CREATE TABLE {$tbl}", $l);
345 if (preg_match('#\`([a-z0-9_]+)\`#i', $l, $m) && !preg_match('#alter\s+table\s+#i', $l)) {
346 $l = preg_replace('#\`([a-z0-9_]+)\`#i', "{$m[1]}_name", $l);
349 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
350 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
351 $extra[] = "create sequence {$tbl}_seq;";
355 if (preg_match('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', $l, $m)){
356 $l = preg_replace('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', "ALTER TABLE {$tbl}", $l);
359 // enum value -- use the text instead..
361 if ($tbl && preg_match('#([\w]+)\s+(enum\([\w|\W]+\))#i', $l, $m)) {
362 $l = preg_replace('#enum\([\w|\W]+\)#i', "TEXT", $l);
364 // ignore the alter enum
365 if ($tbl && preg_match('#alter\s+table\s+([\w|\W]+)\s+enum\([\w|\W]+\)#i', $l, $m)) {
369 // UNIQUE KEY .. ignore
370 if ($tbl && preg_match('#UNIQUE KEY#i', $l, $m)) {
371 $last = array_pop($ret);
372 $ret[] = trim($last, ",");
376 if ($tbl && preg_match('#RENAME\s+TO#i', $l, $m)) {
380 if ($tbl && preg_match('#change\s+column#i', $l, $m)) {
384 // INDEX lookup ..ignore
385 if ($tbl && preg_match('#INDEX lookup+([\w|\W]+)#i', $l, $m)) {
386 $last = array_pop($ret);
387 $ret[] = trim($last, ",");
392 // CREATE INDEX ..ignore
393 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+#i', $l, $m)) {
394 // $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
399 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
401 $l = preg_replace('# datetime#i', ' TIMESTAMP WITHOUT TIME ZONE', $l);
402 $l = preg_replace('# blob#i', ' TEXT', $l);
403 $l = preg_replace('# longtext#i', ' TEXT', $l);
404 $l = preg_replace('# tinyint#i', ' INT', $l);
410 $ret = array_merge($extra,$ret);
411 // echo implode("\n", $ret); exit;
413 file_put_contents($fn, implode("\n", $ret));
418 function runUpdateModulesData()
422 $modules = array_reverse($this->modulesList());
424 // move 'project' one to the end...
426 foreach ($modules as $module){
427 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
428 if($module == 'Core' || !file_exists($file)){
432 $class = "Pman_{$module}_UpdateDatabase";
434 if(!method_exists($x, 'updateData')){
443 function updateDataEnums()
445 $enum = DB_DataObject::Factory('core_enum');
450 'name' => 'COMPTYPE',
451 'display_name' => 'Company Types',
452 'is_system_enum' => 1,
456 'display_name' => 'Owner',
457 'seqid' => 999, // last...
458 'is_system_enum' => 1,
465 'name' => 'HtmlEditor.font-family',
466 'display_name' => 'HTML Editor font families',
467 'is_system_enum' => 1,
470 'name' => 'Helvetica,Arial,sans-serif',
471 'display_name' => 'Helvetica',
476 'name' => 'Courier New',
477 'display_name' => 'Courier',
482 'display_name' => 'Tahoma',
486 'name' => 'Times New Roman,serif',
487 'display_name' => 'Times',
492 'display_name' => 'Verdana',
504 function updateDataGroups()
507 $groups = DB_DataObject::factory('groups');
508 $groups->initGroups();
510 $groups->initDatabase($this,array(
512 'name' => 'bcc-email', // group who are bcc'ed on all requests.
513 'type' => 0, // system
520 function updateDataCompanies()
523 // fix comptypes enums..
524 $c = DB_DataObject::Factory('Companies');
526 $c->selectAdd('distinct(comptype) as comptype');
527 $c->whereAdd("comptype != ''");
530 foreach($c->fetchAll('comptype') as $cts) {
534 $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
537 $c = DB_DataObject::Factory('core_enum');
540 //DB_DataObject::debugLevel(1);
542 $c = DB_DataObject::Factory('Companies');
546 comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype)
559 function updateData()
563 $this->updateDataEnums();
564 $this->updateDataGroups();
565 $this->updateDataCompanies();
567 $c = DB_DataObject::Factory('I18n');