MediaWiki  REL1_19
alterSharedConstraints.php
Go to the documentation of this file.
00001 <?php
00029 require_once( dirname( __FILE__ ) . '/../Maintenance.php' );
00030 
00031 class AlterSharedConstraints extends Maintenance {
00032         public function __construct() {
00033                 parent::__construct();
00034                 $this->mDescription = "Alter foreign key to reference master tables in shared database setup.";
00035         }
00036 
00037         public function getDbType() {
00038                 return Maintenance::DB_ADMIN;
00039         }
00040 
00041         public function execute() {
00042                 global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgDBprefix;
00043                 
00044                 if ( $wgSharedDB == null ) {
00045                         $this->output( "Database sharing is not enabled\n" );
00046                         return;
00047                 }
00048                 
00049                 $dbw = wfGetDB( DB_MASTER );
00050                 foreach ( $wgSharedTables as $table ) {
00051                         $stable = $dbw->tableNameInternal($table);
00052                         if ( $wgSharedPrefix != null ) {
00053                                 $ltable = preg_replace( "/^$wgSharedPrefix(.*)/i", "$wgDBprefix\\1", $stable );
00054                         } else {
00055                                 $ltable = "{$wgDBprefix}{$stable}" ;
00056                         }
00057                         
00058                         $result = $dbw->query( "SELECT uc.constraint_name, uc.table_name, ucc.column_name, uccpk.table_name pk_table_name, uccpk.column_name pk_column_name, uc.delete_rule, uc.deferrable, uc.deferred
00059                                           FROM user_constraints uc, user_cons_columns ucc, user_cons_columns uccpk
00060                                          WHERE uc.constraint_type = 'R'
00061                                            AND ucc.constraint_name = uc.constraint_name
00062                                            AND uccpk.constraint_name = uc.r_constraint_name
00063                                            AND uccpk.table_name = '$ltable'" );
00064                         while (($row = $result->fetchRow()) !== false) {
00065                                 
00066                                         $this->output( "Altering {$row['constraint_name']} ...");
00067                                         
00068                                         try {
00069                                                 $dbw->query( "ALTER TABLE {$row['table_name']} DROP CONSTRAINT {$wgDBprefix}{$row['constraint_name']}" );
00070                                         } catch (DBQueryError $exdb) {
00071                                                 if ($exdb->errno != 2443) {
00072                                                         throw $exdb;
00073                                                 }
00074                                         }
00075                                         
00076                                         $deleteRule = $row['delete_rule'] == 'NO ACTION' ? '' : "ON DELETE {$row['delete_rule']}";
00077                                         $dbw->query( "ALTER TABLE {$row['table_name']} ADD CONSTRAINT {$wgDBprefix}{$row['constraint_name']} 
00078                                                 FOREIGN KEY ({$row['column_name']}) 
00079                                                 REFERENCES {$wgSharedDB}.$stable({$row['pk_column_name']}) 
00080                                                 {$deleteRule} {$row['deferrable']} INITIALLY {$row['deferred']}" );
00081                                         
00082                                         $this->output( "DONE\n" );
00083                         }
00084                 }
00085         }
00086 
00087 }
00088 
00089 $maintClass = "AlterSharedConstraints";
00090 require_once( RUN_MAINTENANCE_IF_MAIN );