wiki.techinc.nl/maintenance/SqliteMaintenance.php
Dreamy Jazz e7393b3cc7 Exclude boilerplate maintenance code from code coverage reports
Why:
* Maintenance scripts in core have bolierplate code that is
  added before and after the class to allow directly running
  the maintenance script.
* Running the maintenance script directly has been deprecated
  since 1.40, so this boilerplate code is only to support a now
  deprecated method of running maintenance scripts.
* This code cannot also be marked as covered, due to PHPUnit
  not recognising code coverage for files.
* Therefore, it is best to ignore this boilerplate code in code
  coverage reports as it cannot be marked as covered and also
  is for deprecated code.

What:
* Wrap the boilerplate code (requiring Maintenance.php and then
  later defining the maintenance script class and running if the
  maintenance script was called directly) with @codeCoverageIgnore
  comments.
* Some files use a different boilerplate code, however, these
  should also be marked as ignored for coverage for the same
  reason that coverage is not properly reported for files.

Bug: T371167
Change-Id: I32f5c6362dfb354149a48ce9c28da9a7fc494f7c
2024-08-27 13:22:29 +01:00

145 lines
4.5 KiB
PHP

<?php
/**
* Performs some operations specific to SQLite database backend.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup Maintenance
*/
use Wikimedia\AtEase\AtEase;
use Wikimedia\Rdbms\DatabaseSqlite;
use Wikimedia\Rdbms\IMaintainableDatabase;
// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd
/**
* Maintenance script that performs some operations specific to SQLite database backend.
*
* @ingroup Maintenance
*/
class SqliteMaintenance extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Performs some operations specific to SQLite database backend' );
$this->addOption(
'vacuum',
'Clean up database by removing deleted pages. Decreases database file size'
);
$this->addOption( 'integrity', 'Check database for integrity' );
$this->addOption( 'backup-to', 'Backup database to the given file', false, true );
$this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
}
public function execute() {
// Should work even if we use a non-SQLite database
if ( $this->hasOption( 'check-syntax' ) ) {
$this->checkSyntax();
return;
}
$lb = $this->getServiceContainer()->getDBLoadBalancer();
$dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY );
if ( $dbw->getType() !== 'sqlite' ) {
$this->error( "This maintenance script requires a SQLite database.\n" );
return;
}
/** @var DatabaseSqlite $dbw */
'@phan-var DatabaseSqlite $dbw';
if ( $this->hasOption( 'vacuum' ) ) {
$this->vacuum( $dbw );
}
if ( $this->hasOption( 'integrity' ) ) {
$this->integrityCheck( $dbw );
}
if ( $this->hasOption( 'backup-to' ) ) {
$this->backup( $dbw, $this->getOption( 'backup-to' ) );
}
}
private function vacuum( DatabaseSqlite $dbw ) {
$prevSize = filesize( $dbw->getDbFilePath() );
if ( $prevSize == 0 ) {
$this->fatalError( "Can't vacuum an empty database.\n" );
}
$this->output( 'VACUUM: ' );
if ( $dbw->query( 'VACUUM', __METHOD__ ) ) {
clearstatcache();
$newSize = filesize( $dbw->getDbFilePath() );
$this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
$prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
} else {
$this->output( 'Error\n' );
}
}
private function integrityCheck( IMaintainableDatabase $dbw ) {
$this->output( "Performing database integrity checks:\n" );
$res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ );
if ( !$res || $res->numRows() == 0 ) {
$this->error( "Error: integrity check query returned nothing.\n" );
return;
}
foreach ( $res as $row ) {
$this->output( $row->integrity_check );
}
}
private function backup( DatabaseSqlite $dbw, $fileName ) {
$this->output( "Backing up database:\n Locking..." );
$dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
$ourFile = $dbw->getDbFilePath();
$this->output( " Copying database file $ourFile to $fileName..." );
AtEase::suppressWarnings();
if ( !copy( $ourFile, $fileName ) ) {
$err = error_get_last();
$this->error( " {$err['message']}" );
}
AtEase::restoreWarnings();
$this->output( " Releasing lock...\n" );
$dbw->query( 'COMMIT TRANSACTION', __METHOD__ );
}
private function checkSyntax() {
if ( !Sqlite::isPresent() ) {
$this->error( "Error: SQLite support not found\n" );
}
$files = [ $this->getOption( 'check-syntax' ) ];
$files = array_merge( $files, $this->mArgs );
$result = Sqlite::checkSqlSyntax( $files );
if ( $result === true ) {
$this->output( "SQL syntax check: no errors detected.\n" );
} else {
$this->error( "Error: $result\n" );
}
}
}
// @codeCoverageIgnoreStart
$maintClass = SqliteMaintenance::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd