Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
21 / 22
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PatchSql
95.45% covered (success)
95.45%
21 / 22
66.67% covered (warning)
66.67%
2 / 3
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getDbType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
4.00
1<?php
2/**
3 * Manually run an SQL patch outside of the general updaters.
4 * This ensures that the DB options (charset, prefix, engine) are correctly set.
5 *
6 * @license GPL-2.0-or-later
7 * @file
8 * @ingroup Maintenance
9 */
10
11use MediaWiki\Installer\DatabaseUpdater;
12use MediaWiki\Maintenance\Maintenance;
13
14// @codeCoverageIgnoreStart
15require_once __DIR__ . '/Maintenance.php';
16// @codeCoverageIgnoreEnd
17
18/**
19 * Maintenance script that manually runs an SQL patch outside of the general updaters.
20 *
21 * @ingroup Maintenance
22 */
23class PatchSql extends Maintenance {
24    public function __construct() {
25        parent::__construct();
26        $this->addDescription( 'Run an SQL file into the DB, replacing prefix and charset vars' );
27        $this->addArg(
28            'patch-name',
29            'Name of the patch file, either full path or in sql/$dbtype/'
30        );
31    }
32
33    /** @inheritDoc */
34    public function getDbType() {
35        return Maintenance::DB_ADMIN;
36    }
37
38    public function execute() {
39        $dbw = $this->getDB( DB_PRIMARY );
40        $updater = DatabaseUpdater::newForDB( $dbw, true, $this );
41
42        foreach ( $this->getArgs() as $name ) {
43            $files = [
44                $name,
45                $updater->patchPath( $dbw, $name ),
46                $updater->patchPath( $dbw, "patch-$name.sql" ),
47            ];
48            foreach ( $files as $file ) {
49                if ( file_exists( $file ) ) {
50                    $this->output( "$file ...\n" );
51                    $dbw->sourceFile( $file );
52                    continue 2;
53                }
54            }
55            $this->error( "Could not find $name\n" );
56        }
57        $this->output( "done.\n" );
58    }
59}
60
61// @codeCoverageIgnoreStart
62$maintClass = PatchSql::class;
63require_once RUN_MAINTENANCE_IF_MAIN;
64// @codeCoverageIgnoreEnd