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',
42 $ff = HTML_FlexyFramework::get();
43 if (!empty($ff->cli)) {
48 parent::getAuth(); // load company!
49 $au = $this->getAuthUser();
50 if (!$au || $au->company()->comptype != 'OWNER') {
51 $this->jerr("Not authenticated", array('authFailure' => true));
53 $this->authUser = $au;
57 function get($args, $opt)
59 if($args == 'Person'){
60 if(empty($opt['source']) || empty($opt['prefix'])){
61 die("Missing Source directory for person json files or prefix for the passwrod! Try -f [JSON file path] -p [prefix] \n");
63 if (!file_exists($opt['source'])) {
64 die("can not found person json file : {$opt['source']} \n");
67 $persons = json_decode(file_get_contents($source),true);
69 DB_DataObject::factory('person')->importFromArray(HTML_Flexyframework::get()->page, $persons, $opt['prefix']);
73 if($args == 'Account'){
74 if(empty($opt['source'])){
75 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
78 DB_DataObject::factory('accnt')->importFromJson($opt['source']);
83 $this->runUpdateModulesData();
90 * imports SQL files from all DataObjects directories....
92 * except any matching /migrate/
97 $ff = HTML_Flexyframework::get();
99 $url = parse_url($ff->DB_DataObject['database']);
101 $this->{'import' . $url['scheme']}($url);
106 * mysql - does not support conversions.
113 function importmysql($url)
116 // hide stuff for web..
118 require_once 'System.php';
119 $cat = System::which('cat');
120 $mysql = System::which('mysql');
122 $ar = $this->modulesList();
125 $mysql_cmd = $mysql .
126 ' -h ' . $url['host'] .
127 ' -u' . escapeshellarg($url['user']) .
128 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
129 ' ' . basename($url['path']);
130 echo $mysql_cmd . "\n" ;
133 // old -- DAtaObjects/*.sql
137 $fd = $this->rootDir. "/Pman/$m/DataObjects";
139 foreach(glob($fd.'/*.sql') as $fn) {
142 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
145 // .my.sql but not .pg.sql
146 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
147 && !preg_match('#\.my\.sql#i', basename($fn))
148 ) { // skip migration scripts at present..
151 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
153 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
159 // new -- sql directory..
160 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
161 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
162 $fd = $this->rootDir. "/Pman/$m/sql";
164 foreach(glob($fd.'/*.sql') as $fn) {
165 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
166 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
169 $fd = $this->rootDir. "/Pman/$m/mysql";
171 foreach(glob($fd.'/*.sql') as $fn) {
172 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
173 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
188 * postgresql import..
190 function importpgsql($url)
193 // hide stuff for web..
195 require_once 'System.php';
196 $cat = System::which('cat');
197 $psql = System::which('psql');
199 $ar = $this->modulesList();
201 if (!empty($url['pass'])) {
202 putenv("PGPASSWORD=". $url['pass']);
206 ' -h ' . $url['host'] .
207 ' -U' . escapeshellarg($url['user']) .
208 ' ' . basename($url['path']);
209 echo $psql_cmd . "\n" ;
216 $fd = $this->rootDir. "/Pman/$m/DataObjects";
218 foreach(glob($fd.'/*.sql') as $bfn) {
221 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
224 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
225 && !preg_match('#\.pg\.sql#i', basename($bfn))
226 ) { // skip migration scripts at present..
229 // files ending in .pg.sql are native postgres files..
230 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
232 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
234 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
246 $fd = $this->rootDir. "/Pman/$m/sql";
247 // sql directory - we try to convert..
248 foreach(glob($fd.'/*.sql') as $bfn) {
249 $fn = $this->convertToPG($bfn);
250 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
251 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
255 // postgres specific directory..
257 $fd = $this->rootDir. "/Pman/$m/pgsql";
259 foreach(glob($fd.'/*.sql') as $fn) {
260 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
261 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
271 * simple regex based convert mysql to pgsql...
273 function convertToPG($src)
275 $fn = $this->tempName('sql');
277 $ret = array( ); // pad it a bit.
278 $extra = array("", "" );
281 foreach(file($src) as $l) {
284 if (!strlen($l) || $l[0] == '#') {
288 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
290 // $extra[] = "drop table {$tbl};";
293 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
294 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
295 $extra[] = "create sequence {$tbl}_seq;";
299 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
300 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
302 // ALTER TABLE core_event_audit ADD INDEX looku
306 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
308 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
309 $l = preg_replace('# blob #i', ' TEXT ', $l);
310 $l = preg_replace('# longtext #i', ' TEXT ', $l);
311 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
322 $ret = array_merge($extra,$ret);
324 //echo implode("\n", $ret); //exit;
325 file_put_contents($fn, implode("\n", $ret));
330 function runUpdateModulesData()
333 $modules = $this->modulesList();
334 foreach ($modules as $module){
335 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
336 if($module == 'Core' || !file_exists($file)){
340 $class = "Pman_{$module}_UpdateDatabase";
342 if(!method_exists($x, 'updateData')){
350 function updateData()
354 $enum = DB_DataObject::Factory('core_enum');
359 'name' => 'COMPTYPE',
360 'display_name' => 'Company Types',
364 'display_name' => 'Owner',
365 'seqid' => 999, // last...