Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
DoctrineAbstractSchemaTrait
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
8.02
0.00% covered (danger)
0.00%
0 / 1
 addTableToSchema
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
8.02
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20namespace Wikimedia\Rdbms;
21
22use Doctrine\DBAL\Schema\Schema;
23
24/**
25 * Trait for schema spec of doctrine-based abstract schema
26 * @since 1.36
27 * @internal
28 */
29trait DoctrineAbstractSchemaTrait {
30
31    private $platform;
32
33    private function addTableToSchema( Schema $schema, array $schemaSpec ) {
34        $prefix = $this->platform->getName() === 'postgresql' ? '' : '/*_*/';
35
36        $table = $schema->createTable( $prefix . $schemaSpec['name'] );
37        foreach ( $schemaSpec['columns'] as $column ) {
38            $table->addColumn( $column['name'], $column['type'], $column['options'] );
39        }
40
41        foreach ( $schemaSpec['indexes'] as $index ) {
42            if ( $index['unique'] === true ) {
43                $table->addUniqueIndex( $index['columns'], $index['name'], $index['options'] ?? [] );
44            } else {
45                $table->addIndex( $index['columns'], $index['name'], $index['flags'] ?? [], $index['options'] ?? [] );
46            }
47        }
48
49        if ( isset( $schemaSpec['pk'] ) && $schemaSpec['pk'] !== [] ) {
50            $table->setPrimaryKey( $schemaSpec['pk'] );
51        }
52
53        if ( isset( $schemaSpec['table_options'] ) ) {
54            $table->addOption( 'table_options', implode( ' ', $schemaSpec['table_options'] ) );
55        } else {
56            $table->addOption( 'table_options', '/*$wgDBTableOptions*/' );
57        }
58
59        return $schema;
60    }
61}