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