From: Roan Kattouw Date: Sat, 12 Dec 2009 15:38:03 +0000 (+0000) Subject: Add maintenance script for running queries in batches. This is especially useful... X-Git-Tag: 1.31.0-rc.0~38558 X-Git-Url: https://git.cyclocoop.org/%7B%24admin_url%7Dmembres/fiche.php?a=commitdiff_plain;h=7c47f02b7c043bdd7d14e2dc8fd7939d4bce8ad3;p=lhc%2Fweb%2Fwiklou.git Add maintenance script for running queries in batches. This is especially useful for executing large write queries (e.g. fixing user renames, clearing huge watchlists) on the WMF cluster; before, everyone wrote their own script for this when they needed it. --- diff --git a/maintenance/runBatchedQuery.php b/maintenance/runBatchedQuery.php new file mode 100644 index 0000000000..0a113cc265 --- /dev/null +++ b/maintenance/runBatchedQuery.php @@ -0,0 +1,60 @@ +mDescription = "Run a query repeatedly until it affects 0 rows, and wait for slaves in between.\n" . + "NOTE: You need to set a LIMIT clause yourself."; + $this->addOption( 'wait', "Wait for replication lag to go down to this value. Default: 5", false, true ); + } + + public function execute() { + if ( !$this->hasArg() ) + $this->error( "No query specified. Specify the query as a command line parameter.", true ); + + $query = $this->getArg(); + $wait = $this->getOption( 'wait', 5 ); + $n = 1; + $dbw = wfGetDb( DB_MASTER ); + do { + $this->output( "Batch $n: " ); + $n++; + $dbw->query( $query ); + $affected = $dbw->affectedRows(); + $this->output( "$affected rows\n" ); + wfWaitForSlaves( $wait ); + } while ( $affected > 0 ); + } + + protected function getDbType() { + return Maintenance::DB_ADMIN; + } +} + + +$maintClass = "BatchedQueryRunner"; +require_once( DO_MAINTENANCE );