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)',
64 $ff = HTML_FlexyFramework::get();
65 if (!empty($ff->cli)) {
70 parent::getAuth(); // load company!
71 $au = $this->getAuthUser();
72 if (!$au || $au->company()->comptype != 'OWNER') {
73 $this->jerr("Not authenticated", array('authFailure' => true));
75 $this->authUser = $au;
79 function get($args, $opt)
81 if($args == 'Person'){
82 if(empty($opt['source']) || empty($opt['prefix'])){
83 die("Missing Source directory for person json files or prefix for the passwrod! Try -f [JSON file path] -p [prefix] \n");
85 if (!file_exists($opt['source'])) {
86 die("can not found person json file : {$opt['source']} \n");
89 $persons = json_decode(file_get_contents($opt['source']),true);
91 DB_DataObject::factory('person')->importFromArray($this, $persons, $opt['prefix']);
95 if($args == 'Company'){
96 if(empty($opt['name']) || empty($opt['comptype'])){
97 die("Missing company name or type! Try --name=[the name of company] -- comptype=[the type of company] \n");
100 DB_DataObject::factory('companies')->initCompanies($this, $opt['name'], $opt['comptype']);
106 $this->runUpdateModulesData();
113 * imports SQL files from all DataObjects directories....
115 * except any matching /migrate/
120 $ff = HTML_Flexyframework::get();
122 $url = parse_url($ff->DB_DataObject['database']);
124 $this->{'import' . $url['scheme']}($url);
129 * mysql - does not support conversions.
135 function importmysqldir($dburl, $dir)
137 echo "Import MYSQL :: $dir\n";
140 require_once 'System.php';
141 $cat = System::which('cat');
142 $mysql = System::which('mysql');
146 $mysql_cmd = $mysql .
147 ' -h ' . $dburl['host'] .
148 ' -u' . escapeshellarg($dburl['user']) .
149 (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass']) : '') .
150 ' ' . basename($dburl['path']);
151 //echo $mysql_cmd . "\n" ;
154 foreach(glob($dir.'/*.sql') as $fn) {
157 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
160 // .my.sql but not .pg.sql
161 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
162 && !preg_match('#\.my\.sql#i', basename($fn))
163 ) { // skip migration scripts at present..
166 if (!strlen(trim($fn))) {
170 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
172 echo basename($dir).'/'. basename($fn) . '::' . $cmd. ($this->cli ? "\n" : "<BR>\n");
185 function importmysql($dburl)
188 // hide stuff for web..
189 $ar = $this->modulesList();
193 // old -- DAtaObjects/*.sql
197 $fd = $this->rootDir. "/Pman/$m/DataObjects";
199 $this->importmysqldir($dburl, $fd);
201 // new -- sql directory..
202 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
203 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
205 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/sql");
206 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/mysql");
215 * postgresql import..
217 function importpgsql($url)
220 // hide stuff for web..
225 $ar = $this->modulesList();
229 $fd = $this->rootDir. "/Pman/$m/DataObjects";
231 $this->importpgsqldir($dburl, $fd);
233 // new -- sql directory..
234 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
235 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
237 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/sql");
238 $this->importpgsqldir($dburl, $this->rootDir. "/Pman/$m/pgsql");
245 function importpgsqldir($url, $dir)
247 require_once 'System.php';
248 $cat = System::which('cat');
249 $psql = System::which('psql');
252 if (!empty($url['pass'])) {
253 putenv("PGPASSWORD=". $url['pass']);
257 ' -h ' . $url['host'] .
258 ' -U' . escapeshellarg($url['user']) .
259 ' ' . basename($url['path']);
262 echo $psql_cmd . "\n" ;
263 echo "scan : $dir\n";
264 foreach(glob($dir.'/*.sql') as $bfn) {
267 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
270 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
271 && !preg_match('#\.pg\.sql#i', basename($bfn))
272 ) { // skip migration scripts at present..
277 if (basename($dir) != 'pgsql') {
278 if ( !preg_match('#\.pg\.sql$#', basename($bfn))) {
279 $fn = $this->convertToPG($bfn);
283 // files ending in .pg.sql are native postgres files.. ## depricated
286 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
288 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
303 * simple regex based convert mysql to pgsql...
305 function convertToPG($src)
307 //echo "Convert $src\n";
309 $fn = $this->tempName('sql');
311 $ret = array( ); // pad it a bit.
312 $extra = array("", "" );
315 foreach(file($src) as $l) {
318 if (!strlen($l) || $l[0] == '#') {
322 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
325 if (preg_match('#create\s+table\s+\`([a-z0-9_]+)\`#i', $l, $m)) {
326 $tbl = 'shop_' . strtolower($m[1]);
327 $l = preg_replace('#create\s+table\s+\`([a-z0-9_]+)\`#i', "CREATE TABLE {$tbl}", $l);
329 if (preg_match('#\`([a-z0-9_]+)\`#i', $l, $m) && !preg_match('#alter\s+table\s+#i', $l)) {
330 $l = preg_replace('#\`([a-z0-9_]+)\`#i', "{$m[1]}_name", $l);
333 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
334 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
335 $extra[] = "create sequence {$tbl}_seq;";
339 if (preg_match('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', $l, $m)){
340 $l = preg_replace('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', "ALTER TABLE {$tbl}", $l);
343 // enum value -- use the text instead..
345 if ($tbl && preg_match('#([\w]+)\s+(enum\([\w|\W]+\))#i', $l, $m)) {
346 $l = preg_replace('#enum\([\w|\W]+\)#i', "TEXT", $l);
348 // ignore the alter enum
349 if ($tbl && preg_match('#alter\s+table\s+([\w|\W]+)\s+enum\([\w|\W]+\)#i', $l, $m)) {
353 // UNIQUE KEY .. ignore
354 if ($tbl && preg_match('#UNIQUE KEY#i', $l, $m)) {
355 $last = array_pop($ret);
356 $ret[] = trim($last, ",");
360 if ($tbl && preg_match('#RENAME\s+TO#i', $l, $m)) {
364 if ($tbl && preg_match('#change\s+column#i', $l, $m)) {
368 // INDEX lookup ..ignore
369 if ($tbl && preg_match('#INDEX lookup+([\w|\W]+)#i', $l, $m)) {
370 $last = array_pop($ret);
371 $ret[] = trim($last, ",");
376 // CREATE INDEX ..ignore
377 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+#i', $l, $m)) {
378 // $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
383 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
385 $l = preg_replace('# datetime#i', ' TIMESTAMP WITHOUT TIME ZONE', $l);
386 $l = preg_replace('# blob#i', ' TEXT', $l);
387 $l = preg_replace('# longtext#i', ' TEXT', $l);
388 $l = preg_replace('# tinyint#i', ' INT', $l);
394 $ret = array_merge($extra,$ret);
395 // echo implode("\n", $ret); exit;
397 file_put_contents($fn, implode("\n", $ret));
402 function runUpdateModulesData()
406 $modules = array_reverse($this->modulesList());
408 // move 'project' one to the end...
410 foreach ($modules as $module){
411 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
412 if($module == 'Core' || !file_exists($file)){
416 $class = "Pman_{$module}_UpdateDatabase";
418 if(!method_exists($x, 'updateData')){
427 function updateDataEnums()
429 $enum = DB_DataObject::Factory('core_enum');
434 'name' => 'COMPTYPE',
435 'display_name' => 'Company Types',
436 'is_system_enum' => 1,
440 'display_name' => 'Owner',
441 'seqid' => 999, // last...
442 'is_system_enum' => 1,
449 'name' => 'HtmlEditor.font-family',
450 'display_name' => 'HTML Editor font families',
451 'is_system_enum' => 1,
454 'name' => 'Helvetica,Arial,sans-serif',
455 'display_name' => 'Helvetica',
460 'name' => 'Courier New',
461 'display_name' => 'Courier',
466 'display_name' => 'Tahoma',
470 'name' => 'Times New Roman,serif',
471 'display_name' => 'Times',
476 'display_name' => 'Verdana',
488 function updateDataGroups()
491 $groups = DB_DataObject::factory('groups');
492 $groups->initGroups();
494 $groups->initDatabase($this,array(
496 'name' => 'bcc-email', // group who are bcc'ed on all requests.
497 'type' => 0, // system
504 function updateDataCompanies()
507 // fix comptypes enums..
508 $c = DB_DataObject::Factory('Companies');
510 $c->selectAdd('distinct(comptype) as comptype');
511 $c->whereAdd("comptype != ''");
514 foreach($c->fetchAll('comptype') as $cts) {
518 $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
521 $c = DB_DataObject::Factory('core_enum');
524 //DB_DataObject::debugLevel(1);
526 $c = DB_DataObject::Factory('Companies');
530 comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype)
543 function updateData()
547 $this->updateDataEnums();
548 $this->updateDataGroups();
549 $this->updateDataCompanies();
551 $c = DB_DataObject::Factory('I18n');