Merge "Introduce DoctrineAbstractSchemaTrait to share schema spec logic"
This commit is contained in:
commit
8cafbac2b8
4 changed files with 61 additions and 40 deletions
|
|
@ -1849,6 +1849,7 @@ $wgAutoloadLocalClasses = [
|
|||
'Wikimedia\\Rdbms\\DatabaseMysqli' => __DIR__ . '/includes/libs/rdbms/database/DatabaseMysqli.php',
|
||||
'Wikimedia\\Rdbms\\DatabasePostgres' => __DIR__ . '/includes/libs/rdbms/database/DatabasePostgres.php',
|
||||
'Wikimedia\\Rdbms\\DatabaseSqlite' => __DIR__ . '/includes/libs/rdbms/database/DatabaseSqlite.php',
|
||||
'Wikimedia\\Rdbms\\DoctrineAbstractSchemaTrait' => __DIR__ . '/includes/libs/rdbms/database/DoctrineAbstractSchemaTrait.php',
|
||||
'Wikimedia\\Rdbms\\DoctrineSchemaBuilder' => __DIR__ . '/includes/libs/rdbms/database/DoctrineSchemaBuilder.php',
|
||||
'Wikimedia\\Rdbms\\DoctrineSchemaBuilderFactory' => __DIR__ . '/includes/libs/rdbms/database/DoctrineSchemaBuilderFactory.php',
|
||||
'Wikimedia\\Rdbms\\DoctrineSchemaChangeBuilder' => __DIR__ . '/includes/libs/rdbms/database/DoctrineSchemaChangeBuilder.php',
|
||||
|
|
|
|||
54
includes/libs/rdbms/database/DoctrineAbstractSchemaTrait.php
Normal file
54
includes/libs/rdbms/database/DoctrineAbstractSchemaTrait.php
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
/**
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
namespace Wikimedia\Rdbms;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* Trait for schema spec of doctrine-based abstract schema
|
||||
* @since 1.36
|
||||
* @internal
|
||||
*/
|
||||
trait DoctrineAbstractSchemaTrait {
|
||||
private function addTableToSchema( Schema $schema, array $schemaSpec ) {
|
||||
$table = $schema->createTable( '/*_*/' . $schemaSpec['name'] );
|
||||
foreach ( $schemaSpec['columns'] as $column ) {
|
||||
$table->addColumn( $column['name'], $column['type'], $column['options'] );
|
||||
}
|
||||
foreach ( $schemaSpec['indexes'] as $index ) {
|
||||
if ( $index['unique'] === true ) {
|
||||
$table->addUniqueIndex( $index['columns'], $index['name'], $index['options'] ?? [] );
|
||||
} else {
|
||||
$table->addIndex( $index['columns'], $index['name'], $index['flags'] ?? [], $index['options'] ?? [] );
|
||||
}
|
||||
}
|
||||
$table->setPrimaryKey( $schemaSpec['pk'] );
|
||||
|
||||
if ( isset( $schemaSpec['options'] )
|
||||
&& isset( $schemaSpec['options'][0]['table_options'] )
|
||||
) {
|
||||
$table->addOption( 'table_options', $schemaSpec['options'][0]['table_options'] );
|
||||
} else {
|
||||
$table->addOption( 'table_options', '/*$wgDBTableOptions*/' );
|
||||
}
|
||||
|
||||
return $schema;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,11 +10,11 @@ use Doctrine\DBAL\Schema\Schema;
|
|||
* @unstable
|
||||
*/
|
||||
class DoctrineSchemaBuilder implements SchemaBuilder {
|
||||
use DoctrineAbstractSchemaTrait;
|
||||
|
||||
private $schema;
|
||||
private $platform;
|
||||
|
||||
public const TABLE_PREFIX = '/*_*/';
|
||||
|
||||
/**
|
||||
* A builder object that take abstract schema definition and produces sql to create the tables.
|
||||
*
|
||||
|
|
@ -29,26 +29,7 @@ class DoctrineSchemaBuilder implements SchemaBuilder {
|
|||
* @inheritDoc
|
||||
*/
|
||||
public function addTable( array $schema ) {
|
||||
$table = $this->schema->createTable( self::TABLE_PREFIX . $schema['name'] );
|
||||
foreach ( $schema['columns'] as $column ) {
|
||||
$table->addColumn( $column['name'], $column['type'], $column['options'] );
|
||||
}
|
||||
foreach ( $schema['indexes'] as $index ) {
|
||||
if ( $index['unique'] === true ) {
|
||||
$table->addUniqueIndex( $index['columns'], $index['name'], $index['options'] ?? [] );
|
||||
} else {
|
||||
$table->addIndex( $index['columns'], $index['name'], $index['flags'] ?? [], $index['options'] ?? [] );
|
||||
}
|
||||
}
|
||||
$table->setPrimaryKey( $schema['pk'] );
|
||||
|
||||
if ( isset( $schema['options'] )
|
||||
&& isset( $schema['options'][0]['table_options'] )
|
||||
) {
|
||||
$table->addOption( 'table_options', $schema['options'][0]['table_options'] );
|
||||
} else {
|
||||
$table->addOption( 'table_options', '/*$wgDBTableOptions*/' );
|
||||
}
|
||||
$this->addTableToSchema( $this->schema, $schema );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -11,9 +11,9 @@ use Doctrine\DBAL\Schema\Schema;
|
|||
* @unstable
|
||||
*/
|
||||
class DoctrineSchemaChangeBuilder implements SchemaChangeBuilder {
|
||||
private $platform;
|
||||
use DoctrineAbstractSchemaTrait;
|
||||
|
||||
public const TABLE_PREFIX = '/*_*/';
|
||||
private $platform;
|
||||
|
||||
/**
|
||||
* A builder object that take abstract schema definition and produces sql to create the tables.
|
||||
|
|
@ -28,22 +28,7 @@ class DoctrineSchemaChangeBuilder implements SchemaChangeBuilder {
|
|||
* @inheritDoc
|
||||
*/
|
||||
private function getTableSchema( array $tableSpec ): Schema {
|
||||
$schema = new Schema();
|
||||
$table = $schema->createTable( self::TABLE_PREFIX . $tableSpec['name'] );
|
||||
foreach ( $tableSpec['columns'] as $column ) {
|
||||
$table->addColumn( $column['name'], $column['type'], $column['options'] );
|
||||
}
|
||||
foreach ( $tableSpec['indexes'] as $index ) {
|
||||
if ( $index['unique'] === true ) {
|
||||
$table->addUniqueIndex( $index['columns'], $index['name'] );
|
||||
} else {
|
||||
$table->addIndex( $index['columns'], $index['name'] );
|
||||
}
|
||||
}
|
||||
$table->setPrimaryKey( $tableSpec['pk'] );
|
||||
$table->addOption( 'table_options', '/*$wgDBTableOptions*/' );
|
||||
|
||||
return $schema;
|
||||
return $this->addTableToSchema( new Schema(), $tableSpec );
|
||||
}
|
||||
|
||||
public function getSchemaChangeSql( array $schemaChangeSpec ): array {
|
||||
|
|
|
|||
Loading…
Reference in a new issue