2020-01-18 19:10:13 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a JSON abstract schema to a schema file in the given DBMS type
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2020-05-09 11:54:54 +00:00
|
|
|
use Doctrine\SqlFormatter\NullHighlighter;
|
|
|
|
|
use Doctrine\SqlFormatter\SqlFormatter;
|
2020-01-18 19:10:13 +00:00
|
|
|
use Wikimedia\Rdbms\DoctrineSchemaBuilderFactory;
|
|
|
|
|
|
2022-02-11 10:24:54 +00:00
|
|
|
require_once __DIR__ . '/includes/SchemaMaintenance.php';
|
2020-01-18 19:10:13 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maintenance script to generate schema from abstract json files.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2022-02-11 10:24:54 +00:00
|
|
|
class GenerateSchemaSql extends SchemaMaintenance {
|
2020-01-18 19:10:13 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addDescription( 'Build SQL files from abstract JSON files' );
|
2022-02-11 10:24:54 +00:00
|
|
|
$this->scriptName = 'generateSchemaSql.php';
|
2020-01-18 19:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-11 10:24:54 +00:00
|
|
|
protected function generateSchema( string $platform, array $schema ): string {
|
2020-11-12 15:43:44 +00:00
|
|
|
$schemaBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaBuilder( $platform );
|
|
|
|
|
|
2022-02-11 10:24:54 +00:00
|
|
|
foreach ( $schema as $table ) {
|
2020-01-18 19:10:13 +00:00
|
|
|
$schemaBuilder->addTable( $table );
|
|
|
|
|
}
|
2022-02-11 10:24:54 +00:00
|
|
|
$sql = '';
|
2020-05-09 11:54:54 +00:00
|
|
|
|
2020-05-09 14:27:42 +00:00
|
|
|
$tables = $schemaBuilder->getSql();
|
|
|
|
|
if ( $tables !== [] ) {
|
|
|
|
|
// Temporary
|
2021-02-19 23:05:36 +00:00
|
|
|
$sql .= implode( ";\n\n", $tables ) . ';';
|
2020-05-09 14:27:42 +00:00
|
|
|
$sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
|
|
|
|
|
}
|
2020-06-14 13:29:47 +00:00
|
|
|
|
2020-05-09 19:21:22 +00:00
|
|
|
// Postgres hacks
|
2020-11-12 15:43:44 +00:00
|
|
|
if ( $platform === 'postgres' ) {
|
2021-04-05 08:20:58 +00:00
|
|
|
// FIXME: Fix a lot of weird formatting issues caused by
|
2020-11-24 10:24:30 +00:00
|
|
|
// presence of partial index's WHERE clause, this should probably
|
2021-03-20 19:51:48 +00:00
|
|
|
// be done in some better way, but for now this can work temporarily
|
2020-11-24 10:24:30 +00:00
|
|
|
$sql = str_replace(
|
|
|
|
|
[ "WHERE\n ", "\n /*_*/\n ", " ", " );", "KEY(\n " ],
|
|
|
|
|
[ "WHERE", ' ', " ", ');', "KEY(\n " ],
|
|
|
|
|
$sql
|
|
|
|
|
);
|
2020-06-14 13:29:47 +00:00
|
|
|
}
|
2020-11-12 15:43:44 +00:00
|
|
|
|
2020-05-10 01:22:24 +00:00
|
|
|
// Until the linting issue is resolved
|
|
|
|
|
// https://github.com/doctrine/sql-formatter/issues/53
|
|
|
|
|
$sql = str_replace( "\n/*_*/\n", " /*_*/", $sql );
|
2020-08-19 10:03:24 +00:00
|
|
|
$sql = str_replace( "; CREATE ", ";\n\nCREATE ", $sql );
|
2020-08-20 10:04:57 +00:00
|
|
|
$sql = str_replace( ";\n\nCREATE TABLE ", ";\n\n\nCREATE TABLE ", $sql );
|
2020-05-10 01:22:24 +00:00
|
|
|
$sql = str_replace(
|
|
|
|
|
"\n" . '/*$wgDBTableOptions*/' . ";",
|
2020-08-19 10:03:24 +00:00
|
|
|
' /*$wgDBTableOptions*/;',
|
2020-05-10 01:22:24 +00:00
|
|
|
$sql
|
|
|
|
|
);
|
|
|
|
|
$sql = str_replace(
|
|
|
|
|
"\n" . '/*$wgDBTableOptions*/' . "\n;",
|
2020-08-19 10:03:24 +00:00
|
|
|
' /*$wgDBTableOptions*/;',
|
2020-05-10 01:22:24 +00:00
|
|
|
$sql
|
|
|
|
|
);
|
2020-08-19 10:03:24 +00:00
|
|
|
$sql .= "\n";
|
2020-05-09 11:54:54 +00:00
|
|
|
|
2022-02-11 10:24:54 +00:00
|
|
|
return $sql;
|
2020-01-18 19:10:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$maintClass = GenerateSchemaSql::class;
|
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|