aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>2020-05-16 18:36:38 +0000
committerGerrit Code Review <gerrit@wikimedia.org>2020-05-16 18:36:38 +0000
commitc54453b7e9d2b56747f6589033fc4565cf056c90 (patch)
tree71ff2a0b75d7c8643716bbc71135672c87bdfe93
parent9fb14c42aaa7ce2dad48e60205f548231b673e11 (diff)
parentbfe636bb187d19cfc295b90b8b3b0877e480266e (diff)
downloadmediawikicore-c54453b7e9d2b56747f6589033fc4565cf056c90.tar.gz
mediawikicore-c54453b7e9d2b56747f6589033fc4565cf056c90.zip
Merge "Add test to compare generated sql with the abstract schema"
-rw-r--r--maintenance/generateSchemaSql.php12
-rw-r--r--tests/phpunit/structure/DatabaseIntegrationTest.php28
2 files changed, 35 insertions, 5 deletions
diff --git a/maintenance/generateSchemaSql.php b/maintenance/generateSchemaSql.php
index 75f32852ce75..200a4a7660bd 100644
--- a/maintenance/generateSchemaSql.php
+++ b/maintenance/generateSchemaSql.php
@@ -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 );
}
}
diff --git a/tests/phpunit/structure/DatabaseIntegrationTest.php b/tests/phpunit/structure/DatabaseIntegrationTest.php
index 2f9d6d898b4f..6e86eb59e41c 100644
--- a/tests/phpunit/structure/DatabaseIntegrationTest.php
+++ b/tests/phpunit/structure/DatabaseIntegrationTest.php
@@ -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"
+ );
+ }
}