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