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