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']);
108 $companies = DB_DataObject::factory('companies');
109 $companies->setFrom(array(
110 'name' => $opt['name'],
111 'comptype' => $opt['comptype']
114 $companies->insert();
115 $companies->onInsert(array(), '');
121 $this->runUpdateModulesData();
128 * imports SQL files from all DataObjects directories....
130 * except any matching /migrate/
135 $ff = HTML_Flexyframework::get();
137 $url = parse_url($ff->DB_DataObject['database']);
139 $this->{'import' . $url['scheme']}($url);
144 * mysql - does not support conversions.
151 function importmysql($url)
154 // hide stuff for web..
156 require_once 'System.php';
157 $cat = System::which('cat');
158 $mysql = System::which('mysql');
160 $ar = $this->modulesList();
163 $mysql_cmd = $mysql .
164 ' -h ' . $url['host'] .
165 ' -u' . escapeshellarg($url['user']) .
166 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
167 ' ' . basename($url['path']);
168 echo $mysql_cmd . "\n" ;
171 // old -- DAtaObjects/*.sql
175 $fd = $this->rootDir. "/Pman/$m/DataObjects";
177 foreach(glob($fd.'/*.sql') as $fn) {
180 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
183 // .my.sql but not .pg.sql
184 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
185 && !preg_match('#\.my\.sql#i', basename($fn))
186 ) { // skip migration scripts at present..
189 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
191 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
197 // new -- sql directory..
198 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
199 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
200 $fd = $this->rootDir. "/Pman/$m/sql";
202 foreach(glob($fd.'/*.sql') as $fn) {
203 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
204 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
207 $fd = $this->rootDir. "/Pman/$m/mysql";
209 foreach(glob($fd.'/*.sql') as $fn) {
210 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
211 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
226 * postgresql import..
228 function importpgsql($url)
231 // hide stuff for web..
233 require_once 'System.php';
234 $cat = System::which('cat');
235 $psql = System::which('psql');
237 $ar = $this->modulesList();
239 if (!empty($url['pass'])) {
240 putenv("PGPASSWORD=". $url['pass']);
244 ' -h ' . $url['host'] .
245 ' -U' . escapeshellarg($url['user']) .
246 ' ' . basename($url['path']);
247 echo $psql_cmd . "\n" ;
254 $fd = $this->rootDir. "/Pman/$m/DataObjects";
256 foreach(glob($fd.'/*.sql') as $bfn) {
259 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
262 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
263 && !preg_match('#\.pg\.sql#i', basename($bfn))
264 ) { // skip migration scripts at present..
267 // files ending in .pg.sql are native postgres files..
268 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
270 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
272 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
284 $fd = $this->rootDir. "/Pman/$m/sql";
285 // sql directory - we try to convert..
286 foreach(glob($fd.'/*.sql') as $bfn) {
287 $fn = $this->convertToPG($bfn);
288 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
289 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
293 // postgres specific directory..
295 $fd = $this->rootDir. "/Pman/$m/pgsql";
297 foreach(glob($fd.'/*.sql') as $fn) {
298 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
299 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
309 * simple regex based convert mysql to pgsql...
311 function convertToPG($src)
313 $fn = $this->tempName('sql');
315 $ret = array( ); // pad it a bit.
316 $extra = array("", "" );
319 foreach(file($src) as $l) {
322 if (!strlen($l) || $l[0] == '#') {
326 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
328 // $extra[] = "drop table {$tbl};";
331 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
332 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
333 $extra[] = "create sequence {$tbl}_seq;";
337 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
338 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
340 // ALTER TABLE core_event_audit ADD INDEX looku
344 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
346 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
347 $l = preg_replace('# blob #i', ' TEXT ', $l);
348 $l = preg_replace('# longtext #i', ' TEXT ', $l);
349 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
360 $ret = array_merge($extra,$ret);
362 //echo implode("\n", $ret); //exit;
363 file_put_contents($fn, implode("\n", $ret));
368 function runUpdateModulesData()
371 $modules = $this->modulesList();
372 foreach ($modules as $module){
373 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
374 if($module == 'Core' || !file_exists($file)){
378 $class = "Pman_{$module}_UpdateDatabase";
380 if(!method_exists($x, 'updateData')){
388 function updateData()
392 $enum = DB_DataObject::Factory('core_enum');
397 'name' => 'COMPTYPE',
398 'display_name' => 'Company Types',
402 'display_name' => 'Owner',
403 'seqid' => 999, // last...
411 $groups = DB_DataObject::factory('groups');
412 $groups->initGroups();