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.
128 function importmysql($url)
131 // hide stuff for web..
133 require_once 'System.php';
134 $cat = System::which('cat');
135 $mysql = System::which('mysql');
137 $ar = $this->modulesList();
140 $mysql_cmd = $mysql .
141 ' -h ' . $url['host'] .
142 ' -u' . escapeshellarg($url['user']) .
143 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
144 ' ' . basename($url['path']);
145 echo $mysql_cmd . "\n" ;
148 // old -- DAtaObjects/*.sql
152 $fd = $this->rootDir. "/Pman/$m/DataObjects";
154 foreach(glob($fd.'/*.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 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
168 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
174 // new -- sql directory..
175 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
176 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
177 $fd = $this->rootDir. "/Pman/$m/sql";
179 foreach(glob($fd.'/*.sql') as $fn) {
180 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
181 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
184 $fd = $this->rootDir. "/Pman/$m/mysql";
186 foreach(glob($fd.'/*.sql') as $fn) {
187 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
188 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
203 * postgresql import..
205 function importpgsql($url)
208 // hide stuff for web..
210 require_once 'System.php';
211 $cat = System::which('cat');
212 $psql = System::which('psql');
214 $ar = $this->modulesList();
216 if (!empty($url['pass'])) {
217 putenv("PGPASSWORD=". $url['pass']);
221 ' -h ' . $url['host'] .
222 ' -U' . escapeshellarg($url['user']) .
223 ' ' . basename($url['path']);
224 echo $psql_cmd . "\n" ;
231 $fd = $this->rootDir. "/Pman/$m/DataObjects";
233 foreach(glob($fd.'/*.sql') as $bfn) {
236 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
239 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
240 && !preg_match('#\.pg\.sql#i', basename($bfn))
241 ) { // skip migration scripts at present..
244 // files ending in .pg.sql are native postgres files..
245 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
247 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
249 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
261 $fd = $this->rootDir. "/Pman/$m/sql";
262 // sql directory - we try to convert..
263 foreach(glob($fd.'/*.sql') as $bfn) {
264 $fn = $this->convertToPG($bfn);
265 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
266 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
270 // postgres specific directory..
272 $fd = $this->rootDir. "/Pman/$m/pgsql";
274 foreach(glob($fd.'/*.sql') as $fn) {
275 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
276 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
286 * simple regex based convert mysql to pgsql...
288 function convertToPG($src)
290 $fn = $this->tempName('sql');
292 $ret = array( ); // pad it a bit.
293 $extra = array("", "" );
296 foreach(file($src) as $l) {
299 if (!strlen($l) || $l[0] == '#') {
303 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
305 // $extra[] = "drop table {$tbl};";
308 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
309 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
310 $extra[] = "create sequence {$tbl}_seq;";
314 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
315 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
317 // ALTER TABLE core_event_audit ADD INDEX looku
321 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
323 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
324 $l = preg_replace('# blob #i', ' TEXT ', $l);
325 $l = preg_replace('# longtext #i', ' TEXT ', $l);
326 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
337 $ret = array_merge($extra,$ret);
339 //echo implode("\n", $ret); //exit;
340 file_put_contents($fn, implode("\n", $ret));
345 function runUpdateModulesData()
349 $modules = array_reverse($this->modulesList());
351 // move 'project' one to the end...
353 foreach ($modules as $module){
354 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
355 if($module == 'Core' || !file_exists($file)){
359 $class = "Pman_{$module}_UpdateDatabase";
361 if(!method_exists($x, 'updateData')){
369 function updateData()
373 $enum = DB_DataObject::Factory('core_enum');
378 'name' => 'COMPTYPE',
379 'display_name' => 'Company Types',
383 'display_name' => 'Owner',
384 'seqid' => 999, // last...
392 $groups = DB_DataObject::factory('groups');
393 $groups->initGroups();