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