3 * Trait for finding SQL patch files.
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
23 namespace MediaWiki\DB
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * Trait for finding SQL patch files.
33 trait PatchFileLocation
{
36 * Utility function for finding the appropriate SQL patch file for the currently
39 * The file will be searched for in the following locations, in order of preference:
40 * "$patchDir/$name.$dbType.sql",
41 * "$patchDir/$dbType/$name.sql",
42 * "$patchDir/$dbType/archives/$name.sql",
43 * "$patchDir/$name.sql",
44 * "$patchDir/archives/$name.sql"
46 * @param IDatabase $db
47 * @param string $name The script name (relative to $patchDir, without the '.sql' suffix)
48 * @param string|null $patchDir The directory to find the script in. Use __DIR__ to search in the
49 * directory the calling code is located in. If omitted, the "maintenance"
50 * directory will be used, where the scripts used by the updater are located.
53 * @throws RuntimeException if no matching patch file could be found.
55 protected function getSqlPatchPath( IDatabase
$db, $name, $patchDir = null ) {
56 $dbType = $db->getType();
58 if ( $patchDir === null ) {
59 $patchDir = $GLOBALS['IP'] . '/maintenance';
64 // For a small number of patch files, closely associated with code,
65 // e.g. for unit tests:
66 "$patchDir/$name.$dbType.sql",
68 // For a large number of patch files, e.g. for schema updates of extensions:
69 "$patchDir/$dbType/$name.sql",
71 // For MediaWiki core schema update patches:
72 "$patchDir/$dbType/archives/$name.sql",
74 // Database-agnostic fallback:
75 "$patchDir/$name.sql",
77 // Database-agnostic fallback for MediaWiki core schema update patches:
78 "$patchDir/archives/$name.sql"
81 foreach ( $paths as $p ) {
82 if ( file_exists( $p ) ) {
87 throw new RuntimeException( "No SQL script matching $name could be found in $patchDir" );