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  $jsonPath = $this->getOption( 'json', __DIR__ . '/tables.json' );
64  $relativeJsonPath = str_replace( "$IP/", '', $jsonPath );
65  $sqlPath = $this->getOption( 'sql', __DIR__ . '/tables-generated.sql' );
66  $abstractSchema = json_decode( file_get_contents( $jsonPath ), true );
67  $schemaBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaBuilder(
68  $this->getOption( 'type', 'mysql' )
69  );
70  foreach ( $abstractSchema as $table ) {
71  $schemaBuilder->addTable( $table );
72  }
73  $sql = "-- This file is automatically generated using maintenance/generateSchemaSql.php.\n" .
74  "-- Source: $relativeJsonPath\n" .
75  "-- Do not modify this file directly.\n" .
76  "-- See https://www.mediawiki.org/wiki/Manual:Schema_changes\n";
77 
78  $tables = $schemaBuilder->getSql();
79  if ( $tables !== [] ) {
80  // Temporary
81  $sql = $sql . implode( ";\n\n", $tables ) . ';';
82  $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
83  }
84 
85  // Postgres hacks
86  if ( $this->getOption( 'type', 'mysql' ) === 'postgres' ) {
87  // Remove table prefixes from Postgres schema, people should not set it
88  // but better safe than sorry.
89  $sql = str_replace( "\n/*_*/\n", ' ', $sql );
90 
91  // MySQL goes with varbinary for collation reasons, but postgres can't
92  // properly understand BYTEA type and works just fine with TEXT type
93  // FIXME: This should be fixed at some point (T257755)
94  $sql = str_replace( "BYTEA", 'TEXT', $sql );
95  }
96  // Until the linting issue is resolved
97  // https://github.com/doctrine/sql-formatter/issues/53
98  $sql = str_replace( "\n/*_*/\n", " /*_*/", $sql );
99  $sql = str_replace( "; CREATE ", ";\nCREATE ", $sql );
100  $sql = str_replace(
101  "\n" . '/*$wgDBTableOptions*/' . ";",
102  ' /*$wgDBTableOptions*/;' . "\n",
103  $sql
104  );
105  $sql = str_replace(
106  "\n" . '/*$wgDBTableOptions*/' . "\n;",
107  ' /*$wgDBTableOptions*/;' . "\n",
108  $sql
109  );
110 
111  file_put_contents( $sqlPath, $sql );
112  }
113 
114 }
115 
116 $maintClass = GenerateSchemaSql::class;
117 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
$maintClass
$maintClass
Definition: generateSchemaSql.php:116
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