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 json files or prefix for the passwrod! Try -f [JSON file path] -p [prefix]");
65 $this->runUpdateModulesData();
72 * imports SQL files from all DataObjects directories....
74 * except any matching /migrate/
79 $ff = HTML_Flexyframework::get();
81 $url = parse_url($ff->DB_DataObject['database']);
83 $this->{'import' . $url['scheme']}($url);
88 * mysql - does not support conversions.
95 function importmysql($url)
98 // hide stuff for web..
100 require_once 'System.php';
101 $cat = System::which('cat');
102 $mysql = System::which('mysql');
104 $ar = $this->modulesList();
107 $mysql_cmd = $mysql .
108 ' -h ' . $url['host'] .
109 ' -u' . escapeshellarg($url['user']) .
110 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
111 ' ' . basename($url['path']);
112 echo $mysql_cmd . "\n" ;
115 // old -- DAtaObjects/*.sql
119 $fd = $this->rootDir. "/Pman/$m/DataObjects";
121 foreach(glob($fd.'/*.sql') as $fn) {
124 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
127 // .my.sql but not .pg.sql
128 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
129 && !preg_match('#\.my\.sql#i', basename($fn))
130 ) { // skip migration scripts at present..
133 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
135 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
141 // new -- sql directory..
142 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
143 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
144 $fd = $this->rootDir. "/Pman/$m/sql";
146 foreach(glob($fd.'/*.sql') as $fn) {
147 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
148 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
151 $fd = $this->rootDir. "/Pman/$m/mysql";
153 foreach(glob($fd.'/*.sql') as $fn) {
154 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
155 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
170 * postgresql import..
172 function importpgsql($url)
175 // hide stuff for web..
177 require_once 'System.php';
178 $cat = System::which('cat');
179 $psql = System::which('psql');
181 $ar = $this->modulesList();
183 if (!empty($url['pass'])) {
184 putenv("PGPASSWORD=". $url['pass']);
188 ' -h ' . $url['host'] .
189 ' -U' . escapeshellarg($url['user']) .
190 ' ' . basename($url['path']);
191 echo $psql_cmd . "\n" ;
198 $fd = $this->rootDir. "/Pman/$m/DataObjects";
200 foreach(glob($fd.'/*.sql') as $bfn) {
203 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
206 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
207 && !preg_match('#\.pg\.sql#i', basename($bfn))
208 ) { // skip migration scripts at present..
211 // files ending in .pg.sql are native postgres files..
212 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
214 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
216 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
228 $fd = $this->rootDir. "/Pman/$m/sql";
229 // sql directory - we try to convert..
230 foreach(glob($fd.'/*.sql') as $bfn) {
231 $fn = $this->convertToPG($bfn);
232 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
233 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
237 // postgres specific directory..
239 $fd = $this->rootDir. "/Pman/$m/pgsql";
241 foreach(glob($fd.'/*.sql') as $fn) {
242 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
243 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
253 * simple regex based convert mysql to pgsql...
255 function convertToPG($src)
257 $fn = $this->tempName('sql');
259 $ret = array( ); // pad it a bit.
260 $extra = array("", "" );
263 foreach(file($src) as $l) {
266 if (!strlen($l) || $l[0] == '#') {
270 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
272 // $extra[] = "drop table {$tbl};";
275 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
276 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
277 $extra[] = "create sequence {$tbl}_seq;";
281 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
282 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
284 // ALTER TABLE core_event_audit ADD INDEX looku
288 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
290 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
291 $l = preg_replace('# blob #i', ' TEXT ', $l);
292 $l = preg_replace('# longtext #i', ' TEXT ', $l);
293 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
304 $ret = array_merge($extra,$ret);
306 //echo implode("\n", $ret); //exit;
307 file_put_contents($fn, implode("\n", $ret));
312 function runUpdateModulesData()
315 $modules = $this->modulesList();
316 foreach ($modules as $module){
317 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
318 if($module == 'Core' || !file_exists($file)){
322 $class = "Pman_{$module}_UpdateDatabase";
324 if(!method_exists($x, 'updateData')){
332 function updateData()
336 $enum = DB_DataObject::Factory('core_enum');
341 'name' => 'COMPTYPE',
342 'display_name' => 'Company Types',
346 'display_name' => 'Owner',
347 'seqid' => 999, // last...