Add partial support for running Parsoid selser tests
[mediawiki.git] / maintenance / generateSchemaChangeSql.php
blob20aa4177ded6048a1749f17cb5e7eafe87fc3c1e
1 <?php
3 /**
4 * Convert a JSON abstract schema change to a schema change file in the given DBMS type
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
21 * @file
22 * @ingroup Maintenance
25 use Doctrine\SqlFormatter\NullHighlighter;
26 use Doctrine\SqlFormatter\SqlFormatter;
27 use Wikimedia\Rdbms\DoctrineSchemaBuilderFactory;
29 require_once __DIR__ . '/includes/SchemaMaintenance.php';
31 /**
32 * Maintenance script to generate schema from abstract json files.
34 * @ingroup Maintenance
36 class GenerateSchemaChangeSql extends SchemaMaintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Build SQL files for schema changes from abstract JSON files' );
40 $this->scriptName = 'generateSchemaChangeSql.php';
43 protected function generateSchema( string $platform, array $schema ): string {
44 $schemaChangeBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaChangeBuilder( $platform );
46 $schemaChangeSqls = $schemaChangeBuilder->getSchemaChangeSql( $schema );
48 $sql = '';
50 if ( $schemaChangeSqls !== [] ) {
51 // Temporary
52 $sql .= implode( ";\n\n", $schemaChangeSqls ) . ';';
53 $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
54 } else {
55 $this->fatalError( 'No schema changes detected!' );
58 // Postgres hacks
59 if ( $platform === 'postgres' ) {
60 // Remove table prefixes from Postgres schema, people should not set it
61 // but better safe than sorry.
62 $sql = str_replace( "\n/*_*/\n", ' ', $sql );
64 // MySQL goes with varbinary for collation reasons, but postgres can't
65 // properly understand BYTEA type and works just fine with TEXT type
66 // FIXME: This should be fixed at some point (T257755)
67 $sql = str_replace( "BYTEA", 'TEXT', $sql );
70 // Until the linting issue is resolved
71 // https://github.com/doctrine/sql-formatter/issues/53
72 $sql = str_replace( "\n/*_*/\n", " /*_*/", $sql );
73 $sql = str_replace( "; ", ";\n", $sql );
74 $sql = preg_replace( "/\n+? +?/", ' ', $sql );
75 $sql = str_replace( "/*_*/ ", "/*_*/", $sql );
77 // Sqlite hacks
78 if ( $platform === 'sqlite' ) {
79 // Doctrine prepends __temp__ to the table name and we set the table with the schema prefix causing invalid
80 // sqlite.
81 $sql = preg_replace( '/__temp__\s*\/\*_\*\//', '/*_*/__temp__', $sql );
84 return $sql;
89 $maintClass = GenerateSchemaChangeSql::class;
90 require_once RUN_MAINTENANCE_IF_MAIN;