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)
130 require_once 'System.php';
131 $cat = System::which('cat');
132 $mysql = System::which('mysql');
136 $mysql_cmd = $mysql .
137 ' -h ' . $dburl['host'] .
138 ' -u' . escapeshellarg($dburl['user']) .
139 (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass']) : '') .
140 ' ' . basename($dburl['path']);
141 echo $mysql_cmd . "\n" ;
144 foreach(glob($dir.'/*.sql') as $fn) {
147 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
150 // .my.sql but not .pg.sql
151 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
152 && !preg_match('#\.my\.sql#i', basename($fn))
153 ) { // skip migration scripts at present..
156 $cmd = basename($dir).'/'. basename($fn) . " :: $mysql_cmd -f < " . escapeshellarg($fn) ;
158 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
171 function importmysql($dburl)
174 // hide stuff for web..
175 $ar = $this->modulesList();
179 // old -- DAtaObjects/*.sql
183 $fd = $this->rootDir. "/Pman/$m/DataObjects";
185 $this->importmysqldir($dburl, $fd);
187 // new -- sql directory..
188 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
189 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
191 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/sql");
192 $this->importmysqldir($dburl, $this->rootDir. "/Pman/$m/mysql");
201 * postgresql import..
203 function importpgsql($url)
206 // hide stuff for web..
208 require_once 'System.php';
209 $cat = System::which('cat');
210 $psql = System::which('psql');
212 $ar = $this->modulesList();
214 if (!empty($url['pass'])) {
215 putenv("PGPASSWORD=". $url['pass']);
219 ' -h ' . $url['host'] .
220 ' -U' . escapeshellarg($url['user']) .
221 ' ' . basename($url['path']);
222 echo $psql_cmd . "\n" ;
229 $fd = $this->rootDir. "/Pman/$m/DataObjects";
231 foreach(glob($fd.'/*.sql') as $bfn) {
234 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
237 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
238 && !preg_match('#\.pg\.sql#i', basename($bfn))
239 ) { // skip migration scripts at present..
242 // files ending in .pg.sql are native postgres files..
243 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
245 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
247 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
259 $fd = $this->rootDir. "/Pman/$m/sql";
260 // sql directory - we try to convert..
261 foreach(glob($fd.'/*.sql') as $bfn) {
262 $fn = $this->convertToPG($bfn);
263 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
264 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
268 // postgres specific directory..
270 $fd = $this->rootDir. "/Pman/$m/pgsql";
272 foreach(glob($fd.'/*.sql') as $fn) {
273 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
274 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
284 * simple regex based convert mysql to pgsql...
286 function convertToPG($src)
288 $fn = $this->tempName('sql');
290 $ret = array( ); // pad it a bit.
291 $extra = array("", "" );
294 foreach(file($src) as $l) {
297 if (!strlen($l) || $l[0] == '#') {
301 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
303 // $extra[] = "drop table {$tbl};";
306 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
307 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
308 $extra[] = "create sequence {$tbl}_seq;";
312 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
313 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
315 // ALTER TABLE core_event_audit ADD INDEX looku
319 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
321 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
322 $l = preg_replace('# blob #i', ' TEXT ', $l);
323 $l = preg_replace('# longtext #i', ' TEXT ', $l);
324 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
335 $ret = array_merge($extra,$ret);
337 //echo implode("\n", $ret); //exit;
338 file_put_contents($fn, implode("\n", $ret));
343 function runUpdateModulesData()
347 $modules = array_reverse($this->modulesList());
349 // move 'project' one to the end...
351 foreach ($modules as $module){
352 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
353 if($module == 'Core' || !file_exists($file)){
357 $class = "Pman_{$module}_UpdateDatabase";
359 if(!method_exists($x, 'updateData')){
368 function updateDataEnums()
370 $enum = DB_DataObject::Factory('core_enum');
375 'name' => 'COMPTYPE',
376 'display_name' => 'Company Types',
377 'is_system_enum' => 1,
381 'display_name' => 'Owner',
382 'seqid' => 999, // last...
383 'is_system_enum' => 1,
390 'name' => 'HtmlEditor.font-family',
391 'display_name' => 'HTML Editor font families',
392 'is_system_enum' => 1,
395 'name' => 'Helvetica,Arial,sans-serif',
396 'display_name' => 'Helvetica',
401 'name' => 'Courier New',
402 'display_name' => 'Courier',
407 'display_name' => 'Tahoma',
411 'name' => 'Times New Roman,serif',
412 'display_name' => 'Times',
417 'display_name' => 'Verdana',
429 function updateDataGroups()
432 $groups = DB_DataObject::factory('groups');
433 $groups->initGroups();
435 $groups->initDatabase($this,array(
437 'name' => 'bcc-email', // group who are bcc'ed on all requests.
438 'type' => 0, // system
445 function updateDataCompanies()
448 // fix comptypes enums..
449 $c = DB_DataObject::Factory('Companies');
451 $c->selectAdd('distinct(comptype) as comptype');
452 $c->whereAdd("comptype != ''");
455 foreach($c->fetchAll('comptype') as $cts) {
459 $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
462 $c = DB_DataObject::Factory('core_enum');
465 //DB_DataObject::debugLevel(1);
467 $c = DB_DataObject::Factory('Companies');
471 comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype)
484 function updateData()
488 $this->updateDataEnums();
489 $this->updateDataGroups();
490 $this->updateDataCompanies();