MediaWiki  master
generateSchemaSql.php
Go to the documentation of this file.
1 <?php
2 
25 use Doctrine\SqlFormatter\NullHighlighter;
26 use Doctrine\SqlFormatter\SqlFormatter;
28 
29 require_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 = $sql . implode( ";\n\n", $tables ) . ';';
90  $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
91  }
92 
93  // Postgres hacks
94  if ( $platform === 'postgres' ) {
95  // Remove table prefixes from Postgres schema, people should not set it
96  // but better safe than sorry.
97  $sql = str_replace( "\n/*_*/\n", ' ', $sql );
98 
99  // FIXME: Also fix a lot of weird formatting issues caused by
100  // presence of partial index's WHERE clause, this should probably
101  // be done in some better way, but for now this can work temporaily
102  $sql = str_replace(
103  [ "WHERE\n ", "\n /*_*/\n ", " ", " );", "KEY(\n " ],
104  [ "WHERE", ' ', " ", ');', "KEY(\n " ],
105  $sql
106  );
107 
108  // MySQL goes with varbinary for collation reasons, but postgres can't
109  // properly understand BYTEA type and works just fine with TEXT type
110  // FIXME: This should be fixed at some point (T257755)
111  $sql = str_replace( "BYTEA", 'TEXT', $sql );
112  }
113 
114  if ( $platform === 'mysql' ) {
115  // Temporary
116  // Convert DOUBLE PRECISION (which is default float format in DBAL) to FLOAT
117  $sql = str_replace( "DOUBLE PRECISION", 'FLOAT', $sql );
118  }
119 
120  // Until the linting issue is resolved
121  // https://github.com/doctrine/sql-formatter/issues/53
122  $sql = str_replace( "\n/*_*/\n", " /*_*/", $sql );
123  $sql = str_replace( "; CREATE ", ";\n\nCREATE ", $sql );
124  $sql = str_replace( ";\n\nCREATE TABLE ", ";\n\n\nCREATE TABLE ", $sql );
125  $sql = str_replace(
126  "\n" . '/*$wgDBTableOptions*/' . ";",
127  ' /*$wgDBTableOptions*/;',
128  $sql
129  );
130  $sql = str_replace(
131  "\n" . '/*$wgDBTableOptions*/' . "\n;",
132  ' /*$wgDBTableOptions*/;',
133  $sql
134  );
135  $sql .= "\n";
136 
137  file_put_contents( $sqlPath, $sql );
138  }
139 
140 }
141 
142 $maintClass = GenerateSchemaSql::class;
143 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
$maintClass
Definition: generateSchemaSql.php:142
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:486
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
GenerateSchemaSql\__construct
__construct()
Default constructor.
Definition: generateSchemaSql.php:37
GenerateSchemaSql
Maintenance script to generate schema from abstract json files.
Definition: generateSchemaSql.php:36
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
GenerateSchemaSql\execute
execute()
Do the actual work.
Definition: generateSchemaSql.php:61
Wikimedia\Rdbms\DoctrineSchemaBuilderFactory
@experimental @unstable
Definition: DoctrineSchemaBuilderFactory.php:14
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
$IP
$IP
Definition: WebStart.php:49