UpdateDatabase.php
[Pman.Core] / UpdateDatabase.php
1 <?php
2
3 /**
4  *
5  * This applies database files from
6  * a) OLD - {MODULE}/DataObjects/XXXX.{dbtype}.sql
7  *
8  * b) NEW - {MODULE}/sql/XXX.sql (SHARED or translable)
9  *  and {MODULE}/{dbtype}/XXX.sql (SHARED or translable)
10  *
11  *
12  */
13
14 require_once 'Pman.php';
15 class Pman_Core_UpdateDatabase extends Pman
16 {
17     
18     static $cli_desc = "Update SQL - Beta (it will run updateData of all modules)";
19  
20     static $cli_opts = array(
21       
22         'prefix' => array(
23             'desc' => 'prefix for the password (eg. fred > xxx4fred - prefix is xxx4)',
24             'short' => 'p',
25             'default' => '',
26             'min' => 1,
27             'max' => 1,
28         ),
29         'data-only' => array(
30             'desc' => 'only run the updateData - do not run import the tables and procedures.',
31             'short' => 'p',
32             'default' => '',
33             'min' => 1,
34             'max' => 1,
35             
36         ),
37         'add-company' => array(
38             'desc' => 'add a company name of the company',
39             'short' => 'n',
40             'default' => '',
41             'min' => 1,
42             'max' => 1,
43         ),
44         'add-company-with-type' => array(
45             'desc' => 'the type of company (default OWNER)',
46             'short' => 't',
47             'default' => 'OWNER',
48             'min' => 1,
49             'max' => 1,
50         ),
51         'init' => array(
52             'desc' => 'Initialize the database (pg only supported)',
53             'short' => 'i',
54             'default' => '',
55             'min' => 1,
56             'max' => 1,
57         ),
58         'only-module-sql' => array(
59             'desc' => 'Only run sql import on this modules - eg. Core',
60             'default' => '',
61             'min' => 1,
62             'max' => 1,
63         ),
64         'procedures-only' => array(
65             'desc' => 'Only import procedures (not supported by most modules yet) - ignores sql directory',
66             'default' => '',
67             'min' => 1,
68             'max' => 1,
69         ),
70         
71         
72         'json-person' => array(
73             'desc' => 'Person JSON file',
74             'default' => '',
75             'min' => 1,
76             'max' => 1,
77             
78         ),
79     );
80     
81     static function cli_opts()
82     {
83         
84         $ret = self::$cli_opts;
85         $ff = HTML_FlexyFramework::get();
86         $a = new Pman();
87         $mods = $a->modulesList();
88         foreach($mods as $m) {
89             
90             $fd = $ff->rootDir. "/Pman/$m/UpdateDatabase.php";
91             if (!file_exists($fd)) {
92                 continue;
93             }
94             
95             require_once $fd;
96             
97             $cls = new ReflectionClass('Pman_'. $m . '_UpdateDatabase');
98             
99             $ret = array_merge($ret, $cls->getStaticPropertyValue('cli_opts'));
100             
101             
102         }
103         
104         return $ret;
105     }
106     
107     var $opts = false;
108     var $disabled = array();
109     
110     
111     var $cli = false;
112     function getAuth() {
113         
114         
115         $ff = HTML_FlexyFramework::get();
116         if (!empty($ff->cli)) {
117             $this->cli = true;
118             return true;
119         }
120         
121         parent::getAuth(); // load company!
122         $au = $this->getAuthUser();
123         if (!$au || $au->company()->comptype != 'OWNER') {
124             $this->jerr("Not authenticated", array('authFailure' => true));
125         }
126         $this->authUser = $au;
127         return true;
128     }
129      
130     function get($args, $opts)
131     {
132         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
133    
134         $ff = HTML_FlexyFramework::get();
135         
136         $this->disabled = explode(',', $ff->disable);
137         
138         //$this->fixSequencesPgsql();exit;
139         $this->opts = $opts;
140         
141         // ask all the modules to verify the opts
142         
143         $this->checkOpts($opts);
144         
145         if (empty($opts['data-only'])) {
146             $this->importSQL();
147         }
148         if (!empty($opts['only-module-sql'])) {
149             return;
150         }
151         
152         $this->runUpdateModulesData();
153         
154         
155         if (!empty($opts['add-company'])) {
156             // make sure we have a good cache...?
157            
158             DB_DataObject::factory('companies')->initCompanies($this, $opts);
159         }
160         
161         $this->runExtensions();
162          
163          
164     }
165     function output() {
166         return '';
167     }
168      /**
169      * imports SQL files from all DataObjects directories....
170      * 
171      * except any matching /migrate/
172      */
173     function importSQL()
174     {
175         
176         // loop through all the modules, and see if they have a importSQL method?
177         
178         
179         $ff = HTML_Flexyframework::get();
180         
181         $dburl = parse_url($ff->DB_DataObject['database']);
182         
183         //$this->{'import' . $url['scheme']}($url);
184         
185         $dbtype = $dburl['scheme'];
186         $dirmethod = 'import' . $dburl['scheme'] . 'dir';
187         
188         
189        
190         
191         $ar = $this->modulesList();
192         
193         
194         foreach($ar as $m) {
195              echo "Importing SQL from module $m\n";
196             if (!empty($this->opts['only-module-sql']) && $m != $this->opts['only-module-sql']) {
197                 continue;
198             }
199             
200             
201             // check to see if the class has
202             
203             
204             
205             $file = $this->rootDir. "/Pman/$m/UpdateDatabase.php";
206             if($m != 'Core' && file_exists($file)){
207                 
208                 require_once $file;
209                 $class = "Pman_{$m}_UpdateDatabase";
210                 $x = new $class;
211                 if(method_exists($x, 'importModuleSQL')){
212                     echo "Importing SQL from module $m using Module::importModuleSQL\n";
213                     $x->opts = $this->opts;
214                     $x->rootDir = $this->rootDir;
215                     $x->importModuleSQL($dburl);
216                     continue;
217                 }
218             };
219
220             echo "Importing SQL from module $m\n";
221             
222             
223             // if init has been called
224             // look in pgsql.ini
225             if (!empty($this->opts['init'])) {
226                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
227                 
228             }
229             
230             
231             
232             $fd = $this->rootDir. "/Pman/$m/DataObjects";
233             
234             $this->{$dirmethod}($dburl, $fd);
235             
236             // new -- sql directory..
237             // new style will not support migrate ... they have to go into mysql-migrate.... directories..
238             // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
239             
240             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/sql");
241             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
242             
243             
244             
245             if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
246                 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
247                 
248                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
249                 $this->{'fixSequences'. $dbtype}();
250                 
251             }
252               
253             
254         }
255         
256     }
257     
258     
259     
260      
261     /** -------------- code to handle importing a whole directory of files into the database  -------  **/
262     
263     
264     function importpgsqldir($url, $dir, $disable_triggers = false)
265     {
266         $ff = HTML_FlexyFramework::get();
267         
268         require_once 'System.php';
269         $cat = System::which('cat');
270         $psql = System::which('psql');
271         
272          
273         if (!empty($url['pass'])) { 
274             putenv("PGPASSWORD=". $url['pass']);
275         }
276            
277         $psql_cmd = $psql .
278             ' -h ' . $url['host'] .
279             ' -U' . escapeshellarg($url['user']) .
280              ' ' . basename($url['path']);
281         
282         
283         echo $psql_cmd . "\n" ;
284         echo "scan : $dir\n";
285         
286         if (is_file($dir)) {
287             $files = array($dir);
288
289         } else {
290         
291         
292             $files = glob($dir.'/*.sql');
293             uksort($files, 'strcasecmp');
294         }
295         //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
296         //usort($files, $lsort);
297         
298         
299         foreach($files as $bfn) {
300
301
302             if (preg_match('/migrate/i', basename($bfn))) { // skip migration scripts at present..
303                 continue;
304             }
305             if (preg_match('#\.[a-z]{2}\.sql#i', basename($bfn))
306                 && !preg_match('#\.pg\.sql#i', basename($bfn))
307             ) { // skip migration scripts at present..
308                 continue;
309             }
310             $fn = false;
311
312             if (!preg_match('/pgsql/', basename($dir) )) {
313                  if ( !preg_match('#\.pg\.sql$#', basename($bfn))) {
314                     $fn = $this->convertToPG($bfn);
315                 }
316             }
317
318             // files ending in .pg.sql are native postgres files.. ## depricated
319
320
321             $cmd = "$psql_cmd  < " . escapeshellarg($fn ? $fn : $bfn) . ' 2>&1' ;
322
323             echo "$bfn:   $cmd ". ($ff->cli ? "\n" : "<BR>\n");
324
325             passthru($cmd);
326
327             if ($fn) {
328                 unlink($fn);
329             }
330         }
331
332               
333              
334         
335     }
336     
337     
338     /**
339      * mysql - does not support conversions.
340      * 
341      *
342      */
343     
344     
345     function importmysqldir($dburl, $dir)
346     {
347         
348         $this->fixMysqlInnodb(); /// run once 
349         
350         echo "Import MYSQL :: $dir\n";
351         
352         
353         require_once 'System.php';
354         $cat = System::which('cat');
355         $mysql = System::which('mysql');
356         
357        
358            
359         $mysql_cmd = $mysql .
360             ' -h ' . $dburl['host'] .
361             ' -u' . escapeshellarg($dburl['user']) .
362             (!empty($dburl['pass']) ? ' -p' . escapeshellarg($dburl['pass'])  :  '') .
363             ' ' . basename($dburl['path']);
364         //echo $mysql_cmd . "\n" ;
365         
366         $files = glob($dir.'/*.sql');
367         uksort($files, 'strcasecmp');
368         
369        
370         foreach($files as $fn) {
371                 
372                  
373                 if (preg_match('/migrate/i', basename($fn))) { // skip migration scripts at present..
374                     continue;
375                 }
376                 // .my.sql but not .pg.sql
377                 if (preg_match('#\.[a-z]{2}\.sql#i', basename($fn))
378                     && !preg_match('#\.my\.sql#i', basename($fn))
379                 ) { // skip migration scripts at present..
380                     continue;
381                 }
382                 if (!strlen(trim($fn))) {
383                     continue;
384                 }
385                 
386                 $cmd = "$mysql_cmd -f < " . escapeshellarg($fn) ;
387                 
388                 echo basename($dir).'/'. basename($fn) .    '::' .  $cmd. ($this->cli ? "\n" : "<BR>\n");
389                 
390                 passthru($cmd);
391             
392                 
393         }
394        
395         
396         
397     }
398     
399     
400     /**
401      * simple regex based convert mysql to pgsql...
402      */
403     function convertToPG($src)
404     {
405         //echo "Convert $src\n";
406                
407         $fn = $this->tempName('sql');
408         
409         $ret = array( ); // pad it a bit.
410         $extra = array("", "" );
411         
412         $tbl = false;
413         foreach(file($src) as $l) {
414             $l = trim($l);
415             
416             if (!strlen($l) || $l[0] == '#') {
417                 continue;
418             }
419             $m = array();
420             if (preg_match('#create\s+table\s+([a-z0-9_]+)#i',  $l, $m)) {
421                 $tbl = $m[1];
422              }
423             if (preg_match('#create\s+table\s+\`([a-z0-9_]+)\`#i',  $l, $m)) {
424                 $tbl = 'shop_' . strtolower($m[1]);
425                 $l = preg_replace('#create\s+table\s+\`([a-z0-9_]+)\`#i', "CREATE TABLE {$tbl}", $l);
426             }
427             if (preg_match('#\`([a-z0-9_]+)\`#i',  $l, $m) && !preg_match('#alter\s+table\s+#i',  $l)) {
428                 $l = preg_replace('#\`([a-z0-9_]+)\`#i', "{$m[1]}_name", $l);
429             }
430             // autoinc
431             if ($tbl && preg_match('#auto_increment#i',  $l, $m)) {
432                 $l = preg_replace('#auto_increment#i', "default nextval('{$tbl}_seq')", $l);
433                 $extra[]  =   "create sequence {$tbl}_seq;";
434               
435             }
436             
437             if (preg_match('#alter\s+table\s+(\`[a-z0-9_]+\`)#i',  $l, $m)){
438                 $l = preg_replace('#alter\s+table\s+(\`[a-z0-9_]+\`)#i', "ALTER TABLE {$tbl}", $l);
439             }
440             
441             // enum value -- use the text instead..
442             
443             if ($tbl && preg_match('#([\w]+)\s+(enum\([\w|\W]+\))#i',  $l, $m)) {
444                 $l = preg_replace('#enum\([\w|\W]+\)#i', "TEXT", $l);
445             }
446             // ignore the alter enum
447             if ($tbl && preg_match('#alter\s+table\s+([\w|\W]+)\s+enum\([\w|\W]+\)#i',  $l, $m)) {
448                 continue;
449             }
450             
451             // UNIQUE KEY .. ignore
452             if ($tbl && preg_match('#UNIQUE KEY#i',  $l, $m)) {
453                 $last = array_pop($ret);
454                 $ret[] = trim($last, ",");
455                 continue;
456             }
457             
458             if ($tbl && preg_match('#RENAME\s+TO#i',  $l, $m)) {
459                 continue;
460             }
461             
462             if ($tbl && preg_match('#change\s+column#i',  $l, $m)) {
463                 continue;
464             }
465             
466             // INDEX lookup ..ignore
467             if ($tbl && preg_match('#INDEX lookup+([\w|\W]+)#i',  $l, $m)) {
468                $last = array_pop($ret);
469                $ret[] = trim($last, ",");
470                continue;
471                
472             }
473             
474             // CREATE INDEX ..ignore
475             if (preg_match('#alter\s+table\s+([a-z0-9_]+)\s+add\s+index\s+#i',  $l, $m)) {
476 //               $l = "CREATE INDEX  {$m[1]}_{$m[2]} ON {$m[1]} {$m[3]}";
477                 continue;
478              }
479              
480             // basic types..
481             $l = preg_replace('#int\([0-9]+\)#i', 'INT', $l);
482             
483             $l = preg_replace('# datetime#i', ' TIMESTAMP WITHOUT TIME ZONE', $l);
484             $l = preg_replace('# blob#i', ' TEXT', $l);
485             $l = preg_replace('# longtext#i', ' TEXT', $l);
486             $l = preg_replace('# tinyint#i', ' INT', $l);
487             
488             $ret[] = $l;
489             
490         }
491         
492         $ret = array_merge($extra,$ret);
493 //        echo implode("\n", $ret); exit;
494         
495         file_put_contents($fn, implode("\n", $ret));
496         
497         return $fn;
498     }
499     
500     
501     function checkOpts($opts)
502     {
503         
504         
505         foreach($opts as $o=>$v) {
506             if (!preg_match('/^json-/', $o) || empty($v)) {
507                 continue;
508             }
509             if (!file_exists($v)) {
510                 die("File does not exist : OPTION --{$o} = {$v} \n");
511             }
512         }
513         
514         $modules = array_reverse($this->modulesList());
515         
516         // move 'project' one to the end...
517         
518         foreach ($modules as $module){
519             $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
520             if($module == 'Core' || !file_exists($file)){
521                 continue;
522             }
523             require_once $file;
524             $class = "Pman_{$module}_UpdateDatabase";
525             $x = new $class;
526             if(!method_exists($x, 'checkOpts')){
527                 continue;
528             };
529             $x->checkOpts($opts);
530         }
531                 
532     }
533     static function jsonImportFromArray($opts)
534     {
535         foreach($opts as $o=>$v) {
536             if (!preg_match('/^json-/', $o) || empty($v)) {
537                 continue;
538             }
539             $type = str_replace('_', '-', substr($o,5));
540             
541             $data= json_decode(file_get_contents($v),true);
542             $pg = HTML_FlexyFramework::get()->page;
543             DB_DataObject::factory($type)->importFromArray($pg ,$data,$opts);
544             
545         }
546         
547         
548         
549     }
550     
551     
552     
553     function runUpdateModulesData()
554     {
555         
556         
557         HTML_FlexyFramework::get()->generateDataobjectsCache(true);
558         echo "Running jsonImportFromArray\n";
559         Pman_Core_UpdateDatabase::jsonImportFromArray($this->opts);
560         
561         
562         echo "Running updateData on modules\n";
563         // runs core...
564         echo "Core\n";
565         $this->updateData(); 
566         $modules = array_reverse($this->modulesList());
567         
568         // move 'project' one to the end...
569         
570         foreach ($modules as $module){
571             $file = $this->rootDir. "/Pman/$module/UpdateDatabase.php";
572             if($module == 'Core' || !file_exists($file)){
573                 continue;
574             }
575             
576             require_once $file;
577             $class = "Pman_{$module}_UpdateDatabase";
578             $x = new $class;
579             if(!method_exists($x, 'updateData')){
580                 continue;
581             };
582             echo "$module\n";
583             $x->updateData();
584         }
585                 
586     }
587     
588     
589     function updateDataEnums()
590     {
591         
592         $enum = DB_DataObject::Factory('core_enum');
593         //DB_DAtaObject::debugLevel(1);
594         $enum->initEnums(
595             array(
596                 array(
597                     'etype' => '',
598                     'name' => 'COMPTYPE',
599                     'display_name' =>  'Company Types',
600                     'is_system_enum' => 1,
601                     'cn' => array(
602                         array(
603                             'name' => 'OWNER',
604                             'display_name' => 'Owner',
605                             'seqid' => 999, // last...
606                             'is_system_enum' => 1,
607                         )
608                         
609                     )
610                 ),
611                 array(
612                     'etype' => '',
613                     'name' => 'HtmlEditor.font-family',
614                     'display_name' =>  'HTML Editor font families',
615                     'is_system_enum' => 1,
616                     'cn' => array(
617                         array(
618                             'name' => 'Helvetica,Arial,sans-serif',
619                             'display_name' => 'Helvetica',
620                             
621                         ),
622                         
623                         array(
624                             'name' => 'Courier New',
625                             'display_name' => 'Courier',
626                              
627                         ),
628                         array(
629                             'name' => 'Tahoma',
630                             'display_name' => 'Tahoma',
631                             
632                         ),
633                         array(
634                             'name' => 'Times New Roman,serif',
635                             'display_name' => 'Times',
636                            
637                         ),
638                         array(
639                             'name' => 'Verdana',
640                             'display_name' => 'Verdana',
641                             
642                         ),
643                         
644                             
645                         
646                     )
647                 ),
648             )
649         ); 
650         
651     }
652     function updateDataGroups()
653     {
654          
655         $groups = DB_DataObject::factory('groups');
656         $groups->initGroups();
657         
658         $groups->initDatabase($this,array(
659             array(
660                 'name' => 'bcc-email', // group who are bcc'ed on all requests.
661                 'type' => 0, // system
662             ),
663             array(
664                 'name' => 'system-email-from',
665                 'type' => 0, // system
666             ),
667             array(
668                 'name' => 'core-person-signup-bcc',
669                 'type' => 0, // system
670             ),
671         ));
672         
673     }
674     
675     function updateDataCompanies()
676     {
677          
678         // fix comptypes enums..
679         $c = DB_DataObject::Factory('Companies');
680         $c->selectAdd();
681         $c->selectAdd('distinct(comptype) as comptype');
682         $c->whereAdd("comptype != ''");
683         
684         $ctb = array();
685         foreach($c->fetchAll('comptype') as $cts) {
686             
687             
688             
689            $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
690         
691         }
692          $c = DB_DataObject::Factory('core_enum');
693          
694         $c->initEnums($ctb);
695         //DB_DataObject::debugLevel(1);
696         // fix comptypeid
697         $c = DB_DataObject::Factory('Companies');
698         $c->query("
699             UPDATE Companies 
700                 SET
701                     comptype_id = (SELECT id FROM core_enum where etype='comptype' and name=Companies.comptype LIMIT 1)
702                 WHERE
703                     comptype_id = 0
704                     AND
705                     LENGTH(comptype) > 0
706                   
707                   
708                   ");
709          
710         
711         
712     }
713     
714     function updateData()
715     {
716         // fill i18n data..
717         HTML_FlexyFramework::get()->generateDataobjectsCache(true);
718         $this->updateDataEnums();
719         $this->updateDataGroups();
720         $this->updateDataCompanies();
721         
722         $c = DB_DataObject::Factory('I18n');
723         $c->buildDB();
724          
725        
726         
727         
728     }
729     
730     function fixMysqlInnodb()
731     {
732         
733         static $done_check = false;
734         if ($done_check) {
735             return;
736         }
737         // innodb in single files is far more efficient that MYD or one big innodb file.
738         // first check if database is using this format.
739         $db = DB_DataObject::factory('core_enum');
740         $db->query("show variables like 'innodb_file_per_table'");
741         $db->fetch();
742         if ($db->Value == 'OFF') {
743             die("Error: set innodb_file_per_table = 1 in my.cnf\n\n");
744         }
745         
746         $done_check = true;;
747
748  
749         
750         
751         
752         
753         
754     }
755     
756     
757     /** ------------- schema fixing ... there is an issue with data imported having the wrong sequence names... --- */
758     
759     function fixSequencesMysql()
760     {
761         // not required...
762     }
763     
764     function fixSequencesPgsql()
765     {
766      
767      
768         //DB_DataObject::debugLevel(1);
769         $cs = DB_DataObject::factory('core_enum');
770         $cs->query("
771          SELECT
772                     'ALTER SEQUENCE '||
773                     CASE WHEN strpos(seq_name, '.') > 0 THEN
774                         min(seq_name)
775                     ELSE 
776                         quote_ident(min(schema_name)) ||'.'|| quote_ident(min(seq_name))
777                     END 
778                     
779                     ||' OWNED BY '|| quote_ident(min(schema_name)) || '.' ||
780                     quote_ident(min(table_name)) ||'.'|| quote_ident(min(column_name)) ||';' as cmd
781              FROM (
782                       
783                        SELECT 
784                      n.nspname AS schema_name,
785                      c.relname AS table_name,
786                      a.attname AS column_name, 
787                      regexp_replace(regexp_replace(d.adsrc, E'nextval\\\\(+[''\\\"]*', ''),E'[''\\\"]*::.*\$','') AS seq_name 
788                  FROM pg_class c 
789                  JOIN pg_attribute a ON (c.oid=a.attrelid) 
790                  JOIN pg_attrdef d ON (a.attrelid=d.adrelid AND a.attnum=d.adnum) 
791                  JOIN pg_namespace n ON (c.relnamespace=n.oid)
792                  WHERE has_schema_privilege(n.oid,'USAGE')
793                    AND n.nspname NOT LIKE 'pg!_%' escape '!'
794                    AND has_table_privilege(c.oid,'SELECT')
795                    AND (NOT a.attisdropped)
796                    AND d.adsrc ~ '^nextval'
797               
798              ) seq
799              WHERE
800                  CASE WHEN strpos(seq_name, '.') > 0 THEN
801                      substring(seq_name, 1,strpos(seq_name,'.')-1)
802                 ELSE
803                     schema_name
804                 END = schema_name
805              
806              GROUP BY seq_name HAVING count(*)=1
807              ");
808         $cmds = array();
809         while ($cs->fetch()) {
810             $cmds[] = $cs->cmd;
811         }
812         foreach($cmds as $cmd) {
813             $cs = DB_DataObject::factory('core_enum');
814             echo "$cmd\n";
815             $cs->query($cmd);
816         }
817         $cs = DB_DataObject::factory('core_enum');
818          $cs->query("
819                SELECT  'SELECT SETVAL(' ||
820                          quote_literal(quote_ident(nspname) || '.' || quote_ident(S.relname)) ||
821                         ', MAX(' || quote_ident(C.attname)|| ')::integer )  FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd 
822                 FROM pg_class AS S,
823                     pg_depend AS D,
824                     pg_class AS T,
825                     pg_attribute AS C,
826                     pg_namespace AS NS
827                 WHERE S.relkind = 'S'
828                     AND S.oid = D.objid
829                     AND D.refobjid = T.oid
830                     AND D.refobjid = C.attrelid
831                     AND D.refobjsubid = C.attnum
832                     AND NS.oid = T.relnamespace
833                 ORDER BY S.relname   
834         ");
835          $cmds = array();
836         while ($cs->fetch()) {
837             $cmds[] = $cs->cmd;
838         }
839         foreach($cmds as $cmd) {
840             $cs = DB_DataObject::factory('core_enum');
841             echo "$cmd\n";
842             $cs->query($cmd);
843         }
844        
845     }
846     
847     var $extensions = array(
848         'EngineCharset',
849         'Links',
850     );
851     
852     function runExtensions()
853     {
854         
855         $ff = HTML_Flexyframework::get();
856         
857         $dburl = parse_url($ff->DB_DataObject['database']);
858         
859         $dbtype = $dburl['scheme'];
860        
861         foreach($this->extensions as $ext) {
862        
863             $scls = ucfirst($dbtype). $ext;
864             $cls = __CLASS__ . '_'. $scls;
865             $fn = implode('/',explode('_', $cls)).'.php';
866             if (!file_exists(__DIR__.'/UpdateDatabase/'. $scls .'.php')) {
867                 return;
868             }
869             require_once $fn;
870             $c = new $cls();
871             
872         }
873         
874     }
875 }