27require_once __DIR__ .
'/Maintenance.php';
39 parent::__construct();
41 "Run an update query on all rows of a table. " .
42 "Waits for replicas at appropriate intervals." );
43 $this->
addOption(
'table',
'The table name',
true,
true );
44 $this->
addOption(
'set',
'The SET clause',
true,
true );
45 $this->
addOption(
'where',
'The WHERE clause',
false,
true );
46 $this->
addOption(
'key',
'A column name, the values of which are unique',
true,
true );
47 $this->
addOption(
'batch-size',
'The batch size (default 1000)',
false,
true );
48 $this->
addOption(
'db',
'The database name, or omit to use the current wiki.',
false,
true );
55 $where = $this->
getOption(
'where',
null );
56 $where = $where ===
null ? [] : [ $where ];
57 $batchSize = $this->
getOption(
'batch-size', 1000 );
60 if ( $dbName ===
null ) {
63 $dbw = $this->
getServiceContainer()->getConnectionProvider()->getPrimaryDatabase( $dbName );
66 $queryBuilder = $dbw->newSelectQueryBuilder()
70 ->caller( __METHOD__ );
73 foreach ( $iterator as $n => $batch ) {
74 $this->
output(
"Batch $n: " );
82 $firstRow = reset( $batch );
83 $lastRow = end( $batch );
85 $dbw->newUpdateQueryBuilder()
89 ->andWhere( $dbw->expr( $key,
'>=', $firstRow->$key ) )
90 ->andWhere( $dbw->expr( $key,
'<=', $lastRow->$key ) )
91 ->caller( __METHOD__ )
94 $affected = $dbw->affectedRows();
95 $this->
output(
"$affected rows affected\n" );
102 return Maintenance::DB_ADMIN;
108require_once RUN_MAINTENANCE_IF_MAIN;
Allows iterating a large number of rows in batches transparently.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Maintenance script to run a database query in batches and wait for replica DBs.
__construct()
Default constructor.
execute()
Do the actual work.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...