MediaWiki REL1_40
generateSchemaSql.php
Go to the documentation of this file.
1<?php
2
25use Doctrine\SqlFormatter\NullHighlighter;
26use Doctrine\SqlFormatter\SqlFormatter;
28
29require_once __DIR__ . '/includes/SchemaMaintenance.php';
30
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Build SQL files from abstract JSON files' );
40 $this->scriptName = 'generateSchemaSql.php';
41 }
42
43 protected function generateSchema( string $platform, array $schema ): string {
44 $schemaBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaBuilder( $platform );
45
46 foreach ( $schema as $table ) {
47 $schemaBuilder->addTable( $table );
48 }
49 $sql = '';
50
51 $tables = $schemaBuilder->getSql();
52 if ( $tables !== [] ) {
53 // Temporary
54 $sql .= implode( ";\n\n", $tables ) . ';';
55 $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
56 }
57
58 // Postgres hacks
59 if ( $platform === 'postgres' ) {
60 // FIXME: Fix a lot of weird formatting issues caused by
61 // presence of partial index's WHERE clause, this should probably
62 // be done in some better way, but for now this can work temporarily
63 $sql = str_replace(
64 [ "WHERE\n ", "\n /*_*/\n ", " ", " );", "KEY(\n " ],
65 [ "WHERE", ' ', " ", ');', "KEY(\n " ],
66 $sql
67 );
68 }
69
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( "; CREATE ", ";\n\nCREATE ", $sql );
74 $sql = str_replace( ";\n\nCREATE TABLE ", ";\n\n\nCREATE TABLE ", $sql );
75 $sql = str_replace(
76 "\n" . '/*$wgDBTableOptions*/' . ";",
77 ' /*$wgDBTableOptions*/;',
78 $sql
79 );
80 $sql = str_replace(
81 "\n" . '/*$wgDBTableOptions*/' . "\n;",
82 ' /*$wgDBTableOptions*/;',
83 $sql
84 );
85 $sql .= "\n";
86
87 return $sql;
88 }
89
90}
91
92$maintClass = GenerateSchemaSql::class;
93require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to generate schema from abstract json files.
generateSchema(string $platform, array $schema)
__construct()
Default constructor.
addDescription( $text)
Set the description text.