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