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(HTML_Flexyframework::get()->page, $persons, $opt['prefix']);
87 if($args == 'Account'){
88 if(empty($opt['source'])){
89 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
92 if (!file_exists($opt['source'])) {
93 die("can not found account json file : {$opt['source']} \n");
96 $accounts = json_decode(file_get_contents($opt['source']),true);
98 DB_DataObject::factory('accnt')->importFromArray($accounts);
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");
111 $this->runUpdateModulesData();
118 * imports SQL files from all DataObjects directories....
120 * except any matching /migrate/
125 $ff = HTML_Flexyframework::get();
127 $url = parse_url($ff->DB_DataObject['database']);
129 $this->{'import' . $url['scheme']}($url);
134 * mysql - does not support conversions.
141 function importmysql($url)
144 // hide stuff for web..
146 require_once 'System.php';
147 $cat = System::which('cat');
148 $mysql = System::which('mysql');
150 $ar = $this->modulesList();
153 $mysql_cmd = $mysql .
154 ' -h ' . $url['host'] .
155 ' -u' . escapeshellarg($url['user']) .
156 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
157 ' ' . basename($url['path']);
158 echo $mysql_cmd . "\n" ;
161 // old -- DAtaObjects/*.sql
165 $fd = $this->rootDir. "/Pman/$m/DataObjects";
167 foreach(glob($fd.'/*.sql') as $fn) {
170 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
173 // .my.sql but not .pg.sql
174 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
175 && !preg_match('#\.my\.sql#i', basename($fn))
176 ) { // skip migration scripts at present..
179 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
181 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
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..
190 $fd = $this->rootDir. "/Pman/$m/sql";
192 foreach(glob($fd.'/*.sql') as $fn) {
193 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
194 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
197 $fd = $this->rootDir. "/Pman/$m/mysql";
199 foreach(glob($fd.'/*.sql') as $fn) {
200 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
201 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
216 * postgresql import..
218 function importpgsql($url)
221 // hide stuff for web..
223 require_once 'System.php';
224 $cat = System::which('cat');
225 $psql = System::which('psql');
227 $ar = $this->modulesList();
229 if (!empty($url['pass'])) {
230 putenv("PGPASSWORD=". $url['pass']);
234 ' -h ' . $url['host'] .
235 ' -U' . escapeshellarg($url['user']) .
236 ' ' . basename($url['path']);
237 echo $psql_cmd . "\n" ;
244 $fd = $this->rootDir. "/Pman/$m/DataObjects";
246 foreach(glob($fd.'/*.sql') as $bfn) {
249 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
252 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
253 && !preg_match('#\.pg\.sql#i', basename($bfn))
254 ) { // skip migration scripts at present..
257 // files ending in .pg.sql are native postgres files..
258 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
260 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
262 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
274 $fd = $this->rootDir. "/Pman/$m/sql";
275 // sql directory - we try to convert..
276 foreach(glob($fd.'/*.sql') as $bfn) {
277 $fn = $this->convertToPG($bfn);
278 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
279 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
283 // postgres specific directory..
285 $fd = $this->rootDir. "/Pman/$m/pgsql";
287 foreach(glob($fd.'/*.sql') as $fn) {
288 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
289 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
299 * simple regex based convert mysql to pgsql...
301 function convertToPG($src)
303 $fn = $this->tempName('sql');
305 $ret = array( ); // pad it a bit.
306 $extra = array("", "" );
309 foreach(file($src) as $l) {
312 if (!strlen($l) || $l[0] == '#') {
316 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
318 // $extra[] = "drop table {$tbl};";
321 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
322 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
323 $extra[] = "create sequence {$tbl}_seq;";
327 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
328 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
330 // ALTER TABLE core_event_audit ADD INDEX looku
334 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
336 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
337 $l = preg_replace('# blob #i', ' TEXT ', $l);
338 $l = preg_replace('# longtext #i', ' TEXT ', $l);
339 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
350 $ret = array_merge($extra,$ret);
352 //echo implode("\n", $ret); //exit;
353 file_put_contents($fn, implode("\n", $ret));
358 function runUpdateModulesData()
361 $modules = $this->modulesList();
362 foreach ($modules as $module){
363 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
364 if($module == 'Core' || !file_exists($file)){
368 $class = "Pman_{$module}_UpdateDatabase";
370 if(!method_exists($x, 'updateData')){
378 function updateData()
382 $enum = DB_DataObject::Factory('core_enum');
387 'name' => 'COMPTYPE',
388 'display_name' => 'Company Types',
392 'display_name' => 'Owner',
393 'seqid' => 999, // last...
401 $groups = DB_DataObject::factory('groups');
402 $groups->initGroups();