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)";
26 $ff = HTML_FlexyFramework::get();
27 if (!empty($ff->cli)) {
32 parent::getAuth(); // load company!
33 $au = $this->getAuthUser();
34 if (!$au || $au->company()->comptype != 'OWNER') {
35 $this->jerr("Not authenticated", array('authFailure' => true));
37 $this->authUser = $au;
44 $this->runUpdateModulesData();
51 * imports SQL files from all DataObjects directories....
53 * except any matching /migrate/
58 $ff = HTML_Flexyframework::get();
60 $url = parse_url($ff->DB_DataObject['database']);
62 $this->{'import' . $url['scheme']}($url);
67 * mysql - does not support conversions.
74 function importmysql($url)
77 // hide stuff for web..
79 require_once 'System.php';
80 $cat = System::which('cat');
81 $mysql = System::which('mysql');
83 $ar = $this->modulesList();
87 ' -h ' . $url['host'] .
88 ' -u' . escapeshellarg($url['user']) .
89 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
90 ' ' . basename($url['path']);
91 echo $mysql_cmd . "\n" ;
94 // old -- DAtaObjects/*.sql
98 $fd = $this->rootDir. "/Pman/$m/DataObjects";
100 foreach(glob($fd.'/*.sql') as $fn) {
103 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
106 // .my.sql but not .pg.sql
107 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
108 && !preg_match('#\.my\.sql#i', basename($fn))
109 ) { // skip migration scripts at present..
112 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
114 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
120 // new -- sql directory..
121 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
122 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
123 $fd = $this->rootDir. "/Pman/$m/sql";
125 foreach(glob($fd.'/*.sql') as $fn) {
126 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
127 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
130 $fd = $this->rootDir. "/Pman/$m/mysql";
132 foreach(glob($fd.'/*.sql') as $fn) {
133 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
134 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
149 * postgresql import..
151 function importpgsql($url)
154 // hide stuff for web..
156 require_once 'System.php';
157 $cat = System::which('cat');
158 $psql = System::which('psql');
160 $ar = $this->modulesList();
162 if (!empty($url['pass'])) {
163 putenv("PGPASSWORD=". $url['pass']);
167 ' -h ' . $url['host'] .
168 ' -U' . escapeshellarg($url['user']) .
169 ' ' . basename($url['path']);
170 echo $psql_cmd . "\n" ;
177 $fd = $this->rootDir. "/Pman/$m/DataObjects";
179 foreach(glob($fd.'/*.sql') as $bfn) {
182 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
185 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
186 && !preg_match('#\.pg\.sql#i', basename($bfn))
187 ) { // skip migration scripts at present..
190 // files ending in .pg.sql are native postgres files..
191 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
193 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
195 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
207 $fd = $this->rootDir. "/Pman/$m/sql";
208 // sql directory - we try to convert..
209 foreach(glob($fd.'/*.sql') as $bfn) {
210 $fn = $this->convertToPG($bfn);
211 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
212 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
216 // postgres specific directory..
218 $fd = $this->rootDir. "/Pman/$m/pgsql";
220 foreach(glob($fd.'/*.sql') as $fn) {
221 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
222 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
232 * simple regex based convert mysql to pgsql...
234 function convertToPG($src)
236 $fn = $this->tempName('sql');
238 $ret = array( ); // pad it a bit.
239 $extra = array("", "" );
242 foreach(file($src) as $l) {
245 if (!strlen($l) || $l[0] == '#') {
249 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
251 // $extra[] = "drop table {$tbl};";
254 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
255 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
256 $extra[] = "create sequence {$tbl}_seq;";
260 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
261 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
263 // ALTER TABLE core_event_audit ADD INDEX looku
267 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
269 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
270 $l = preg_replace('# blob #i', ' TEXT ', $l);
271 $l = preg_replace('# longtext #i', ' TEXT ', $l);
272 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
283 $ret = array_merge($extra,$ret);
285 //echo implode("\n", $ret); //exit;
286 file_put_contents($fn, implode("\n", $ret));
291 function runUpdateModulesData()
294 $modules = $this->modulesList();
295 foreach ($modules as $module){
296 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
297 if($module == 'Core' || !file_exists($file)){
301 $class = "Pman_{$module}_UpdateDatabase";
303 if(!method_exists($x, 'updateData')){
311 function updateData()
315 $enum = DB_DataObject::Factory('core_enum');
320 'name' => 'COMPTYPE',
321 'display_name' => 'Company Types',
325 'display_name' => 'Owner',
326 'seqid' => 999, // last...