From 3a839c5927f7b8b6301e5c5a0698223bf5cd4a4b Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Wed, 21 Sep 2016 13:39:00 -0700 Subject: [PATCH] Inject schema vars into DatabaseMysql Also fix broken patchSql.php maintenance script Change-Id: I45bccb0e2e10bd06651a551fa08ed0c66d11eb6a --- includes/installer/DatabaseUpdater.php | 20 +++++++++++++++++-- includes/installer/MysqlUpdater.php | 14 +++++++++++++ .../libs/rdbms/database/DatabaseMysqlBase.php | 15 -------------- maintenance/Maintenance.php | 2 +- maintenance/patchSql.php | 6 ++++-- maintenance/sql.php | 5 +++++ 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/includes/installer/DatabaseUpdater.php b/includes/installer/DatabaseUpdater.php index 0e4b098aeb..0d0da080fa 100644 --- a/includes/installer/DatabaseUpdater.php +++ b/includes/installer/DatabaseUpdater.php @@ -170,14 +170,14 @@ abstract class DatabaseUpdater { } /** - * @param DatabaseBase $db + * @param Database $db * @param bool $shared * @param Maintenance $maintenance * * @throws MWException * @return DatabaseUpdater */ - public static function newForDB( &$db, $shared = false, $maintenance = null ) { + public static function newForDB( Database $db, $shared = false, $maintenance = null ) { $type = $db->getType(); if ( in_array( $type, Installer::getDBTypes() ) ) { $class = ucfirst( $type ) . 'Updater'; @@ -402,6 +402,20 @@ abstract class DatabaseUpdater { } } + /** + * Get appropriate schema variables in the current database connection. + * + * This should be called after any request data has been imported, but before + * any write operations to the database. The result should be passed to the DB + * setSchemaVars() method. + * + * @return array + * @since 1.28 + */ + public function getSchemaVars() { + return []; // DB-type specific + } + /** * Do all the updates * @@ -410,6 +424,8 @@ abstract class DatabaseUpdater { public function doUpdates( $what = [ 'core', 'extensions', 'stats' ] ) { global $wgVersion; + $this->db->setSchemaVars( $this->getSchemaVars() ); + $what = array_flip( $what ); $this->skipSchema = isset( $what['noschema'] ) || $this->fileHandle !== null; if ( isset( $what['core'] ) ) { diff --git a/includes/installer/MysqlUpdater.php b/includes/installer/MysqlUpdater.php index 65af086e08..693b6ff538 100644 --- a/includes/installer/MysqlUpdater.php +++ b/includes/installer/MysqlUpdater.php @@ -1122,4 +1122,18 @@ class MysqlUpdater extends DatabaseUpdater { 'Making rev_page_id index non-unique' ); } + + public function getSchemaVars() { + global $wgDBTableOptions; + + $vars = []; + $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $wgDBTableOptions ); + $vars['wgDBTableOptions'] = str_replace( + 'CHARSET=mysql4', + 'CHARSET=binary', + $vars['wgDBTableOptions'] + ); + + return $vars; + } } diff --git a/includes/libs/rdbms/database/DatabaseMysqlBase.php b/includes/libs/rdbms/database/DatabaseMysqlBase.php index 5134a4a8b4..675bc87059 100644 --- a/includes/libs/rdbms/database/DatabaseMysqlBase.php +++ b/includes/libs/rdbms/database/DatabaseMysqlBase.php @@ -1269,21 +1269,6 @@ abstract class DatabaseMysqlBase extends DatabaseBase { return $this->query( "DROP TABLE IF EXISTS " . $this->tableName( $tableName ), $fName ); } - /** - * @return array - */ - protected function getDefaultSchemaVars() { - $vars = parent::getDefaultSchemaVars(); - $vars['wgDBTableOptions'] = str_replace( 'TYPE', 'ENGINE', $GLOBALS['wgDBTableOptions'] ); - $vars['wgDBTableOptions'] = str_replace( - 'CHARSET=mysql4', - 'CHARSET=binary', - $vars['wgDBTableOptions'] - ); - - return $vars; - } - /** * Get status information from SHOW STATUS in an associative array * diff --git a/maintenance/Maintenance.php b/maintenance/Maintenance.php index 7e0fb455bf..e1a4dc6e3e 100644 --- a/maintenance/Maintenance.php +++ b/maintenance/Maintenance.php @@ -1241,7 +1241,7 @@ abstract class Maintenance { * @param integer $db DB index (DB_REPLICA/DB_MASTER) * @param array $groups; default: empty array * @param string|bool $wiki; default: current wiki - * @return IDatabase + * @return Database */ protected function getDB( $db, $groups = [], $wiki = false ) { if ( is_null( $this->mDb ) ) { diff --git a/maintenance/patchSql.php b/maintenance/patchSql.php index 43fbd38cd1..bc21140945 100644 --- a/maintenance/patchSql.php +++ b/maintenance/patchSql.php @@ -45,11 +45,13 @@ class PatchSql extends Maintenance { public function execute() { $dbw = $this->getDB( DB_MASTER ); + $updater = DatabaseUpdater::newForDB( $dbw, true, $this ); + foreach ( $this->mArgs as $arg ) { $files = [ $arg, - $dbw->patchPath( $arg ), - $dbw->patchPath( "patch-$arg.sql" ), + $updater->patchPath( $dbw, $arg ), + $updater->patchPath( $dbw, "patch-$arg.sql" ), ]; foreach ( $files as $file ) { if ( file_exists( $file ) ) { diff --git a/maintenance/sql.php b/maintenance/sql.php index a9fe45ae13..a9a982ce50 100644 --- a/maintenance/sql.php +++ b/maintenance/sql.php @@ -80,6 +80,11 @@ class MwSql extends Maintenance { $this->error( "The server selected ({$db->getServer()}) is not a replica DB.", 1 ); } + if ( $index === DB_MASTER ) { + $updater = DatabaseUpdater::newForDB( $db, true, $this ); + $db->setSchemaVars( $updater->getSchemaVars() ); + } + if ( $this->hasArg( 0 ) ) { $file = fopen( $this->getArg( 0 ), 'r' ); if ( !$file ) { -- 2.20.1