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] \n");
64 DB_DataObject::factory('person')->createPerson();
68 $this->runUpdateModulesData();
75 * imports SQL files from all DataObjects directories....
77 * except any matching /migrate/
82 $ff = HTML_Flexyframework::get();
84 $url = parse_url($ff->DB_DataObject['database']);
86 $this->{'import' . $url['scheme']}($url);
91 * mysql - does not support conversions.
98 function importmysql($url)
101 // hide stuff for web..
103 require_once 'System.php';
104 $cat = System::which('cat');
105 $mysql = System::which('mysql');
107 $ar = $this->modulesList();
110 $mysql_cmd = $mysql .
111 ' -h ' . $url['host'] .
112 ' -u' . escapeshellarg($url['user']) .
113 (!empty($url['pass']) ? ' -p' . escapeshellarg($url['pass']) : '') .
114 ' ' . basename($url['path']);
115 echo $mysql_cmd . "\n" ;
118 // old -- DAtaObjects/*.sql
122 $fd = $this->rootDir. "/Pman/$m/DataObjects";
124 foreach(glob($fd.'/*.sql') as $fn) {
127 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
130 // .my.sql but not .pg.sql
131 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
132 && !preg_match('#\.my\.sql#i', basename($fn))
133 ) { // skip migration scripts at present..
136 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
138 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
144 // new -- sql directory..
145 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
146 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
147 $fd = $this->rootDir. "/Pman/$m/sql";
149 foreach(glob($fd.'/*.sql') as $fn) {
150 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
151 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
154 $fd = $this->rootDir. "/Pman/$m/mysql";
156 foreach(glob($fd.'/*.sql') as $fn) {
157 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
158 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
173 * postgresql import..
175 function importpgsql($url)
178 // hide stuff for web..
180 require_once 'System.php';
181 $cat = System::which('cat');
182 $psql = System::which('psql');
184 $ar = $this->modulesList();
186 if (!empty($url['pass'])) {
187 putenv("PGPASSWORD=". $url['pass']);
191 ' -h ' . $url['host'] .
192 ' -U' . escapeshellarg($url['user']) .
193 ' ' . basename($url['path']);
194 echo $psql_cmd . "\n" ;
201 $fd = $this->rootDir. "/Pman/$m/DataObjects";
203 foreach(glob($fd.'/*.sql') as $bfn) {
206 if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
209 if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
210 && !preg_match('#\.pg\.sql#i', basename($bfn))
211 ) { // skip migration scripts at present..
214 // files ending in .pg.sql are native postgres files..
215 $fn = preg_match('#\.pg\.sql$#', basename($bfn)) ? false : $this->convertToPG($bfn);
217 $cmd = "$psql_cmd < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
219 echo "$bfn: $cmd ". ($this->cli ? "\n" : "<BR>\n");
231 $fd = $this->rootDir. "/Pman/$m/sql";
232 // sql directory - we try to convert..
233 foreach(glob($fd.'/*.sql') as $bfn) {
234 $fn = $this->convertToPG($bfn);
235 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
236 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
240 // postgres specific directory..
242 $fd = $this->rootDir. "/Pman/$m/pgsql";
244 foreach(glob($fd.'/*.sql') as $fn) {
245 $cmd = "$psql_cmd < " . escapeshellarg($fn) ;
246 echo $cmd. ($this->cli ? "\n" : "<BR>\n");
256 * simple regex based convert mysql to pgsql...
258 function convertToPG($src)
260 $fn = $this->tempName('sql');
262 $ret = array( ); // pad it a bit.
263 $extra = array("", "" );
266 foreach(file($src) as $l) {
269 if (!strlen($l) || $l[0] == '#') {
273 if (preg_match('#create\s+table\s+([a-z0-9_]+)#i', $l, $m)) {
275 // $extra[] = "drop table {$tbl};";
278 if ($tbl && preg_match('#auto_increment#i', $l, $m)) {
279 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
280 $extra[] = "create sequence {$tbl}_seq;";
284 if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+([^(]+)(.*)$#i', $l, $m)) {
285 $l = "CREATE INDEX {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
287 // ALTER TABLE core_event_audit ADD INDEX looku
291 $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
293 $l = preg_replace('# datetime #i', ' TIMESTAMP WITHOUT TIME ZONE ', $l);
294 $l = preg_replace('# blob #i', ' TEXT ', $l);
295 $l = preg_replace('# longtext #i', ' TEXT ', $l);
296 //$l = preg_match('#int\([0-9]+\)#i', 'INT', $l);
307 $ret = array_merge($extra,$ret);
309 //echo implode("\n", $ret); //exit;
310 file_put_contents($fn, implode("\n", $ret));
315 function runUpdateModulesData()
318 $modules = $this->modulesList();
319 foreach ($modules as $module){
320 $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
321 if($module == 'Core' || !file_exists($file)){
325 $class = "Pman_{$module}_UpdateDatabase";
327 if(!method_exists($x, 'updateData')){
335 function updateData()
339 $enum = DB_DataObject::Factory('core_enum');
344 'name' => 'COMPTYPE',
345 'display_name' => 'Company Types',
349 'display_name' => 'Owner',
350 'seqid' => 999, // last...