Add test to compare generated sql with the abstract schema

This helps preventing mistakes, for example if you don't know or you
forget to generate the sql schema for one or all of them.
Also it helps preventing malicious changes to the generated files that
might go in without much scrutiny as the manual files.

Bug: T252919
Change-Id: I50e2715a55914f7712f9925c149bd09e8265a20b
This commit is contained in:
Amir Sarabadani 2020-05-16 00:57:27 +02:00
parent be96cf6cd8
commit bfe636bb18
2 changed files with 35 additions and 5 deletions

View file

@ -59,9 +59,11 @@ class GenerateSchemaSql extends Maintenance {
}
public function execute() {
$jsonFile = $this->getOption( 'json', __DIR__ . '/tables.json' );
$sqlFile = $this->getOption( 'sql', __DIR__ . '/tables-generated.sql' );
$abstractSchema = json_decode( file_get_contents( $jsonFile ), true );
global $IP;
$jsonPath = $this->getOption( 'json', __DIR__ . '/tables.json' );
$relativeJsonPath = str_replace( "$IP/", '', $jsonPath );
$sqlPath = $this->getOption( 'sql', __DIR__ . '/tables-generated.sql' );
$abstractSchema = json_decode( file_get_contents( $jsonPath ), true );
$schemaBuilder = ( new DoctrineSchemaBuilderFactory() )->getSchemaBuilder(
$this->getOption( 'type', 'mysql' )
);
@ -69,7 +71,7 @@ class GenerateSchemaSql extends Maintenance {
$schemaBuilder->addTable( $table );
}
$sql = "-- This file is automatically generated using maintenance/generateSchemaSql.php.\n" .
"-- Source: $jsonFile\n" .
"-- Source: $relativeJsonPath\n" .
"-- Do not modify this file directly.\n" .
"-- See https://www.mediawiki.org/wiki/Manual:Schema_changes\n";
@ -94,7 +96,7 @@ class GenerateSchemaSql extends Maintenance {
$sql
);
file_put_contents( $sqlFile, $sql );
file_put_contents( $sqlPath, $sql );
}
}

View file

@ -53,4 +53,32 @@ class DatabaseIntegrationTest extends MediaWikiTestCase {
$this->assertFalse( $this->db->tableExists( 'foobarbaz' ) );
$this->assertIsInt( $res->numRows() );
}
public function automaticSqlGenerationParams() {
return [
[ 'mysql', '/maintenance/tables-generated.sql' ],
[ 'sqlite', '/maintenance/sqlite/tables-generated.sql' ],
[ 'postgres', '/maintenance/postgres/tables-generated.sql' ],
];
}
/**
* @dataProvider automaticSqlGenerationParams
*/
public function testAutomaticSqlGeneration( $type, $sqlPath ) {
global $IP;
$abstractSchemaPath = "$IP/maintenance/tables.json";
$mysqlPath = $IP . $sqlPath;
$oldContent = file_get_contents( $mysqlPath );
$maintenanceScript = new GenerateSchemaSql();
$maintenanceScript->loadWithArgv(
[ '--json=' . $abstractSchemaPath, '--sql=' . $mysqlPath, '--type=' . $type ]
);
$maintenanceScript->execute();
$this->assertEquals(
$oldContent,
file_get_contents( $mysqlPath ),
"The generated schema in '$type' type has to be the same"
);
}
}