Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SqliteMaintenance
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 6
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 vacuum
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 integrityCheck
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 backup
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 checkSyntax
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Performs some operations specific to SQLite database backend.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24use Wikimedia\AtEase\AtEase;
25use Wikimedia\Rdbms\DBConnRef;
26use Wikimedia\Rdbms\IMaintainableDatabase;
27
28require_once __DIR__ . '/Maintenance.php';
29
30/**
31 * Maintenance script that performs some operations specific to SQLite database backend.
32 *
33 * @ingroup Maintenance
34 */
35class SqliteMaintenance extends Maintenance {
36    public function __construct() {
37        parent::__construct();
38        $this->addDescription( 'Performs some operations specific to SQLite database backend' );
39        $this->addOption(
40            'vacuum',
41            'Clean up database by removing deleted pages. Decreases database file size'
42        );
43        $this->addOption( 'integrity', 'Check database for integrity' );
44        $this->addOption( 'backup-to', 'Backup database to the given file', false, true );
45        $this->addOption( 'check-syntax', 'Check SQL file(s) for syntax errors', false, true );
46    }
47
48    public function execute() {
49        // Should work even if we use a non-SQLite database
50        if ( $this->hasOption( 'check-syntax' ) ) {
51            $this->checkSyntax();
52            return;
53        }
54
55        $lb = $this->getServiceContainer()->getDBLoadBalancer();
56        $dbw = $lb->getMaintenanceConnectionRef( DB_PRIMARY );
57        if ( $dbw->getType() !== 'sqlite' ) {
58            $this->error( "This maintenance script requires a SQLite database.\n" );
59
60            return;
61        }
62
63        if ( $this->hasOption( 'vacuum' ) ) {
64            $this->vacuum( $dbw );
65        }
66
67        if ( $this->hasOption( 'integrity' ) ) {
68            $this->integrityCheck( $dbw );
69        }
70
71        if ( $this->hasOption( 'backup-to' ) ) {
72            $this->backup( $dbw, $this->getOption( 'backup-to' ) );
73        }
74    }
75
76    private function vacuum( DBConnRef $dbw ) {
77        // Call non-standard DatabaseSqlite::getDbFilePath method
78        $prevSize = filesize( $dbw->__call( 'getDbFilePath', [] ) );
79        if ( $prevSize == 0 ) {
80            $this->fatalError( "Can't vacuum an empty database.\n" );
81        }
82
83        $this->output( 'VACUUM: ' );
84        if ( $dbw->query( 'VACUUM', __METHOD__ ) ) {
85            clearstatcache();
86            $newSize = filesize( $dbw->getDbFilePath() );
87            $this->output( sprintf( "Database size was %d, now %d (%.1f%% reduction).\n",
88                $prevSize, $newSize, ( $prevSize - $newSize ) * 100.0 / $prevSize ) );
89        } else {
90            $this->output( 'Error\n' );
91        }
92    }
93
94    private function integrityCheck( IMaintainableDatabase $dbw ) {
95        $this->output( "Performing database integrity checks:\n" );
96        $res = $dbw->query( 'PRAGMA integrity_check', __METHOD__ );
97
98        if ( !$res || $res->numRows() == 0 ) {
99            $this->error( "Error: integrity check query returned nothing.\n" );
100
101            return;
102        }
103
104        foreach ( $res as $row ) {
105            $this->output( $row->integrity_check );
106        }
107    }
108
109    private function backup( DBConnRef $dbw, $fileName ) {
110        $this->output( "Backing up database:\n   Locking..." );
111        $dbw->query( 'BEGIN IMMEDIATE TRANSACTION', __METHOD__ );
112        $ourFile = $dbw->__call( 'getDbFilePath', [] );
113        $this->output( "   Copying database file $ourFile to $fileName..." );
114        AtEase::suppressWarnings();
115        if ( !copy( $ourFile, $fileName ) ) {
116            $err = error_get_last();
117            $this->error( "      {$err['message']}" );
118        }
119        AtEase::restoreWarnings();
120        $this->output( "   Releasing lock...\n" );
121        $dbw->query( 'COMMIT TRANSACTION', __METHOD__ );
122    }
123
124    private function checkSyntax() {
125        if ( !Sqlite::isPresent() ) {
126            $this->error( "Error: SQLite support not found\n" );
127        }
128        $files = [ $this->getOption( 'check-syntax' ) ];
129        $files = array_merge( $files, $this->mArgs );
130        $result = Sqlite::checkSqlSyntax( $files );
131        if ( $result === true ) {
132            $this->output( "SQL syntax check: no errors detected.\n" );
133        } else {
134            $this->error( "Error: $result\n" );
135        }
136    }
137}
138
139$maintClass = SqliteMaintenance::class;
140require_once RUN_MAINTENANCE_IF_MAIN;