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");
107 DB_DataObject::factory('companies')->initCompanies(HTML_Flexyframework::get()->page, $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.
143 function importmysql($url)
146 // hide stuff for web..
148 require_once 'System.php';
149 $cat = System::which('cat');
150 $mysql = System::which('mysql');
152 $ar = $this->modulesList();
155 $mysql_cmd = $mysql .
156 ' -h ' . $url['host'] .
157 ' -u' . escapeshellarg($url['user']) .
158 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
159 ' ' . basename($url['path']);
160 echo $mysql_cmd . "\n" ;
163 // old -- DAtaObjects/*.sql
167 $fd = $this->rootDir. "/Pman/$m/DataObjects";
169 foreach(glob($fd.'/*.sql') as $fn) {
172 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
175 // .my.sql but not .pg.sql
176 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
177 && !preg_match('#\.my\.sql#i', basename($fn))
178 ) { // skip migration scripts at present..
181 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
183 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
189 // new -- sql directory..
190 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
191 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
192 $fd = $this->rootDir. "/Pman/$m/sql";
194 foreach(glob($fd.'/*.sql') as $fn) {
195 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
196 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
199 $fd = $this->rootDir. "/Pman/$m/mysql";
201 foreach(glob($fd.'/*.sql') as $fn) {
202 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
203 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
218 * postgresql import..
220 function importpgsql($url)
223 // hide stuff for web..
225 require_once 'System.php';
226 $cat = System::which('cat');
227 $psql = System::which('psql');
229 $ar = $this->modulesList();
231 if (!empty($url['pass'])) {
232 putenv("PGPASSWORD=". $url['pass']);
236 ' -h ' . $url['host'] .
237 ' -U' . escapeshellarg($url['user']) .
238 ' ' . basename($url['path']);
239 echo $psql_cmd . "\n" ;
246 $fd = $this->rootDir. "/Pman/$m/DataObjects";
248 foreach(glob($fd.'/*.sql') as $bfn) {
251 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
254 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
255 && !preg_match('#\.pg\.sql#i', basename($bfn))
256 ) { // skip migration scripts at present..
259 // files ending in .pg.sql are native postgres files..
260 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
262 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
264 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
276 $fd = $this->rootDir. "/Pman/$m/sql";
277 // sql directory - we try to convert..
278 foreach(glob($fd.'/*.sql') as $bfn) {
279 $fn = $this->convertToPG($bfn);
280 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
281 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
285 // postgres specific directory..
287 $fd = $this->rootDir. "/Pman/$m/pgsql";
289 foreach(glob($fd.'/*.sql') as $fn) {
290 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
291 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
301 * simple regex based convert mysql to pgsql...
303 function convertToPG($src)
305 $fn = $this->tempName('sql');
307 $ret = array( ); // pad it a bit.
308 $extra = array("", "" );
311 foreach(file($src) as $l) {
314 if (!strlen($l) || $l[0] == '#') {
318 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
320 // $extra[] = "drop table {$tbl};";
323 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
324 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
325 $extra[] = "create sequence {$tbl}_seq;";
329 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
330 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
332 // ALTER TABLE core_event_audit ADD INDEX looku
336 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
338 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
339 $l = preg_replace('# blob #i', ' TEXT ', $l);
340 $l = preg_replace('# longtext #i', ' TEXT ', $l);
341 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
352 $ret = array_merge($extra,$ret);
354 //echo implode("\n", $ret); //exit;
355 file_put_contents($fn, implode("\n", $ret));
360 function runUpdateModulesData()
363 $modules = $this->modulesList();
364 foreach ($modules as $module){
365 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
366 if($module == 'Core' || !file_exists($file)){
370 $class = "Pman_{$module}_UpdateDatabase";
372 if(!method_exists($x, 'updateData')){
380 function updateData()
384 $enum = DB_DataObject::Factory('core_enum');
389 'name' => 'COMPTYPE',
390 'display_name' => 'Company Types',
394 'display_name' => 'Owner',
395 'seqid' => 999, // last...
403 $groups = DB_DataObject::factory('groups');
404 $groups->initGroups();