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'){
63 $this->runUpdateModulesData();
70 * imports SQL files from all DataObjects directories....
72 * except any matching /migrate/
77 $ff = HTML_Flexyframework::get();
79 $url = parse_url($ff->DB_DataObject['database']);
81 $this->{'import' . $url['scheme']}($url);
86 * mysql - does not support conversions.
93 function importmysql($url)
96 // hide stuff for web..
98 require_once 'System.php';
99 $cat = System::which('cat');
100 $mysql = System::which('mysql');
102 $ar = $this->modulesList();
105 $mysql_cmd = $mysql .
106 ' -h ' . $url['host'] .
107 ' -u' . escapeshellarg($url['user']) .
108 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
109 ' ' . basename($url['path']);
110 echo $mysql_cmd . "\n" ;
113 // old -- DAtaObjects/*.sql
117 $fd = $this->rootDir. "/Pman/$m/DataObjects";
119 foreach(glob($fd.'/*.sql') as $fn) {
122 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
125 // .my.sql but not .pg.sql
126 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
127 && !preg_match('#\.my\.sql#i', basename($fn))
128 ) { // skip migration scripts at present..
131 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
133 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
139 // new -- sql directory..
140 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
141 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
142 $fd = $this->rootDir. "/Pman/$m/sql";
144 foreach(glob($fd.'/*.sql') as $fn) {
145 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
146 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
149 $fd = $this->rootDir. "/Pman/$m/mysql";
151 foreach(glob($fd.'/*.sql') as $fn) {
152 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
153 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
168 * postgresql import..
170 function importpgsql($url)
173 // hide stuff for web..
175 require_once 'System.php';
176 $cat = System::which('cat');
177 $psql = System::which('psql');
179 $ar = $this->modulesList();
181 if (!empty($url['pass'])) {
182 putenv("PGPASSWORD=". $url['pass']);
186 ' -h ' . $url['host'] .
187 ' -U' . escapeshellarg($url['user']) .
188 ' ' . basename($url['path']);
189 echo $psql_cmd . "\n" ;
196 $fd = $this->rootDir. "/Pman/$m/DataObjects";
198 foreach(glob($fd.'/*.sql') as $bfn) {
201 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
204 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
205 && !preg_match('#\.pg\.sql#i', basename($bfn))
206 ) { // skip migration scripts at present..
209 // files ending in .pg.sql are native postgres files..
210 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
212 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
214 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
226 $fd = $this->rootDir. "/Pman/$m/sql";
227 // sql directory - we try to convert..
228 foreach(glob($fd.'/*.sql') as $bfn) {
229 $fn = $this->convertToPG($bfn);
230 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
231 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
235 // postgres specific directory..
237 $fd = $this->rootDir. "/Pman/$m/pgsql";
239 foreach(glob($fd.'/*.sql') as $fn) {
240 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
241 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
251 * simple regex based convert mysql to pgsql...
253 function convertToPG($src)
255 $fn = $this->tempName('sql');
257 $ret = array( ); // pad it a bit.
258 $extra = array("", "" );
261 foreach(file($src) as $l) {
264 if (!strlen($l) || $l[0] == '#') {
268 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
270 // $extra[] = "drop table {$tbl};";
273 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
274 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
275 $extra[] = "create sequence {$tbl}_seq;";
279 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
280 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
282 // ALTER TABLE core_event_audit ADD INDEX looku
286 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
288 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
289 $l = preg_replace('# blob #i', ' TEXT ', $l);
290 $l = preg_replace('# longtext #i', ' TEXT ', $l);
291 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
302 $ret = array_merge($extra,$ret);
304 //echo implode("\n", $ret); //exit;
305 file_put_contents($fn, implode("\n", $ret));
310 function runUpdateModulesData()
313 $modules = $this->modulesList();
314 foreach ($modules as $module){
315 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
316 if($module == 'Core' || !file_exists($file)){
320 $class = "Pman_{$module}_UpdateDatabase";
322 if(!method_exists($x, 'updateData')){
330 function updateData()
334 $enum = DB_DataObject::Factory('core_enum');
339 'name' => 'COMPTYPE',
340 'display_name' => 'Company Types',
344 'display_name' => 'Owner',
345 'seqid' => 999, // last...