MediaWiki  master
generateSchemaChangeSql.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__ . '/includes/SchemaMaintenance.php';
30 
37  public function __construct() {
38  parent::__construct();
39  $this->addDescription( 'Build SQL files for schema changes from abstract JSON files' );
40  $this->scriptName = 'generateSchemaChangeSql.php';
41  }
42 
43  protected function generateSchema( string $platform, array $schema ): string {
44  $schemaChangeBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaChangeBuilder( $platform );
45 
46  $schemaChangeSqls = $schemaChangeBuilder->getSchemaChangeSql( $schema );
47 
48  $sql = '';
49 
50  if ( $schemaChangeSqls !== [] ) {
51  // Temporary
52  $sql .= implode( ";\n\n", $schemaChangeSqls ) . ';';
53  $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql );
54  } else {
55  $this->fatalError( 'No schema changes detected!' );
56  }
57 
58  // Until the linting issue is resolved
59  // https://github.com/doctrine/sql-formatter/issues/53
60  $sql = str_replace( "\n/*_*/\n", " /*_*/", $sql );
61  $sql = str_replace( "; ", ";\n", $sql );
62  $sql = preg_replace( "/\n+? +?/", ' ', $sql );
63  $sql = str_replace( "/*_*/ ", "/*_*/", $sql );
64 
65  // Sqlite hacks
66  if ( $platform === 'sqlite' ) {
67  // Doctrine prepends __temp__ to the table name and we set the table with the schema prefix causing invalid
68  // sqlite.
69  $sql = preg_replace( '/__temp__\s*\/\*_\*\//', '/*_*/__temp__', $sql );
70  }
71 
72  return $sql;
73  }
74 
75 }
76 
77 $maintClass = GenerateSchemaChangeSql::class;
78 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance script to generate schema from abstract json files.
__construct()
Default constructor.
generateSchema(string $platform, array $schema)
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.