Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| RunBatchedQuery | |
100.00% |
42 / 42 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
31 / 31 |
|
100.00% |
1 / 1 |
4 | |||
| getDbType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Run a database query in batches and wait for replica DBs. This is used on large |
| 4 | * wikis to prevent replication lag from going through the roof when executing |
| 5 | * large write queries. |
| 6 | * |
| 7 | * @license GPL-2.0-or-later |
| 8 | * @file |
| 9 | * @ingroup Maintenance |
| 10 | */ |
| 11 | |
| 12 | // @codeCoverageIgnoreStart |
| 13 | require_once __DIR__ . '/Maintenance.php'; |
| 14 | // @codeCoverageIgnoreEnd |
| 15 | |
| 16 | use MediaWiki\Maintenance\Maintenance; |
| 17 | |
| 18 | /** |
| 19 | * Maintenance script to run a database query in batches and wait for replica DBs. |
| 20 | * |
| 21 | * @ingroup Maintenance |
| 22 | */ |
| 23 | class RunBatchedQuery extends Maintenance { |
| 24 | public function __construct() { |
| 25 | parent::__construct(); |
| 26 | $this->addDescription( |
| 27 | "Run an update query on all rows of a table. " . |
| 28 | "Waits for replicas at appropriate intervals." ); |
| 29 | $this->addOption( 'table', 'The table name', true, true ); |
| 30 | $this->addOption( 'set', 'The SET clause', true, true ); |
| 31 | $this->addOption( 'where', 'The WHERE clause', false, true ); |
| 32 | $this->addOption( 'key', 'A column name, the values of which are unique', true, true ); |
| 33 | $this->addOption( 'batch-size', 'The batch size (default 1000)', false, true ); |
| 34 | $this->addOption( 'db', 'The database name, or omit to use the current wiki.', false, true ); |
| 35 | } |
| 36 | |
| 37 | public function execute() { |
| 38 | $table = $this->getOption( 'table' ); |
| 39 | $key = $this->getOption( 'key' ); |
| 40 | $set = $this->getOption( 'set' ); |
| 41 | $where = $this->getOption( 'where', null ); |
| 42 | $where = $where === null ? [] : [ $where ]; |
| 43 | $batchSize = $this->getOption( 'batch-size', 1000 ); |
| 44 | |
| 45 | $dbName = $this->getOption( 'db', null ); |
| 46 | if ( $dbName === null ) { |
| 47 | $dbw = $this->getPrimaryDB(); |
| 48 | } else { |
| 49 | $dbw = $this->getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $dbName ); |
| 50 | } |
| 51 | |
| 52 | $queryBuilder = $dbw->newSelectQueryBuilder() |
| 53 | ->select( $key ) |
| 54 | ->from( $table ) |
| 55 | ->where( $where ) |
| 56 | ->caller( __METHOD__ ); |
| 57 | |
| 58 | $iterator = new BatchRowIterator( $dbw, $queryBuilder, $key, $batchSize ); |
| 59 | foreach ( $iterator as $n => $batch ) { |
| 60 | $this->output( "Batch $n: " ); |
| 61 | |
| 62 | // Note that the update conditions do not rely on the atomicity of the |
| 63 | // SELECT query in order to guarantee that all rows are updated. The |
| 64 | // results of the SELECT are merely a partitioning hint. Simultaneous |
| 65 | // updates merely result in the wrong number of rows being updated |
| 66 | // in a batch. |
| 67 | |
| 68 | $firstRow = reset( $batch ); |
| 69 | $lastRow = end( $batch ); |
| 70 | |
| 71 | $dbw->newUpdateQueryBuilder() |
| 72 | ->table( $table ) |
| 73 | ->set( $set ) |
| 74 | ->where( $where ) |
| 75 | ->andWhere( $dbw->expr( $key, '>=', $firstRow->$key ) ) |
| 76 | ->andWhere( $dbw->expr( $key, '<=', $lastRow->$key ) ) |
| 77 | ->caller( __METHOD__ ) |
| 78 | ->execute(); |
| 79 | |
| 80 | $affected = $dbw->affectedRows(); |
| 81 | $this->output( "$affected rows affected\n" ); |
| 82 | $this->waitForReplication(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** @inheritDoc */ |
| 87 | public function getDbType() { |
| 88 | return Maintenance::DB_ADMIN; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // @codeCoverageIgnoreStart |
| 93 | $maintClass = RunBatchedQuery::class; |
| 94 | require_once RUN_MAINTENANCE_IF_MAIN; |
| 95 | // @codeCoverageIgnoreEnd |