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");
64 DB_DataObject::factory('person')->importFromJson($opt['source'], $opt['prefix']);
68 if($args == 'Account'){
69 if(empty($opt['source'])){
70 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
73 DB_DataObject::factory('accnt')->importFromJson($opt['source']);
78 $this->runUpdateModulesData();
85 * imports SQL files from all DataObjects directories....
87 * except any matching /migrate/
92 $ff = HTML_Flexyframework::get();
94 $url = parse_url($ff->DB_DataObject['database']);
96 $this->{'import' . $url['scheme']}($url);
101 * mysql - does not support conversions.
108 function importmysql($url)
111 // hide stuff for web..
113 require_once 'System.php';
114 $cat = System::which('cat');
115 $mysql = System::which('mysql');
117 $ar = $this->modulesList();
120 $mysql_cmd = $mysql .
121 ' -h ' . $url['host'] .
122 ' -u' . escapeshellarg($url['user']) .
123 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
124 ' ' . basename($url['path']);
125 echo $mysql_cmd . "\n" ;
128 // old -- DAtaObjects/*.sql
132 $fd = $this->rootDir. "/Pman/$m/DataObjects";
134 foreach(glob($fd.'/*.sql') as $fn) {
137 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
140 // .my.sql but not .pg.sql
141 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
142 && !preg_match('#\.my\.sql#i', basename($fn))
143 ) { // skip migration scripts at present..
146 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
148 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
154 // new -- sql directory..
155 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
156 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
157 $fd = $this->rootDir. "/Pman/$m/sql";
159 foreach(glob($fd.'/*.sql') as $fn) {
160 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
161 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
164 $fd = $this->rootDir. "/Pman/$m/mysql";
166 foreach(glob($fd.'/*.sql') as $fn) {
167 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
168 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
183 * postgresql import..
185 function importpgsql($url)
188 // hide stuff for web..
190 require_once 'System.php';
191 $cat = System::which('cat');
192 $psql = System::which('psql');
194 $ar = $this->modulesList();
196 if (!empty($url['pass'])) {
197 putenv("PGPASSWORD=". $url['pass']);
201 ' -h ' . $url['host'] .
202 ' -U' . escapeshellarg($url['user']) .
203 ' ' . basename($url['path']);
204 echo $psql_cmd . "\n" ;
211 $fd = $this->rootDir. "/Pman/$m/DataObjects";
213 foreach(glob($fd.'/*.sql') as $bfn) {
216 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
219 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
220 && !preg_match('#\.pg\.sql#i', basename($bfn))
221 ) { // skip migration scripts at present..
224 // files ending in .pg.sql are native postgres files..
225 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
227 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
229 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
241 $fd = $this->rootDir. "/Pman/$m/sql";
242 // sql directory - we try to convert..
243 foreach(glob($fd.'/*.sql') as $bfn) {
244 $fn = $this->convertToPG($bfn);
245 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
246 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
250 // postgres specific directory..
252 $fd = $this->rootDir. "/Pman/$m/pgsql";
254 foreach(glob($fd.'/*.sql') as $fn) {
255 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
256 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
266 * simple regex based convert mysql to pgsql...
268 function convertToPG($src)
270 $fn = $this->tempName('sql');
272 $ret = array( ); // pad it a bit.
273 $extra = array("", "" );
276 foreach(file($src) as $l) {
279 if (!strlen($l) || $l[0] == '#') {
283 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
285 // $extra[] = "drop table {$tbl};";
288 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
289 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
290 $extra[] = "create sequence {$tbl}_seq;";
294 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
295 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
297 // ALTER TABLE core_event_audit ADD INDEX looku
301 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
303 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
304 $l = preg_replace('# blob #i', ' TEXT ', $l);
305 $l = preg_replace('# longtext #i', ' TEXT ', $l);
306 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
317 $ret = array_merge($extra,$ret);
319 //echo implode("\n", $ret); //exit;
320 file_put_contents($fn, implode("\n", $ret));
325 function runUpdateModulesData()
328 $modules = $this->modulesList();
329 foreach ($modules as $module){
330 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
331 if($module == 'Core' || !file_exists($file)){
335 $class = "Pman_{$module}_UpdateDatabase";
337 if(!method_exists($x, 'updateData')){
345 function updateData()
349 $enum = DB_DataObject::Factory('core_enum');
354 'name' => 'COMPTYPE',
355 'display_name' => 'Company Types',
359 'display_name' => 'Owner',
360 'seqid' => 999, // last...