r92240 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92239‎ | r92240 | r92241 >
Date:12:44, 15 July 2011
Author:freakolowsky
Status:ok
Tags:
Comment:
* maintenance script to switch FK references if using wgSharedDB
Modified paths:
  • /trunk/phase3/maintenance/oracle/alterSharedConstraints.php (added) (history)

Diff [purge]

Index: trunk/phase3/maintenance/oracle/alterSharedConstraints.php
@@ -0,0 +1,90 @@
 2+<?php
 3+/**
 4+ * This program is free software; you can redistribute it and/or modify
 5+ * it under the terms of the GNU General Public License as published by
 6+ * the Free Software Foundation; either version 2 of the License, or
 7+ * (at your option) any later version.
 8+ *
 9+ * This program is distributed in the hope that it will be useful,
 10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 12+ * GNU General Public License for more details.
 13+ *
 14+ * You should have received a copy of the GNU General Public License along
 15+ * with this program; if not, write to the Free Software Foundation, Inc.,
 16+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 17+ * http://www.gnu.org/copyleft/gpl.html
 18+ *
 19+ * @ingroup Maintenance
 20+ */
 21+
 22+/**
 23+ * When using shared tables that are referenced by foreign keys on local
 24+ * tables you have to change the constraints on local tables.
 25+ *
 26+ * The shared tables have to have GRANT REFERENCE on shared tables to local schema
 27+ * i.e.: GRANT REFERENCES (user_id) ON mwuser TO hubclient;
 28+ */
 29+
 30+require_once( dirname( __FILE__ ) . '/../Maintenance.php' );
 31+
 32+class AlterSharedConstraints extends Maintenance {
 33+ public function __construct() {
 34+ parent::__construct();
 35+ $this->mDescription = "Alter foreign key to reference master tables in shared database setup.";
 36+ }
 37+
 38+ public function getDbType() {
 39+ return Maintenance::DB_ADMIN;
 40+ }
 41+
 42+ public function execute() {
 43+ global $wgSharedDB, $wgSharedTables, $wgSharedPrefix, $wgDBprefix;
 44+
 45+ if ( $wgSharedDB == null ) {
 46+ $this->output( "Database sharing is not enabled\n" );
 47+ return;
 48+ }
 49+
 50+ $dbw = wfGetDB( DB_MASTER );
 51+ foreach ( $wgSharedTables as $table ) {
 52+ $stable = $dbw->tableNameInternal($table);
 53+ if ( $wgSharedPrefix != null ) {
 54+ $ltable = preg_replace( "/^$wgSharedPrefix(.*)/i", "$wgDBprefix\\1", $stable );
 55+ } else {
 56+ $ltable = "{$wgDBprefix}{$stable}" ;
 57+ }
 58+
 59+ $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
 60+ FROM user_constraints uc, user_cons_columns ucc, user_cons_columns uccpk
 61+ WHERE uc.constraint_type = 'R'
 62+ AND ucc.constraint_name = uc.constraint_name
 63+ AND uccpk.constraint_name = uc.r_constraint_name
 64+ AND uccpk.table_name = '$ltable'" );
 65+ while (($row = $result->fetchRow()) !== false) {
 66+
 67+ $this->output( "Altering {$row['constraint_name']} ...");
 68+
 69+ try {
 70+ $dbw->query( "ALTER TABLE {$row['table_name']} DROP CONSTRAINT {$wgDBprefix}{$row['constraint_name']}" );
 71+ } catch (DBQueryError $exdb) {
 72+ if ($exdb->errno != 2443) {
 73+ throw $exdb;
 74+ }
 75+ }
 76+
 77+ $deleteRule = $row['delete_rule'] == 'NO ACTION' ? '' : "ON DELETE {$row['delete_rule']}";
 78+ $dbw->query( "ALTER TABLE {$row['table_name']} ADD CONSTRAINT {$wgDBprefix}{$row['constraint_name']}
 79+ FOREIGN KEY ({$row['column_name']})
 80+ REFERENCES {$wgSharedDB}.$stable({$row['pk_column_name']})
 81+ {$deleteRule} {$row['deferrable']} INITIALLY {$row['deferred']}" );
 82+
 83+ $this->output( "DONE\n" );
 84+ }
 85+ }
 86+ }
 87+
 88+}
 89+
 90+$maintClass = "AlterSharedConstraints";
 91+require_once( RUN_MAINTENANCE_IF_MAIN );
Property changes on: trunk/phase3/maintenance/oracle/alterSharedConstraints.php
___________________________________________________________________
Added: svn:eol-style
192 + native

Status & tagging log