MediaWiki  master
DoctrineSchemaBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use Doctrine\DBAL\Platforms\AbstractPlatform;
6 use Doctrine\DBAL\Schema\Schema;
7 
13  private $schema;
14  private $platform;
15 
16  public const TABLE_PREFIX = '/*_*/';
17 
23  public function __construct( AbstractPlatform $platform ) {
24  $this->schema = new Schema();
25  $this->platform = $platform;
26  }
27 
31  public function addTable( array $schema ) {
32  $table = $this->schema->createTable( self::TABLE_PREFIX . $schema['name'] );
33  foreach ( $schema['columns'] as $column ) {
34  $table->addColumn( $column['name'], $column['type'], $column['options'] );
35  }
36  foreach ( $schema['indexes'] as $index ) {
37  if ( $index['unique'] === true ) {
38  $table->addUniqueIndex( $index['columns'], $index['name'] );
39  } else {
40  $table->addIndex( $index['columns'], $index['name'] );
41  }
42  }
43  $table->setPrimaryKey( $schema['pk'] );
44  $table->addOption( 'table_options', '/*$wgDBTableOptions*/' );
45  }
46 
50  public function getSql() {
51  return $this->schema->toSql( $this->platform );
52  }
53 }
Wikimedia\Rdbms\DoctrineSchemaBuilder\$schema
$schema
Definition: DoctrineSchemaBuilder.php:13
Wikimedia\Rdbms\DoctrineSchemaBuilder\TABLE_PREFIX
const TABLE_PREFIX
Definition: DoctrineSchemaBuilder.php:16
Wikimedia\Rdbms\DoctrineSchemaBuilder\getSql
getSql()
string[] SQLs to run
Definition: DoctrineSchemaBuilder.php:50
Wikimedia\Rdbms
Definition: ChronologyProtector.php:24
Wikimedia\Rdbms\SchemaBuilder
Interface SchemaBuilder that gets a definition and produces SQL based on RDBMS.
Definition: SchemaBuilder.php:11
Wikimedia\Rdbms\DoctrineSchemaBuilder\$platform
$platform
Definition: DoctrineSchemaBuilder.php:14
Wikimedia\Rdbms\DoctrineSchemaBuilder
@experimental @unstable
Definition: DoctrineSchemaBuilder.php:12
Wikimedia\Rdbms\DoctrineSchemaBuilder\__construct
__construct(AbstractPlatform $platform)
A builder object that take abstract schema definition and produces sql to create the tables.
Definition: DoctrineSchemaBuilder.php:23
Wikimedia\Rdbms\DoctrineSchemaBuilder\addTable
addTable(array $schema)
An example of $schema value: [ 'name' => 'actor', 'columns' => [ [ 'actor_id', 'bigint',...
Definition: DoctrineSchemaBuilder.php:31