MediaWiki REL1_37
generateSchemaSql.php
Go to the documentation of this file.
1<?php
2
25use Doctrine\SqlFormatter\NullHighlighter;
26use Doctrine\SqlFormatter\SqlFormatter;
28
29require_once __DIR__ . '/Maintenance.php';
30
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Build SQL files from abstract JSON files' );
40
41 $this->addOption(
42 'json',
43 'Path to the json file. Default: tables.json',
44 false,
45 true
46 );
47 $this->addOption(
48 'sql',
49 'Path to output. Default: tables-generated.sql',
50 false,
51 true
52 );
53 $this->addOption(
54 'type',
55 'Can be either \'mysql\', \'sqlite\', or \'postgres\'. Default: mysql',
56 false,
57 true
58 );
59 }
60
61 public function execute() {
62 global $IP;
63 $platform = $this->getOption( 'type', 'mysql' );
64 $jsonPath = $this->getOption( 'json', __DIR__ . '/tables.json' );
65 $relativeJsonPath = str_replace(
66 [ "$IP/extensions/", "$IP/" ],
67 '',
68 $jsonPath
69 );
70 $sqlPath = $this->getOption( 'sql', __DIR__ . '/tables-generated.sql' );
71 $abstractSchema = json_decode( file_get_contents( $jsonPath ), true );
72 $schemaBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaBuilder( $platform );
73
74 if ( $abstractSchema === null ) {
75 $this->fatalError( "'$jsonPath' seems to be invalid json. Check the syntax and try again!" );
76 }
77
78 foreach ( $abstractSchema as $table ) {
79 $schemaBuilder->addTable( $table );
80 }
81 $sql = "-- This file is automatically generated using maintenance/generateSchemaSql.php.\n" .
82 "-- Source: $relativeJsonPath\n" .
83 "-- Do not modify this file directly.\n" .
84 "-- See https://www.mediawiki.org/wiki/Manual:Schema_changes\n";
85
86 $tables = $schemaBuilder->getSql();
87 if ( $tables !== [] ) {
88 // Temporary
89 $sql .= implode( ";\n\n", $tables ) . ';';
90 $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
91 }
92
93 // Postgres hacks
94 if ( $platform === 'postgres' ) {
95 // FIXME: Fix a lot of weird formatting issues caused by
96 // presence of partial index's WHERE clause, this should probably
97 // be done in some better way, but for now this can work temporarily
98 $sql = str_replace(
99 [ "WHERE\n ", "\n /*_*/\n ", " ", " );", "KEY(\n " ],
100 [ "WHERE", ' ', " ", ');', "KEY(\n " ],
101 $sql
102 );
103
104 // MySQL goes with varbinary for collation reasons, but postgres can't
105 // properly understand BYTEA type and works just fine with TEXT type
106 // FIXME: This should be fixed at some point (T257755)
107 $sql = str_replace( "BYTEA", 'TEXT', $sql );
108 }
109
110 // Until the linting issue is resolved
111 // https://github.com/doctrine/sql-formatter/issues/53
112 $sql = str_replace( "\n/*_*/\n", " /*_*/", $sql );
113 $sql = str_replace( "; CREATE ", ";\n\nCREATE ", $sql );
114 $sql = str_replace( ";\n\nCREATE TABLE ", ";\n\n\nCREATE TABLE ", $sql );
115 $sql = str_replace(
116 "\n" . '/*$wgDBTableOptions*/' . ";",
117 ' /*$wgDBTableOptions*/;',
118 $sql
119 );
120 $sql = str_replace(
121 "\n" . '/*$wgDBTableOptions*/' . "\n;",
122 ' /*$wgDBTableOptions*/;',
123 $sql
124 );
125 $sql .= "\n";
126
127 file_put_contents( $sqlPath, $sql );
128 }
129
130}
131
132$maintClass = GenerateSchemaSql::class;
133require_once RUN_MAINTENANCE_IF_MAIN;
$IP
Definition WebStart.php:49
Maintenance script to generate schema from abstract json files.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.