3 * Performs some operations specific to SQLite database backend.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 use MediaWiki\Maintenance\Maintenance
;
25 use Wikimedia\AtEase\AtEase
;
26 use Wikimedia\Rdbms\DatabaseSqlite
;
27 use Wikimedia\Rdbms\IMaintainableDatabase
;
29 // @codeCoverageIgnoreStart
30 require_once __DIR__
. '/Maintenance.php';
31 // @codeCoverageIgnoreEnd
34 * Maintenance script that performs some operations specific to SQLite database backend.
36 * @ingroup Maintenance
38 class SqliteMaintenance
extends Maintenance
{
39 public function __construct() {
40 parent
::__construct();
41 $this->addDescription( 'Performs some operations specific to SQLite database backend' );
44 'Clean up database by removing deleted pages. Decreases database file size'
46 $this->addOption( 'integrity', 'Check database for integrity' );
47 $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
48 $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
51 public function execute() {
52 // Should work even if we use a non-SQLite database
53 if ( $this->hasOption( 'check-syntax' ) ) {
58 $lb = $this->getServiceContainer()->getDBLoadBalancer();
59 $dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY
);
60 if ( $dbw->getType() !== 'sqlite' ) {
61 $this->error( "This maintenance script requires a SQLite database.\n" );
65 /** @var DatabaseSqlite $dbw */
66 '@phan-var DatabaseSqlite $dbw';
68 if ( $this->hasOption( 'vacuum' ) ) {
69 $this->vacuum( $dbw );
72 if ( $this->hasOption( 'integrity' ) ) {
73 $this->integrityCheck( $dbw );
76 if ( $this->hasOption( 'backup-to' ) ) {
77 $this->backup( $dbw, $this->getOption( 'backup-to' ) );
81 private function vacuum( DatabaseSqlite
$dbw ) {
82 $prevSize = filesize( $dbw->getDbFilePath() );
83 if ( $prevSize == 0 ) {
84 $this->fatalError( "Can't vacuum an empty database.\n" );
87 $this->output( 'VACUUM: ' );
88 if ( $dbw->query( 'VACUUM', __METHOD__
) ) {
90 $newSize = filesize( $dbw->getDbFilePath() );
91 $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
92 $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
94 $this->output( 'Error\n' );
98 private function integrityCheck( IMaintainableDatabase
$dbw ) {
99 $this->output( "Performing database integrity checks:\n" );
100 $res = $dbw->query( 'PRAGMA integrity_check', __METHOD__
);
102 if ( !$res ||
$res->numRows() == 0 ) {
103 $this->error( "Error: integrity check query returned nothing.\n" );
108 foreach ( $res as $row ) {
109 $this->output( $row->integrity_check
);
113 private function backup( DatabaseSqlite
$dbw, $fileName ) {
114 $this->output( "Backing up database:\n Locking..." );
115 $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__
);
116 $ourFile = $dbw->getDbFilePath();
117 $this->output( " Copying database file $ourFile to $fileName..." );
118 AtEase
::suppressWarnings();
119 if ( !copy( $ourFile, $fileName ) ) {
120 $err = error_get_last();
121 $this->error( " {$err['message']}" );
123 AtEase
::restoreWarnings();
124 $this->output( " Releasing lock...\n" );
125 $dbw->query( 'COMMIT TRANSACTION', __METHOD__
);
128 private function checkSyntax() {
129 if ( !Sqlite
::isPresent() ) {
130 $this->error( "Error: SQLite support not found\n" );
132 $files = [ $this->getOption( 'check-syntax' ) ];
133 $files = array_merge( $files, $this->mArgs
);
134 $result = Sqlite
::checkSqlSyntax( $files );
135 if ( $result === true ) {
136 $this->output( "SQL syntax check: no errors detected.\n" );
138 $this->error( "Error: $result\n" );
143 // @codeCoverageIgnoreStart
144 $maintClass = SqliteMaintenance
::class;
145 require_once RUN_MAINTENANCE_IF_MAIN
;
146 // @codeCoverageIgnoreEnd