MediaWiki master
DoctrineAbstractSchemaTrait.php
Go to the documentation of this file.
1<?php
6namespace Wikimedia\Rdbms;
7
8use Doctrine\DBAL\Platforms\AbstractPlatform;
9use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
10use Doctrine\DBAL\Schema\Schema;
11
18
19 private AbstractPlatform $platform;
20
21 private function addTableToSchema( Schema $schema, array $schemaSpec ): Schema {
22 $prefix = ( $this->platform instanceof PostgreSQLPlatform ) ? '' : '/*_*/';
23
24 $table = $schema->createTable( $prefix . $schemaSpec['name'] );
25 foreach ( $schemaSpec['columns'] as $column ) {
26 $table->addColumn( $column['name'], $column['type'], $column['options'] );
27 }
28
29 foreach ( $schemaSpec['indexes'] as $index ) {
30 if ( $index['unique'] === true ) {
31 $table->addUniqueIndex( $index['columns'], $index['name'], $index['options'] ?? [] );
32 } else {
33 $table->addIndex( $index['columns'], $index['name'], $index['flags'] ?? [], $index['options'] ?? [] );
34 }
35 }
36
37 if ( isset( $schemaSpec['pk'] ) && $schemaSpec['pk'] !== [] ) {
38 $table->setPrimaryKey( $schemaSpec['pk'] );
39 }
40
41 if ( isset( $schemaSpec['table_options'] ) ) {
42 $table->addOption( 'table_options', implode( ' ', $schemaSpec['table_options'] ) );
43 } else {
44 $table->addOption( 'table_options', '/*$wgDBTableOptions*/' );
45 }
46
47 return $schema;
48 }
49}
trait DoctrineAbstractSchemaTrait
Trait for schema spec of doctrine-based abstract schema.