r111014 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r111013‎ | r111014 | r111015 >
Date:01:25, 9 February 2012
Author:asher
Status:ok (Comments)
Tags:
Comment:
1.19 schema migration script for wmf,
exempts revision.rev_sha1 column from enwiki for now
Modified paths:
  • /branches/REL1_19/phase3/maintenance/upgrade-1.19wmf1-1.php (added) (history)

Diff [purge]

Index: branches/REL1_19/phase3/maintenance/upgrade-1.19wmf1-1.php
@@ -0,0 +1,151 @@
 2+<?php
 3+
 4+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 5+
 6+
 7+class SchemaMigration extends Maintenance {
 8+
 9+ public function __construct() {
 10+ parent::__construct();
 11+ $this->mDescription = "Run Schema Migrations for branch against all wikis";
 12+ $this->addOption( 'secondary', 'Run on secondary / non-prod slaves', false, false );
 13+ }
 14+
 15+ function doAllSchemaChanges() {
 16+ global $wgLBFactoryConf, $wgConf;
 17+
 18+ if ( $this->getOption( 'secondary' ) ) {
 19+ require( dirname( __FILE__ ) . '/../../wmf-config/db-secondary.php' );
 20+ }
 21+
 22+ $sectionLoads = $wgLBFactoryConf['sectionLoads'];
 23+ $sectionsByDB = $wgLBFactoryConf['sectionsByDB'];
 24+
 25+ $rootPass = trim( wfShellExec( '/home/wikipedia/bin/mysql_root_pass' ) );
 26+
 27+ // Compile wiki lists
 28+ $wikisBySection = array();
 29+ foreach ( $wgConf->getLocalDatabases() as $wiki ) {
 30+ if ( isset( $sectionsByDB[$wiki] ) ) {
 31+ $wikisBySection[$sectionsByDB[$wiki]][] = $wiki;
 32+ } else {
 33+ $wikisBySection['DEFAULT'][] = $wiki;
 34+ }
 35+ }
 36+
 37+ // Do the upgrades
 38+ foreach ( $sectionLoads as $section => $loads ) {
 39+ $master = true;
 40+ foreach ( $loads as $server => $load ) {
 41+ if ( $master ) {
 42+ echo "Skipping $section master $server\n";
 43+ $master = false;
 44+ continue;
 45+ }
 46+
 47+ $db = new DatabaseMysql(
 48+ $server,
 49+ 'root',
 50+ $rootPass,
 51+ false, /* dbName */
 52+ 0, /* flags, no transactions */
 53+ '' /* prefix */
 54+ );
 55+
 56+ foreach ( $wikisBySection[$section] as $wiki ) {
 57+ $db->selectDB( $wiki );
 58+ $this->upgradeWiki( $db );
 59+ if ( !$this->getOption( 'secondary' ) ) {
 60+ while ( $db->getLag() > 10 ) {
 61+ echo "Waiting for $server to catch up to master.\n";
 62+ sleep( 20 );
 63+ }
 64+ }
 65+ }
 66+ }
 67+ }
 68+
 69+ echo "All done (except masters).\n";
 70+ }
 71+
 72+ function upgradeWiki( $db ) {
 73+ $wiki = $db->getDBname();
 74+ $server = $db->getServer();
 75+ $missing = "";
 76+
 77+ $upgradeLogRow = $db->selectRow( 'updatelog',
 78+ 'ul_key',
 79+ array( 'ul_key' => '1.19wmf1-1' ),
 80+ __FUNCTION__ );
 81+ if ( $upgradeLogRow ) {
 82+ echo $db->getDBname() . ": already done\n";
 83+ return;
 84+ }
 85+
 86+ echo "$server $wiki 1.19wmf1-1";
 87+
 88+ //$this->sourceUpgradeFile( $db, dirname( __FILE__ ) .'/schema-changes-1.19wmf1-1.sql' );
 89+
 90+ if ( ! $db->indexExists( 'page', 'page_redirect_namespace_len' ) ) {
 91+ echo " page_redirect_namespace_len index";
 92+ $this->sourceUpgradeFile( $db, dirname( __FILE__ ) . '/archives/patch-page_redirect_namespace_len.sql' );
 93+ if ( ! $db->indexExists( 'page', 'page_redirect_namespace_len' ) ) {
 94+ $missing .= " page_redirect_namespace_len";
 95+ }
 96+ }
 97+
 98+ if ( ! $db->fieldExists( 'archive', 'ar_sha1' ) ) {
 99+ echo " ar_sha1";
 100+ $this->sourceUpgradeFile( $db, dirname( __FILE__ ) . '/archives/patch-ar_sha1.sql' );
 101+ if ( ! $db->fieldExists( 'archive', 'ar_sha1' ) ) {
 102+ $missing .= " ar_sha1";
 103+ }
 104+ }
 105+
 106+ if ( $db->fieldExists( 'article_feedback', 'aa_page_id' ) &&
 107+ ! $db->indexExists( 'article_feedback', 'aa_page_user_token' ) ) {
 108+ echo " aa_page_user_token index";
 109+ $this->sourceUpgradeFile( $db, dirname( __FILE__ ) . '/archives/patch-af_page_user_token-index.sql' );
 110+ if (! $db->indexExists( 'article_feedback', 'aa_page_user_token' ) ) {
 111+ $missing .= " aa_page_user_token";
 112+ }
 113+ }
 114+
 115+ if ( $wiki == "enwiki" ) {
 116+ echo " skipping rev_sha1";
 117+ } else {
 118+ if ( ! $db->fieldExists( 'revision', 'rev_sha1' ) ) {
 119+ echo " rev_sha1";
 120+ $this->sourceUpgradeFile( $db, dirname( __FILE__ ) . '/archives/patch-rev_sha1.sql' );
 121+ if ( ! $db->fieldExists( 'revision', 'rev_sha1' ) ) {
 122+ $missing .= " rev_sha1";
 123+ }
 124+ }
 125+ }
 126+
 127+ if ( $missing ) {
 128+ echo " WARN: missing $missing\n";
 129+ } else {
 130+ $db->insert( 'updatelog',
 131+ array( 'ul_key' => '1.19wmf1-1' ),
 132+ __FUNCTION__ );
 133+ echo " ok\n";
 134+ }
 135+ }
 136+
 137+ function sourceUpgradeFile( $db, $file ) {
 138+ if ( !file_exists( $file ) ) {
 139+ echo "File missing: $file\n";
 140+ exit( 1 );
 141+ }
 142+ $db->sourceFile( $file );
 143+ }
 144+
 145+ function execute() {
 146+ $this->doAllSchemaChanges();
 147+ }
 148+}
 149+
 150+$maintClass = "SchemaMigration";
 151+require_once( RUN_MAINTENANCE_IF_MAIN );
 152+

Comments

#Comment by Catrope (talk | contribs)   01:40, 9 February 2012

Per IRC, the root pass should be pulled from /usr/local/bin not from /home/wikipedia/bin , because the only the latter is managed by puppet (and it's what's actually in $PATH), and the commented-out line of code should be removed.

Looks good otherwise.

Status & tagging log