diff options
author | Umherirrender <umherirrender_de.wp@web.de> | 2021-12-01 20:14:06 +0100 |
---|---|---|
committer | Umherirrender <umherirrender_de.wp@web.de> | 2022-01-24 21:02:15 +0100 |
commit | 5f4bcf66e19caa4792595814d71026cdb328935c (patch) | |
tree | f229bb16bff8fc23ec13cad3ce7866f68fb7e074 /maintenance/generateSchemaChangeSql.php | |
parent | f2915281696a4c60f9aa606bab3bff1abb9b29a3 (diff) | |
download | mediawikicore-5f4bcf66e19caa4792595814d71026cdb328935c.tar.gz mediawikicore-5f4bcf66e19caa4792595814d71026cdb328935c.zip |
Improve generateSchemaChangeSql.php/generateSchemaSql.php
- Allow to specify a folder with --sql and get the name from json
filename or use the default sql filename.
- Show output that the script has something done.
Change-Id: I0dbec168efac465c962ab7184953bc2461e85d1d
Diffstat (limited to 'maintenance/generateSchemaChangeSql.php')
-rw-r--r-- | maintenance/generateSchemaChangeSql.php | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/maintenance/generateSchemaChangeSql.php b/maintenance/generateSchemaChangeSql.php index 31587029c95f..82fed62a14d7 100644 --- a/maintenance/generateSchemaChangeSql.php +++ b/maintenance/generateSchemaChangeSql.php @@ -60,6 +60,7 @@ class GenerateSchemaChangeSql extends Maintenance { public function execute() { global $IP; + $platform = $this->getOption( 'type', 'mysql' ); $jsonPath = $this->getOption( 'json' ); $installPath = $IP; // For windows @@ -69,15 +70,17 @@ class GenerateSchemaChangeSql extends Maintenance { } $relativeJsonPath = str_replace( "$installPath/", '', $jsonPath ); $sqlPath = $this->getOption( 'sql' ); + // Allow to specify a folder and build the name from the json filename + if ( is_dir( $sqlPath ) ) { + $sqlPath .= '/' . pathinfo( $relativeJsonPath, PATHINFO_FILENAME ) . '.sql'; + } $abstractSchemaChange = json_decode( file_get_contents( $jsonPath ), true ); if ( $abstractSchemaChange === null ) { $this->fatalError( "'$jsonPath' seems to be invalid json. Check the syntax and try again!" ); } - $schemaChangeBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaChangeBuilder( - $this->getOption( 'type', 'mysql' ) - ); + $schemaChangeBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaChangeBuilder( $platform ); $schemaChangeSqls = $schemaChangeBuilder->getSchemaChangeSql( $abstractSchemaChange ); @@ -90,10 +93,12 @@ class GenerateSchemaChangeSql extends Maintenance { // Temporary $sql .= implode( ";\n\n", $schemaChangeSqls ) . ';'; $sql = ( new SqlFormatter( new NullHighlighter() ) )->format( $sql ); + } else { + $this->error( 'No schema changes detected!' ); } // Postgres hacks - if ( $this->getOption( 'type', 'mysql' ) === 'postgres' ) { + if ( $platform === 'postgres' ) { // Remove table prefixes from Postgres schema, people should not set it // but better safe than sorry. $sql = str_replace( "\n/*_*/\n", ' ', $sql ); @@ -112,13 +117,22 @@ class GenerateSchemaChangeSql extends Maintenance { $sql = str_replace( "/*_*/ ", "/*_*/", $sql ); // Sqlite hacks - if ( $this->getOption( 'type', 'mysql' ) === 'sqlite' ) { + if ( $platform === 'sqlite' ) { // Doctrine prepends __temp__ to the table name and we set the table with the schema prefix causing invalid // sqlite. $sql = preg_replace( '/__temp__\s*\/\*_\*\//', '/*_*/__temp__', $sql ); } + // Give a hint, if nothing changed + if ( is_readable( $sqlPath ) ) { + $oldSql = file_get_contents( $sqlPath ); + if ( $oldSql === $sql ) { + $this->output( "Schema change is unchanged.\n" ); + } + } + file_put_contents( $sqlPath, $sql ); + $this->output( 'Schema change generated and written to ' . $sqlPath . "\n" ); } } |